<div>
<ul>
<li>딸기</li>
<li>수박</li>
<li class="orange">오렌지</li>
<li>망고</li>
<li>멜론</li>
<ul>
</div>
1. ABC ~ XYZ
🌙선택자 ABC의 다음 형제 요소 XYZ 모두를 선택
🌙[ex]
.orange ~ li { color: red;}
2. ABC + XYZ
🌙선택자 ABC의 다음 형제 요소 XYZ 하나를 선택 (가장 인접해 있는, 바로 다음 형제)
🌙[ex]
.organge <a>+</a> li { color: red;}
3. ABC XYZ
🌙'하위 선택자', 선택자 ABC의 하위 요소 XYZ 선택. '띄어쓰기'가 선택자의 기호!
🌙[ex]
div .orange { color: red; }
4. ABC > XYZ
🌙'자식 선택자', 선택자 ABC의 자식 요소 XYZ 선택
🌙[ex]
ul > .orange { color: red; }
5. ABCXYZ
🌙'일치 선택자', 선택자 ABC와 XYZ를 동시에 만족하는 요소 선택
🌙태그를 먼저 쓰고, class 쓰는 순서로 해야 원하는대로 작동함
🌙[ex]
span.orange { color: red; }
<div>
<ul>
<li>딸기</li>
<li class="orange"> 오렌지 </li>
<span class="orange"> 오렌지 </span>
<ul>
</div>
[1] 동작을 처리해주는 가상 선택자
1. ABC:hover
🌙[ex] 100x100size의 빨간색 사각형에 마우스를 올리면 파란색으로 변화
.box {
width: 100px;
height: 100px;
background-color: red;
transition: 1s;
}
.box:hover { background-color: blue;}
<div class="box"> </div>
2. ABC:active
🌙[ex] 100X100size의 빨간색 사각형을 클릭하고 있는 동안 변화 (마우스 버튼을 누르고 있지 않으면 아무 변화가 없다.)
.box {
width: 100px;
height: 100px;
background-color: red;
transition: 1s;
}
.box:active { background-color: blue;}
<div class="box"> </div>
3. ABC:focus
🌙[ex]
.box { width: 10px; height: 10px; color: red; transition: 1s;}
.box:focus { color: blue; }
<div class="box" tabindex="-1"></div>
⭐순서(값)을 -1이 아닌 다른 값을 넣는 것은 논리적 흐름을 방해하기 때문에 권장하지 않는다.
⭐검색 추천: tabindex mdn
🌟CSS는 동작을 처리하는 언어가 아니지만, 위의 3가지 가상 선택자 로 극히 일부는 가능하다.
[2] 특정한 요소를 선택하는 가상 클래스 선택자
<div class="fruits">
<span>딸기</span>
<span>수박</span>
<div>오렌지</div>
<p>망고</p>
<h3>사과</h3>
</div>
1. first-child
🌙선택자 ABC가 형제 요소 중 첫째라면 선택
🌙ABC:first-child
🌙[ex1]
.fruits span:first-child { color: red; }
🌙[ex2]
.fruits div:first-child { color: red; } ❌오류❌
⭐원인: first-child이면서 div 태그인 것이 없기 때문
(div:first-child는 일치 선택자)
2. last-child
🌙선택자 ABC가 형제 요소 중 막내라면 선택
🌙ABC:last-child
🌙[ex]
.fruits h3:last-child { color: red; }
3. nth-child
🌙선택자 ABC가 형제 요소 중 (n)째라면 선택
🌙ABC:nth-child(n)
🌙[ex1]
.fruits *:nth-child(2) { color: red; }
*
: 전체 선택자
n
은 숫자 0부터 시작한다.
🌙[ex2]
.fruits *:nth-child(2n) { color: red }
<div class="fruits">
<span>수박</span>
<p>망고</p>
🌙[ex3]
.fruits *:nth-child(2n+1) { color: red }
🌙[ex4]
.fruits *:nth-child(n+2) { color: red }
<div class="fruits">
// 2, 3, 4, 5번째 요소 선택
4. not
🌙부정 선택자 (Negation)
🌙선택자 XYZ가 아닌 ABC 요소 선택
🌙ABC :not(XYZ)
🌙[ex]
.fruits *:not(span) { color: red; }
[ex] 앞! Content!
.box::before { content: "앞!"; }
<div class="box">
Content!
</div>
🌙[ex1] Content! 뒤!
.box::after {content: "뒤!"; }
<div class="box">
Content!
</div>
🌙[ex2]
.box {
width: 100px;
height: 100px;
background-color: orange;
}
.box::after {
content: "Youh";
}
<div class="box"></div>코드를 입력하세요
🌙[ex3] width와 height 변화를 주고 싶을 때: Content ""; & display: block
.box {
width: 100px;
height: 100px;
background-color: orange;
}
.box::after {
content: "";
width: 30px;
height: 30px;
background-color: royalblue;}
<div class="box">
Content!
</div>
❗아무 변화 없음❗
❗원인: inline 요소는 width와 height 값이 적용되지 않기 때문
.box {
width: 100px;
height: 100px;
background-color: orange;
}
.box::after {
content: "";
/* inline 요소를 block으로 출력되도록 속성 부여 */
✅display: block;
width: 30px;
height: 30px;
background-color: royalblue;
}
<div class="box">
Content!
</div>
🌙속성 ABC를 포함한 요소를 선택한다!
1. [속성 ABC를 포함한 요소] { 속성 : 값; }
🌙[ex1]
(빨간색으로 출력)
[disabled] {
color: red;
}
<input type="text" value="banana">
<input type="password" value="1234">
<input type="text" value="ABCD" disabled>
🌙[ex2]
<--- 빨강
<---- 빨강
<----- 빨강
[type] {color: red;}
<input type="text" value="banana">
<input type="password" value="1234">
<input type="text" value="ABCD" disabled>
2. ABC="XYZ": 속성 ABC를 포함해 값이 XYZ인 요소 선택
🌙[ex]
<---- 빨강
[type="password"] {color: red;}
<input type="text" value="banana">
<input type="password" value="1234">
<input type="text" value="ABCD" disabled>
🌙[ex1] Inherit 적용 전
.parent {
width: 300px;
height: 200px;
background-color: orange;
}
.child {
width: 200px;
height: 100px;
background-color: red;
}
<div class="parent">
<div class="child"> </div>
</div>
🌙[ex2] Inherit 적용 후
.parent {
width: 300px;
height: 200px;
background-color: orange;
}
.child {
width: inherit;
height: 100px;
background-color: red;
}
<div class="parent">
<div class="child"> </div>
</div>
🌙[기타] Viewport 위치 표현: <a>Position</a>, top/right/bottom/left
.parent {
width: 300px;
height: 200px;
background-color: orange;
}
.child {
width: inherit;
hieght: 100px;
background-color: red;
/* viewport에 고정해서 위치를 나타내겠다. */
position: fixed;
top: 100px;
right: 10px;
}
<div class="parent">
<div class="child"> </div>
</div>