μ νμ(Selector) - 2νΈ
βοΈ ν΄λΉ κΈμ ν¨μ€νΈμΊ νΌμ€ - νλ‘ νΈμλ κ°λ° κ°μμμ HTML & CSS, SASS(SCSS) Partμ λ°μμ κ°μ¬λμ κ°μμλ£λ₯Ό μ 리ν κ²μ λλ€.
κ°μ ν΄λμ€ μ νμ(Pseudo-Classes Selectors)#
Hover#
μμμ λ§μ°μ€(ν¬μΈν°)κ° μ¬λΌκ° μλ λμμλ§ μ ν
a:hover { font-weight: bold; font-size: 20px;}<a href="<https://google.com>">Google!</a>Active#
μμλ₯Ό λ§μ°μ€λ‘ ν΄λ¦νλ λμμλ§ μ ν
.box { width: 100px; height: 100px; background: tomato;}.box:active { background: yellowgreen;}<div class="box"></div>Focus#
μμκ° ν¬μ»€μ€ λ λμμλ§ μ ν
λνν μ½ν μΈ μμ μ¬μ© κ°λ₯, input ...
input:focus { border-color: red; width: 200px;}<input type="text" />hover, active, focusλ μ νμμ΄μ§λ§ JSμμμ μ΄λ²€νΈ κ°λ κ³Ό ν·κ°λ¦΄ μμ§κ° μλ€. (? μμ§ λͺ¨λ₯΄λ λ΄μ©)
First child#
νμ μμ μ€ μ²«λ²μ§Έ μμμΈ λͺ¨λ μμ μ€μμ μ ν
.fruits li:first-child { color: red;}<ul class="fruits"> <li>λΈκΈ°</li> <!--μ ν--> <li>μ¬κ³Ό</li> <li>μ€λ μ§</li> <li>λ§κ³ </li></ul>Last child#
νμ μμ μ€ λ§μ§λ§ μμμΈ λͺ¨λ μμ μ€μμ μ ν
.fruits li:last-child { color: red;}<ul class="fruits"> <li>λΈκΈ°</li> <li>μ¬κ³Ό</li> <li>μ€λ μ§</li> <li>λ§κ³ </li> <!--μ ν--></ul>N-th child#
νμ μμ μ€ nλ²μ§Έ μμμΈ λͺ¨λ μμ μ€μμ μ ν( n ν€μλ μ¬μ©μ 0λΆν° ν΄μ (Zero-base))
nν€μλ λ―Έμ¬μ©μ.fruits li:nth-child(2) { color: red;}<ul class="fruits"> <li>λΈκΈ°</li> <li>μ¬κ³Ό</li> <!--μ ν--> <li>μ€λ μ§</li> <li>λ§κ³ </li></ul>nν€μλ μ¬μ©μ.fruits li:nth-child(n + 3) { color: red;}/* nμ 0λΆν° μμμ΄λ―λ‘ 3μ΄μμ λͺ¨λ μμ */<ul class="fruits"> <li>λΈκΈ°</li> <li>μ¬κ³Ό</li> <li>μ€λ μ§</li> <!--μ ν--> <li>λ§κ³ </li> <!--μ ν--></ul>
:xxx-child μ μμ¬ν :
p:nth-child(1)μΈ κ²½μ° -pμΈ νκ·Έ μ€μμ νμ μμ μ€ μ²« λ²μ§Έ μμμΈ κ²μ΄μλλΌ νμ μμ μ€ μ²« λ²μ§Έ μμκ°pνκ·Έμ΄λ©΄ μ νμΌλ‘ ν΄μλλ κ² β μ μμμ€λͺ μ nth-of-type μ°Έμ‘° - κΈ°λ³Έ, νμ, μμ μ νμμμ νΌν© μ¬μ©μ μ£Όμμν¨..fruits :nth-child(1)μΈ κ²½μ° -class="fruits"μΈ μμμ νμ μ€, νμ μμ μ€ μ²« λ²μ§ΈμΈ μμλ λͺ¨λ μ νλ¨.
N-th of type#
νμ μμ μ€ νμ
(tag)μ nλ²μ§Έ μμ μ ν( n ν€μλ μ¬μ©μ 0λΆν° ν΄μ (Zero-base))
.fruits { font-size: 40px;}.fruits p:nth-of-type(1) { color: red;}<div class="fruits"> <div>λΈκΈ°</div> <p>μ¬κ³Ό</p> <!--μ ν--> <p>λ§κ³ </p> <span>μ€λ μ§</span></div>:nth-of-type() μ μμ¬ν :
- νμ (tag)μ nλ²μ§Έλ₯Ό μ°Ύλ κ²μ΄κΈ° λλ¬Έμ class λλ id μ νμλ₯Ό λ£μΌλ©΄ κ° νμ μ€ nλ²μ§Έ μ€μμ class λλ id μ νμλ‘ μ ννλ κ²μ΄μ§, class λλ id μ νμλ₯Όκ°λ μμ μ€ nλ²μ§Έλ₯Ό μ°Ύλ κ²μ΄ μλλ€.
λΆμ μ νμ(Negation Selector)#
ν΄λΉ μ νμκ° μλ μμλ₯Ό μ ν
.fruits { font-size: 40px;}.fruits li:not(.strawberry) { color: red;}<ul class="fruits"> <li>μ€λ μ§</li> <!--μ ν--> <li class="strawberry">λΈκΈ°</li> <li>λ§κ³ </li> <!--μ ν--> <li>μ¬κ³Ό</li> <!--μ ν--></ul>κ°μ μμ μ νμ(Pseudo-Elements Selectors)#
emmit λ¬Έλ² : ul>li{\$}*10
Before#
μμ λ΄λΆμ μμ λ΄μ©(content)μ μ½μ
ul li::before { content: 'μ«μ'; color: red;}<ul> <li>1</li> <li>2</li> <li>3</li></ul>μ μμ¬ν content μμ±μ μ€μ νμ§ μμΌλ©΄ μ무κ²λ 보μ΄μ§ μλλ€. λΉ λ΄μ©μ λ£κ³ μΆλ€λ©΄ content: ""; λ₯Ό μΆκ°ν΄μΌνλ€.
After#
μμ λ΄λΆμ λ€μ λ΄μ©(content)μ μ½μ
ul li::after { content: '.0';}<ul> <li>1</li> <li>2</li> <li>3</li></ul>::before λ ::after λͺ¨λ contentμ ν
μ€νΈ λΏ μλλΌ μ΄λ―Έμ§λ₯Ό μ½μ
νλ κ²λκ°λ₯νλ€. url ν¨μ λ₯Ό μ΄μ©νλ λ°©λ²μ΄ μλ€.
ul li::after { content: url('<https://heropy.blog/css/images/logo.png>');}<ul> <li>1</li> <li>2</li> <li>3</li></ul>μ μμ¬ν :before , :after λ λμνμ§λ§, μ΄λ κ³Όκ±°μ κ°μ ν΄λμ€ μ νμ, κ°μμμ μ νμ κ°λ μ΄ λΆλ¦¬λκΈ° μ μ μ¬μ©λλ κ²μΌλ‘ νμ¬λ ::before , ::after λ‘μ¬μ©νλ κ²μ΄ νμ€μ΄λ€.
μμ± μ νμ(Attribute Selectors)#
Attr#
μμ± attr μ ν¬ν¨ν μμ μ ν
[disabled] { opacity: 0.2;}<input type="text" value="HEROPY" /><input type="password" value="1234" /><input type="text" value="disabled text" disabled />Attr=Value#
μμ± attr μ ν¬ν¨νλ©° μμ± κ°μ΄ value μΈ μμ μ ν
[type='password'] { opacity: 0.5; color: red;}<input type="text" value="HEROPY" /><input type="password" value="1234" /><input type="text" value="disabled text" disabled />value μλ λ°μ΄νλ₯Ό λΆμΌ μλ λΆμ΄μ§ μμ μλ μλ€. type=password or type="password"
Attr^=Value#
μμ± attr μ ν¬ν¨νλ©° μμ± κ°μ΄ value λ‘ μμνλ μμ μ ν
[class^='btn-'] { font-weight: bold; border-radius: 20px;}<button class="btn-success">Success</button><button class="btn-danger">Danger</button><button>Normal</button>Attr\$=Value#
μμ± attr μ ν¬ν¨νλ©° μμ± κ°μ΄ value λ‘ λλλ μμ μ ν
[class$='btn-'] { font-weight: bold; border-radius: 20px;}[class$='success'] { color: green;}[class$='danger'] { color: red;}<button class="btn-success">Success</button><button class="btn-danger">Danger</button><button>Normal</button>μμ(Inheritance)#
.ecosystem { color: red;}<div class="ecosystem"> μνκ³ <div class="animal"> λλ¬Ό <div class="tiger">νΈλμ΄</div> <div class="lion">μ¬μ</div> <div class="elephant">μ½λΌλ¦¬</div> </div> <div class="plant">μλ¬Ό</div></div>μνκ³(.ecosystem)μ μ μ©λ μμμ΄ νμ μμλ€μ λͺ¨λ μ μ©λλ€.
μμλλ μμ±λ€(properties)#
κΈμλ₯Ό λ€λ£¨λ μμ±λ€μ΄ κΈ°λ³Έμ μΌλ‘ μμμ΄ λλ€. λλ¨Έμ§ μμ±λ€μ μμμ΄ λμ§ μλλ€.
font
font-size
font-weight
font-style
line-height
font-family
color
text-align
text-indent
text-decoration
letter-spacing
opacity
etc...
κ°μ μμ#
μλλ μμλμ§ μλ μμ±μ κ°μ μ μΌλ‘ μμμν¬ μ μλ€.
<div class="parent"> <div class="child"></div></div>.parent { position: absolute; /* μλλ μμλμ§ μλ μμ±κ³Ό κ° */}.child { position: inherit; /* λΆλͺ¨μ μμ±κ³Ό κ° position: absolute; μ κ°μ μμ λ°λλ€ */}μμλμ§ μλ μμ±λ inherit μ΄λΌλ κ°μ μ¬μ©ν΄μ λΆλͺ¨μμ μμμΌλ‘ κ°μ μμμν¬ μ μλ€. μμμ μ μΈν νμμκ²λ μ μ©λμ§ μμΌλ©°, λͺ¨λ μμ±μ΄ κ°μ μμμ μ¬μ©ν μ μλ κ²μ μλλ€.
μ°μ μμ(Priority)#
<body> <!--μΈλΌμΈ μ μΈλ°©μ--> <div id="color_yellow" class="color_green" style="color: orange;"> Hello world! </div></body>div { color: red !important;} /* !important */
#color_yellow { color: yellow;} /* id μ νμ */
.color_green { color: green;} /* class μ νμ */
div { color: blue;} /* tag μ νμ */
* { color: darkblue;} /* μ 체 μ νμ */
body { color: violet;} /* μμ */νλμ μμλ₯Ό κ°λ₯΄ν€λ μ¬λ¬ μ μΈλ€. μ΄λ€ κ²μ΄ μ νλ κΉ?
μ°μ μμ κ²°μ #
κ°μ μμκ° μ¬λ¬ μ μΈμ λμμ΄ λ κ²½μ°, μ΄λ€ μ μΈμ CSS μμ±(property)μ μ°μ μ μ©ν μ§ κ²°μ νλ λ°©λ²
λͺ μλ μ μκ° λμ μ μΈμ΄ μ°μ (λͺ μλ)
μ μκ° κ°μ κ²½μ°, κ°μ₯ λ§μ§λ§μ ν΄μ(λ¦κ² μμ±ν)λλ μ μΈμ΄ μ°μ (μ μΈ μμ)
λͺ μλλ 'μμ' κ·μΉλ³΄λ€ μ°μ (μ€μλ)
!importantκ° μ μ©λ μ μΈ λ°©μμ΄ λ€λ₯Έ λͺ¨λ λ°©μλ³΄λ€ μ°μ (μ€μλ)
μ°μ μμμλ 'μ€μλ, λͺ μλ, μ μΈ μμ'μ κ°λ μ΄ μλ€.
1. κ°μ₯μ€μ( !important )#
λͺ¨λ μ μΈμ 무μνκ³ κ°μ₯ μ°μ
μ μ : β pt
2. μΈλΌμΈ μ μΈ λ°©μ(Style Attribute)#
μΈλΌμΈ μ μΈ(HTML style μμ±μ μ¬μ©)
μ μ : 1000 pt
3. Id μ νμ(ID Selector)#
μμ΄λ μ νμ
μ μ : 100 pt
4. ν΄λμ€ μ νμ(Class Selector)#
ν΄λμ€ μ νμ
μ μ : 10 pt
5. νκ·Έ μ νμ(Tag Selector)#
νκ·Έ μ νμ
μ μ : 1 pt
6. μ 체 μ νμ(Universal Selector)#
μ 체 μ νμ
μ μ : 0 pt
7. μμ(CSS Inheritance)#
μμ λ°μ μμ±μ νμ μ°μ νμ§ μμ
μ μ : κ³μ°νμ§ μμ
.list li.item { color: red;} /* 21pt ν΄λμ€+νκ·Έ+ν΄λμ€ */
.list li:hover { color: red;} /* 21pt ν΄λμ€+νκ·Έ+(κ°μ)ν΄λμ€ */
.box::before { content: 'Good '; color: red;} /* 11pt ν΄λμ€+(κ°μ μμ)νκ·Έ */
#submit span { color: red;} /* 101pt μμ΄λ+νκ·Έ */
header .menu li:nth-child(2) { color: red;} /* 22pt νκ·Έ+ν΄λμ€+νκ·Έ+(κ°μ)ν΄λμ€ */
h1 { color: red;} /* 1pt νκ·Έ */
:not(.box) { color: red;} /* 10pt ν΄λμ€ */
:not(span) { color: red;} /* 1pt νκ·Έ */:hover μ κ°μ 'κ°μ ν΄λμ€'λ 'ν΄λμ€' μ νμ μ μ( 10pt )λ₯Ό κ°μ§λ©° 'κ°μ μμ'λ 'νκ·Έ' μ νμ μ μ( 1pt )λ₯Ό κ°μ§λ€. λΆμ μ νμ :not() λ μ μλ₯Ό κ°μ§μ§μλλ€.
Reference#
ν¨μ€νΈμΊ νΌμ€ - νλ‘ νΈμλ κ°λ° κ°μ - HTML & CSS, SASS(SCSS) Part by ParkYoungWoong