/* 1. [attr] */
a[target] {
color: red;
}
/* 2. [attr=value] */
a[href="https://example.org"] {
color: indigo;
}
/* 3. [attr^=value] */
a[href^="http://"] {
color: green;
}
/* 4. [attr$=value] */
a[href$=".com"] {
font-style: italic;
}
/* 5. [attr*=value] */
a[href*="example"] {
color: sienna;
}
[attr=value]
μμ± μ νμλ λ³΄ν΅ input μμμ νμ
λ³λ‘ μ€νμΌμ μ μ©νκ³ μΆμ λ μ¬μ©νλ€./* 1. :first-child */
li:first-child {
color: green;
}
/* 2. :last-child */
span:last-child {
color: tomato;
}
/* 3. :nth-child() */
li:nth-child(3) {
color: hotpink;
}
/* 4. :first-of-type */
p:first-of-type {
color: red;
}
/* 5. :last-of-type */
p:last-of-type {
color: blue;
}
/* 6. :nth-of-type() */
p:nth-of-type(even) {
color: blue;
}
/* 7. :not() */
input:not(.pw) {
background-color: indianred;
}
/* 8. :link */
a:link {
color: tomato;
}
/* 9. :visited */
a:visited {
color: yellow;
}
/* 10. :hover */
input[type=button]:hover {
background-color: teal;
color: white;
}
/* 11. :active */
input[type=button]:active {
background-color: red;
color: white;
}
/* 12. :focus */
input[type=text]:focus {
background-color: blue;
color: white;
}
/* 13. :enabled */
input[type=text]:enabled {
background-color: skyblue;
}
/* 14. :disabled */
input[type=text]:disabled {
background-color: silver;
}
/* 15. :checked */
input[type=radio]:checked {
outline: 3px solid red;
}
:first-child
κ°μ ν΄λμ€ μ νμλ νμ μμλ€ μ€μμ 첫 λ²μ§Έ μμ μμλ₯Ό κ°λ¦¬ν¨λ€.:first-of-type
κ°μ ν΄λμ€ μ νμλ€.:nth-child()
κ°μ ν΄λμ€ μ νμμ κ΄νΈ μμλ μ«μ λΏλ§ μλλΌ oddμ even ν€μλλ λ£μ μ μλ€.:first-of-type
κ°μ ν΄λμ€ μ νμλ ν΄λΉ μμλ€ μ€μμ 첫 λ²μ§Έ μμλ₯Ό κ°λ¦¬ν¨λ€.:not()
κ°μ ν΄λμ€ μ νμλ κ΄νΈ μμ μμ±λ μμλ₯Ό μ μΈν λλ¨Έμ§ λͺ¨λ μμμ μ€νμΌμ μ μ©νλ€.:active
κ°μ ν΄λμ€ μ νμλ mousedown μ΄λ²€νΈκ° λ°μνμ λ μ μ©λλ μ€νμΌμ μ§μ ν μ μλ€./* 1. ::before */
.movie::before {
content: 'β';
color: indianred;
}
/* 2. ::after */
.movie::after {
content: 'β';
color: indianred;
}
/* 3. ::first-letter */
.lorem::first-letter {
color: red;
font-size: 30px;
}
/* 4. ::first-line */
.lorem::first-line {
color: red;
}
/* 5. ::seletion */
.lorem::selection {
background-color: blue;
}
::before
κ°μ μμ μ νμλ₯Ό μ¬μ©ν μμμ ::first-letter
κ°μ μμ μ νμλ₯Ό μ¬μ©νλ©΄ ::before
κ°μ μμ μ νμκ° κ°λ¦¬ν€λ μμμ 첫 λ²μ§Έ ν
μ€νΈμ μ€νμΌμ΄ μ μ©λλ€.::first-line
κ°μ μμ μ νμλ λ·°ν¬νΈ κ°λ‘ λλΉμ μκ΄μμ΄ λ¬΄μ‘°κ±΄ 첫 μ€λ§ μ€νμΌμ΄ μ μ©λλ€.::selection
κ°μ μμ μ νμλ μ ν μμμ λν μ€νμΌμ μ μ©ν λ μ¬μ©νλ€./* 1. νμ μ νμ */
#list li:last-of-type {
color: red;
}
/* 2. μμ μ νμ */
#list > li:last-of-type {
color: red;
}
/* 3. μΌλ° νμ μ νμ */
code ~ p {
color: red;
}
/* 4. μΈμ νμ μ νμ */
code + p {
color: red;
}
/* 5. κ·Έλ£Ήν */
div, p {
color: red;
}
* {
color: red;
}
div > * {
color: red;
}
index.html
<div class="parent1">
parent1
<div class="child1">child1</div>
<div class="child2">child2</div>
</div>
<div class="parent2">
parent2
<div class="child1">child1</div>
<div class="child2">child2</div>
</div>
main.css
div {
border: 1px solid silver;
margin: 2px
}
.child1 {
color: red;
}
.parent1, .parent2 {
color: blue;
}
.parent2 .child1 {
all: unset;
}
all: initial
κ°μ μ¬μ©νλ©΄ λΆλͺ¨ μμλ‘λΆν° μμλ°μ λͺ¨λ μ€νμΌμ κΈ°λ³Έκ°μΌλ‘ λ³κ²½ν μ μλ€.all: inherit
κ°μ μ¬μ©νλ©΄ λΆλͺ¨ μμλ‘λΆν° μμλ°μ λͺ¨λ μ€νμΌμ μ μ©νλ€.all: unset
κ°μ μ¬μ©νλ©΄ λΆλͺ¨λ‘λΆν° μμλ°μ κ°μ΄ μμΌλ©΄ inheritμΌλ‘ μλνκ³ , λΆλͺ¨λ‘λΆν° μμλ°μ κ°μ΄ μμΌλ©΄ initialλ‘ μλνλ€.all: unset
μ μ μ©ν¨μΌλ‘μ¨ ν΄λΉ μμλ₯Ό μ΄κΈ°νλ₯Ό μν€κ³ , ν΄λΉ μμμ λΆλͺ¨κ° κ°μ§κ³ μλ μ€νμΌλ§ μμλ°μ μ¬μ μν μ μλ€.