⚠️ body의 직계 자손의 경우,
:first-child
선택자가 적용되지만,:last-child
는 적용되지 않는다.
li:nth-child(even) {
color: pink;
}
li:nth-child(2n+1) {
color: pink;
}
<div class="container">
<h1>Hello world</h1>
<span>Hello world</span>
<p>Hello world</p>
<span>HTML</span>
<p>CSS</p>
</div>
<style>
span:nth-of-type(2) {
color: pink;
}
</style>
⚠️ nth-child 와 nth-of-type 다른점? ? ?
nth-child는 부모의 모든 자식 요소 중 n번째를 나타나는 반면 nth-of-type은 부모의 특정 자식 요소 중 n번째를 말한다.
<div class="container">
<h1>HTML</h1>
<h2>CSS</h2>
<h3>JavaScript</h3>
</div>
<style>
h2:only-of-type {
color: pink;
}
</style>
<div class="container">
<p>HTML</p>
<p>CSS</p>
<p>JavaScript</p>
</div>
<style>
p:not(:first-child) {
color: pink;
}
</style>
:root {
--color-yellow: rgb(255, 255, 189);
--font-size: 12px;
--box-shadow: 1px 1px 1px #ddd;
}
-- (하이픈 2개) 속성이름 : 속성값 ;
.container {
background-color: var(--color-yellow);
box-shadow: var(--box-shadow);
}
span {
font-size: var(--font-size);
}
선택자::가상요소 {속성: 속성값};
Content
img
, br
, input
에는 적용할 수 없다. <ul>
<li>첫번째</li>
<li>두번째</li>
<li>세번째</li>
</ul>
<style>
li::before {
content: "#";
}
li::after {
content: "!";
}
</style>
<input type="text" placeholder="텍스트를 입력해주세요" />
<style>
input::placeholder {
color: pink;
}
</style>
여기서 "활성"은 마우스 버튼을 누르는 순간부터 떼는 시점까지!
a
,button
,input
가 포커스의 요소들이다.
이는 모두 클릭, 입력 등 사용자와 상호작용이 있는 요소들
Reference