CSS의 선택자인 가상클래스선택자, 가상요소선택자, 속성선택자에 대해서 알아보자
CSS에서도 "동작"을 제어하는 가상클래스 선택자를 제공한다.
<style>
.box {
width:100px;
height:100px;
background-color:orange;
transition:1s;
}
.box:hover{
width:300px;
background-color:royalblue;
}
</style>
</head>
<body>
<div class="box">
</div>
</body>
<a href="www.naver.com" src="네이버">네이버</a>
<style>
a.active{
color:red;
}
</style>
</head>
<body>
</body>
<input type="text">
input:focus{
background-color:orange;
}
<style>
.fruits span(하위선택자,fruit가 class인 tag의 자식요소인 span tag):first-child{
color:red;
}
</style>
</head>
<body>
<div class="fruits">
<span>딸기</span>
<span>수박</span>
<div>오렌지</div>
<p>망고</p>
<h3>사과</h3>
</div>
</body>
<style>
.fruits span:last-child{
color:red;
}
</style>
</head>
<body>
<div class="fruits">
<span>딸기</span>
<span>수박</span>
</div>
</body>
<style>
.fruits *(전체 자식 tag):nth-child(2){
color:red;
}
</style>
</head>
<body>
<div class="fruits">
<span>딸기</span>
<span>수박</span>
<div>오렌지</div>
</div>
</body>
출력화면
짝수번째 선택 : fruits *:nth-child(2n)
홀수번째 선택 : fruits *:nth-child(2n+1)
2번째 요소부터 시작하기(선택하기) : fruits *:nth-child(n+2)
<style>
.fruits div:not(span){
color:red;
}
</style>
</head>
<body>
<div class="fruits">
<span>딸기</span>
<span>수박</span>
<div>오렌지</div>
</div>
</body>
가상요소선택자=가상태그선택자(혼용)
::before, ::after 선택자에는 content 속성이 비어있더라도 들어가 있어야 한다
속성의 이름을 가지고 선택하는 선택자
ex> <input type="text" value="Sharon" disabled>
[disabled]{
color:red;
}
ex> <input type="text" value="Sharon" disabled>
<input type="password" value="1234">
<input type="text" value="ABCD">
[type]{
color:red;
}
ex> <input type="text" value="Sharon" disabled>
<input type="password" value="1234">
<input type="text" value="ABCD">
[type="ABCD"]{
color:red;
}