: Type, (*), class, and ID
태그 이름 선택자, 요소 선택자
span
, p
, img
.class
, #id
: attr, attr^, attr$, attr*
속성으로 ~ 을 가지고 있는 요소를 선택한다
<a href="http://example.com" target="_blank">
a[target] /* target 속성을 가진 요소 */
a[href="https://example.com"] /* href 속성이 = 뒤의 값인 경우 */
a[href^="https://"] /* 해당 내용으로 시작하는 */
a[href$=".com"] /* 해당 내용으로 끝나는 */
a[href^="example"] /* 해당 내용을 포함하는 */
selector
로 인해 선택된 것들의 형제 요소들 중 첫, n, 마지막 요소를 나타낸다.nth-child
와 달리 위에 나타난 특징을 상쇄해주는 선택자이다. nth-of-type
은 해당 타입을 가지고 있는 요소의 몇 번째를 의미한다.
-child
보다 선호됨.<div>a</div>
<p>b</p>
<p>c</p>
<div>d</div>
p:first-child{} /* 해당하는 요소 없음 */
p:first-of-type{} /* b */
<form action="">
<input type="text" placeholder="id">
<input type="email" placeholder="email">
<input type="password" class="pw" placeholder="password">
<input type="submit">
</form>
input:not(.pw) {}
input
요소들 중 class="pw"
인 요소를 제외한 나머지를 뜻함.input:not([placeholder]) {}
input
요소들 중 placeholder 속성이 없는 요소만을 뜻함.input:not([type="text"]) {}
input
요소들 중 type="text"
인 요소를 제외한 나머지를 뜻함.<div>
<p id="first" class="sentence">This text is selected!</p>
<p id="second" class="sentence">This text isn't selected.</p>
<p id="third" class="sentence">This text isn't selected.</p>
</div>
<div>
<h2>This text isn't selected: it's not a `p`.</h2>
<p id="last">This text isn't selected.</p>
</div>
위와 같은 코드가 있다고 할 때, 각각의 선택자는 무엇을 선택할까?
p
의 형제 요소 중 첫번 째인id="first"
인 요소만 선택된다.
h2
는p
가 아니기 때문에 첫 요소임에도 선택 xid="last"
인p
도 첫 번째 요소가 아니라서 선택 x-child
는 단순히 해당 속성을 가지고 있는 요소의 몇 번째를 의미하는 게 아니라, 해당 속성을 가지고 있는 요소의 형제들 중 몇 번째를 의미한다.
class="sentence"
인 요소들의 형제 요소 중 마지막인id="third"
가 선택된다.
- 아래와 같이
id="third"
인 블럭에서 class 속성을 없앤다고 해서id="second"
가 선택되지 않는다. 이 때는 해당하는 요소가 사라지게 된다.<p id="first" class="sentence">This text isn't selected!</p> <p id="second" class="sentence">This text isn't selected.</p> <p id="third">This text was selected.</p>
id="third"
인 p
와 id="last"
인 p
가 선택된다.
- 차이점은 공백의 유무이다.
div:last-child
의 경우 두 번째div
가 선택되었을 것이다. 하지만 공백은 그 내부 자식으로 넘어가기 때문에 div중 마지막 요소를 찾게 된다.
id="last"
인 p
가 선택된다.div p :not(.class)
라면, div
안에 p
안에 class
를 갖지 않은 요소를 선택했을 것이다. 현재의 경우는 존재하지 않는다.마우스 움직임에 따라 반응하는 선택자
❗
:active
는 뒤에 다른 링크 의사 클래스가 올 경우 덮어씌워진다. 따라서 제일 마지막에 배치할 것!
LVHA 순서를 지키는 게 중요함
link
: 방문하기 전
visited
: 방문 후
hover
: 마우스를 올렸을 때
active
: 누르고 있을 때
+focus
: 해당 요소가 포커싱 됐을 때
: enabled, disabled, checked
해당 요소에 속성으로 위와 같은 속성이 적용됐을 때 적용
주로 input 에서 사용된다.
::before
요소의 콘텐츠 시작 부분에 추가
::after
끝부분에 생성된 콘텐츠 추가
content
를 통해 생성될 내용 추가
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
</ul>
li {list-style: none; display: inline;}
li::after{content:"I"; padding-left: 5px;}
li:last-child::after{content:"";}
first-letter
첫 번째 글자
first-line
첫 번째 줄
<p>lorem, ipsum....</p>
<p>lorem, ipsum....</p>
p::first-letter { color: red; font-size: 2rem;}
p::first-line { font-weight: bold; }
이때
::before
가상 요소를 추가한다면, 스타일링 블럭이 앞에 있든 뒤에 있든 상관없이::before
은 코드 가장 맨 앞에 추가되어 before에 css가 먹게 된다.
selection
요소를 드래그했을 때 반응하는 css
p:first-child::selection {background-color: yellow;}
ul li :a
ul > li:a
선택한 코드의 형제 중 조건을 만족하고, 뒤에 있는 요소 선택
<p>p1</p>
<code>code</code>
<p>p2</p>
<span>span1</span>
code ~ p {color: red;}
code 태그 뒤쪽에 있는 p 태그만 붉게 바꾼다.
code + p {color:red;} /* 적용됨 */
code + span {color:red;} /* 적용 안 됨 */
다른 타입의 요소들에게 한번에 스타일을 주고 싶을 때 사용
p, span, div {color: red;}
div > * {} /* div 하위의 모든 요소 */
div ~ * {} / div 아래에 있는 모든 요소
child {color:initial} /* 부모의 스타일 적용 x */
child {all:initial} /* 모든 상속 x */
부모로부터 상속받을 값이 존재할 때 inherit
부모로부터 상속받을 값이 없을 때initial
*나 상위의 상위 요소 등의 스타일은 모두 무시하고, 부모에게 직속 상속 받은 속성만 적용한다.
!important > 인라인 스타일(1000) > id(100) > class, attribute, pseudo-class(10) > 요소(또는 가상 요소)가 많은 순(1)