μ νμλ CSSμμ μ€νμΌμ μ ν HTML λ± λ§ν¬μ μΈμ΄μ μμλ₯Ό μ ννλ λ¬Έμλ₯Ό λ»νλ€. λλΆλΆμ CSS μ½λλ A { (B): (C); } ννλ‘ μ΄λ€μ Έ μλλ°, μ€νμΌμ λμ΄ν μ€κ΄νΈ μ λΆλΆμ μ€λ A λΆλΆμ΄ λ°λ‘ μ νμλ€. μ΄λ² ν¬μ€νΈλ μ νμμ μ νκ³Ό μ‘°ν©μ λν΄ μμλ³΄κ³ , μ νμμ λ¬Έμμ κ΄κ³ κΈ°μ€μΌλ‘ λ¬Έμ ꡬ쑰λ₯Ό μ΄ν΄ν΄ λ³Έλ€. <ν΄λΉ ν¬μ€νΈλ λΉμ 곡μλ₯Ό μν HTML/CSS κ°μ κΈ°λ°μΌλ‘ μμ±λλ€>
μμ μ νμ
h1 { color: yellow; }
h2 { color: yellow; }
h3 { color: yellow; }
h4 { color: yellow; }
h5 { color: yellow; }
h6 { color: yellow; }
μ νμ μ€ κ°μ₯ κΈ°λ³Έμ΄ λλ μ νμλ‘, μ νμ λΆλΆμ νκ·Έ μ΄λ¦μ΄ λ€μ΄κ°λ μ μ λ°μ νκ·Έ μ νμλΌκ³ λ νλ€. μμ μ νμλ₯Ό μ¬μ©νλ©΄ λ¬Έμ λ΄ μ νμμ ν΄λΉνλ λͺ¨λ μμμ μ€νμΌ κ·μΉμ΄ μ μ©λλ€.
κ·Έλ£Ήν
h1, h2, h3, h4, h5, h6 { color: yellow; }
* { color: yellow; }
μ νμλ κΈ°λ³Έμ μΌλ‘ μΌνλ₯Ό μ΄μ©ν΄μ κ·Έλ£Ήνν μ μκ³ , asterisk κΈ°νΈ(*)λ‘ λ¬Έμ λ΄μ λͺ¨λ μμλ₯Ό μ νν μλ μλ€.
h1 { color: yellow; font-size: 2em; background-color: gray; }
h1, h2, h3, h4, h5, h6 { color: yellow; font-size: 2em; background-color: gray; }
μ νμ λΏ μλλΌ μ μΈλ κ·Έλ£Ήν κ°λ₯νλ€. λ¬Όλ‘ μ νμμ μ μΈμ λμμ κ·Έλ£Ήνν μλ μλ€.
μμ μ νμμ νκ³
μλμ κ°μ μ½λκ° μλ€κ³ κ°μ νμ.
<dl>
<dt>HTML</dt>
<dd><span>HTML</span>μ λ¬Έμμ ꡬ쑰λ₯Ό λνλ
λλ€.</dd>
<dt>CSS</dt>
<dd><span>CSS</span>λ λ¬Έμμ ννμ λνλ
λλ€.</dd>
</dl>
λΉμ μ ν΄λΉ HTML λ¬Έμμ "HTML" λΆλΆμλ 보λΌμ κΈμλ₯Ό μ§μ νκ³ , "CSS" λΆλΆμλ λ°μ€μ μ μΈνκ³ μΆλ€. νμ§λ§ μμ μ νμλ‘λ λμΌν μ΄λ¦μΌλ‘ λ¬Άμ¬ μλ μμλ₯Ό κ°κ° μ νν΄μ κ·μΉμ μ μ©νκΈ°λ λΆκ°λ₯νλ€. <dt>μ 보λΌμμ μ§μ νλ©΄ "CSS"λ 보λΌμμΌλ‘ λ³ν κ²μ΄κ³ , λ°λλ‘ <dt>μ λ°μ€μ κ·ΈμΌλ©΄ "HTML"μλ λμΌνκ² λ°μ€μ΄ κ·Έμ΄μ§ κ²μ΄λ€. λΉμ μ΄ μνλ λ°©μλλ‘ λ¬Έλ§₯λ§λ€ μ€νμΌμ κ°κ° μ μ©ν λ°©λ²μ μμκΉ?
class μ νμ
.foo { font-size: 30px; }
<p class="foo"> ... </p>
μμμ ꡬμ λ°μ§ μκ³ μ€νμΌ κ·μΉμ μ μ©μν€λ κ°μ₯ μΌλ°μ μΈ λ°©λ²μ class μ νμλ₯Ό νμ©νλ κ²μ΄λ€. HTMLμμ λμΌν μ€νμΌμ μ μ©λ°μ classλͺ μ μ μ΄ λκ³ CSSμμ μ μ©ν κ·μΉμ μ¨ λμΌλ©΄ λμΌν classλͺ μ κ°μ§ λͺ¨λ μμμ λμΌν μ€νμΌ κ·μΉμ΄ μ μ©λλ€. κ·ΈλΌ class μ νμλ₯Ό μ΄μ©ν΄ μμ μ νμλ‘ κ΅¬νν΄λ΄μ§ λͺ» νλ λΆλΆμ ꡬνν΄ λ³΄μ.
.html { color: purple; }
.css { text-decoration: underline; }
<dl>
<dt class="html">HTML</dt>
<dd><span class="html">HTML</span>μ λ¬Έμμ ꡬ쑰λ₯Ό λνλ
λλ€.</dd>
<dt class="css">CSS</dt>
<dd><span class="css">CSS</span>λ λ¬Έμμ ννμ λνλ
λλ€.</dd>
</dl>
μ΄λ, ν μμλ class μμ±μ λμμ μ¬λ¬ κ° κ°μ§ μ μλ€. 곡백μΌλ‘ ꡬλΆν΄ μ¬λ¬ κ°μ class κ°μ λ£μ΄ μ£Όλ©΄ λλ€.
.foo { font-size: 30px; }
.bar { color: blue; }
<p class="foo bar"> ... </p>
μ HTMLλ¬Έμ <p> μμλ "foo"μ "bar"λΌλ λ κ°μ classκ° μ μΈλΌ μλ€. CSSλ¬Έμλ λ foo classκ° 30pxμ ν°νΈ ν¬κΈ°λ₯Ό κ°κ², bar classλ κΈμλ₯Ό νλμμΌλ‘ μ μ©νλλ‘ μ€μ λΌ μλ€. μ μ½λλ₯Ό μ€ννλ©΄, <p>μ foo, bar λ λͺ¨λμ κ·μΉμ΄ μ μ©λκ³ μλ€λ κ²μ μ μ μλ€.
id μ νμ
#bar { background-color: yellow; }
<p id="bar"> ... </p>
id μ νμλ class μ νμμ κ±°μ μ μ¬νλ€. λ€λ§ .(λ§μΉ¨ν) λμ #(ν΄μ) κΈ°νΈλ₯Ό μ΄λ€λ μ κ³Ό, μμμ class μμ± λμ id μμ±μ μ¬μ©νλ€λ μ μ΄ λ€λ₯΄λ€. λ, μ¬λ¬ μμμ μ€λ³΅ μ μΈλ μ μλ classμ λ€λ₯΄κ² idλ λ¬Έμ λ΄μμ λ¨ ν λ²λ§ μ°μΌ μ μλ€.
μ νμμ μ‘°ν©
/* μμμ classμ μ‘°ν© */
p.bar { ... }
/* λ€μ€ class */
.foo.bar { ... }
/* idμ classμ μ‘°ν© */
#foo.bar { ... }
μ μμ μ κ°μ΄, μ¬λ¬ μ νμλ₯Ό μ‘°ν©ν΄ μ¬μ©ν μλ μλ€.
μμ± μ νμ
p[class] { color: silver; }
p[class][id] { text-decoration: underline; }
<p class="foo">Hello</p>
<p class="bar">CSS</p>
<p class="baz" id="title">HTML</p>
μμ± μ νμλ ν μ νμμ λ¬λ¦¬ λκ΄νΈλ₯Ό μ΄μ©ν΄ λ¬Άκ³ λκ΄νΈ μμ μμ± μ΄λ¦μ΄ λ€μ΄κ°λ μ νμλ€. μμμ ν΄λΉ μ΄λ¦μ μμ±μ΄ μλ€λ©΄ μ μ©λλ€.
μ νν μμ±κ°μΌλ‘ μ ν
<p class="foo">Hello</p>
<p class="bar">CSS</p>
<p class="baz" id="title">HTML</p>
class, idκ° μ‘΄μ¬ μμ²΄λ‘ λ루λμ λ¬Άμ μλ μμ§λ§ μ΄μ²λΌ λͺ
νν μμ±μ μ§μ ν΄ μ€ μλ μλ€. μ΄μ²λΌ μμ± μ νμμ μμ±κ°κΉμ§ μ§μ ν΄ μ£Όλ©΄ λμΌν μμ±μ λμΌν μμ±κ°μ κ°μ§ λΆλΆμλ§ μ€νμΌμ΄ μ μ©λλ€.
λΆλΆ μμ±κ°μΌλ‘ μ ν
<p class="color hot">red</p>
<p class="cool color">blue</p>
<p class="colorful nature">rainbow</p>
p[class~="color"] { font-style: italic; }
p[class^="color"] { font-style: italic; }
p[class$="color"] { font-style: italic; }
p[class*="color"] { font-style: italic; }
[class~="bar"] : class μμ±μ κ°μ΄ 곡백μΌλ‘ ꡬλΆν "bar" λ¨μ΄κ° ν¬ν¨λλ μμ μ ν.
[class^="bar"] : class μμ±μ κ°μ΄ "bar"λ‘ μμνλ μμ μ ν.
[class$="bar"] : class μμ±μ κ°μ΄ "bar"λ‘ λλλ μμ μ ν.
[class*="bar"] : class μμ±μ κ°μ΄ "bar" λ¬Έμκ° ν¬ν¨λλ μμ μ ν.
p[class~="color"] { font-style: italic; } /* 1, 2λ²μ§Έ μμ */
p[class^="color"] { font-style: italic; } /* 1, 3λ²μ§Έ μμ */
p[class$="color"] { font-style: italic; } /* 2λ²μ§Έ μμ */
p[class*="color"] { font-style: italic; } /* 1, 2, 3λ²μ§Έ μμ */
λ¬Έμ ꡬ쑰μ μ΄ν΄
<html>
<body>
<div>
<h1><span>HTML</span>: Hyper Text Markup Language</h1>
</div>
<p>HTMLκ³Ό CSSμ JAVASCRIPTλ₯Ό μ΄μ©ν΄μ λ©μ§ μΉ μ¬μ΄νΈλ₯Ό μ μν μ μμ΅λλ€.</p>
</body>
</html>
μ νμμ λ¬Έμμ κ΄κ³λ₯Ό μ΄ν΄νκΈ° μν΄ λ¨Όμ μ΄λ»κ² λ¬Έμκ° κ΅¬μ‘°νλλμ§ μ΄ν΄λ³΄μ. μ΄λ€μ κ΄κ³λ λΆλͺ¨μ μμ, μ‘°μκ³Ό μμ, κ·Έλ¦¬κ³ νμ κ΄κ³λ‘ λΆλ₯λ μ μλ€.
λΆλͺ¨μ μμ κ΄κ³
λΆλͺ¨ μμλ κ·Έ μμλ₯Ό ν¬ν¨νλ κ°μ₯ κ°κΉμ΄ μμ μμλ‘, ν μμμ λΆλͺ¨ μμλ νλλΏμ΄λ€. μμ μμλ λΆλͺ¨ μμμ λ°λλΌκ³ μκ°νλ©΄ λλ€. λ€λ§ ν μμμ λΆλͺ¨ μμλ ν κ°λ§ μ‘΄μ¬ν μ μλ λ°λ©΄ ν λΆλͺ¨ μλμ μμ μμλ μ¬λ¬ κ° μ‘΄μ¬ν μ μλ€.
<body>μ λΆλͺ¨ μμ : <html> ↔ <html>μ μμ μμ : <body>
<div>μ λΆλͺ¨ μμ : <body> ↔ <body>μ μμ μμ : <div>, <p>
<h1>μ λΆλͺ¨ μμ : <div> ↔ <div>μ μμ μμ : <h1>
<span>μ λΆλͺ¨ μμ : <h1> ↔ <h1>μ μμ μμ : <span>
<p>μ λΆλͺ¨ μμ : <body> ↔ <body>μ μμ μμ : <div>, <p>
μ‘°μκ³Ό μμ κ΄κ³
μ‘°μκ³Ό μμμ κ΄κ³ μ¬λ¬ λΆλͺ¨ μμ κ΄κ³λ₯Ό ν¬ν¨ν΄ νμ₯ν κ΄κ³λΌκ³ 보면 λλ€. μ‘°μ μμλ ν μμλ₯Ό ν¬ν¨νλ (λΆλͺ¨ μμλ₯Ό ν¬ν¨ν) λͺ¨λ μμκ³ , μμ μμλ λ°λλ‘ κ·Έ μμκ° ν¬ν¨νκ³ μλ λͺ¨λ μμλ€.
<body>μ μ‘°μ μμ : <html> ↔ <html>μ μμ μμ : <body>, <div>, <h1>, <span>, <p>
<div>μ μ‘°μ μμ : <html>, <body> ↔ <body>μ μμ μμ : <div>, <h1>, <span>, <p>
<h1>μ μ‘°μ μμ : <html>, <body>, <div> ↔ <div>μ μμ μμ : <h1>, <span>
<span>μ μ‘°μ μμ : <html>, <body>, <div>, <h1> ↔ <h1>μ μμ μμ : <span>
<p>μ μ‘°μ μμ : <html>, <body> ↔ <body>μ μμ μμ : <div>, <h1>, <span>, <p>
νμ κ΄κ³
μλ‘ κ°μ λΆλͺ¨λ₯Ό κ°μ§κ³ μλ μμ μ¬μ΄μ κ΄κ³λ₯Ό νμ κ΄κ³λΌκ³ νλ€. μ μ½λ μ€ <div>, <p>κ° νμ κ΄κ³λ€. νμ κ΄κ³ μ€ λ°λ‘ λ€μ μλ μμλ₯Ό μ§μΉν κ²½μ°λ μΈμ ν κ΄κ³λ₯Ό κ°μ§λ€, νΉμ μΈμ νλ€κ³ λΆλ₯Έλ€. μ μ½λμμλ <p>κ° <div>μ μΈμ ν νμ μμκ° λλ€.
λ¬Έμ ꡬ쑰 κ΄λ ¨ μ νμ
λ¬Έμ ꡬ쑰λ₯Ό μ΄μ©ν μ νμλ‘ μμ μ νμ, μμ μ νμ, μΈμ μ νμκ° μλ€.
μμ μ νμ
div span { color: red; }
μμ μ νμλ μ νμ μ¬μ΄μ μ무 κΈ°νΈ μμ΄ κ³΅λ°±μΌλ‘ ꡬλΆνλ€. μ΄ μ νμλ <div>μ μμ μμμΈ <span>μ μ ννλ μ νμλ€.
μμ μ νμ
div > h1 { color: red; }
μμ μ νμλ μ νμ μ¬μ΄μ λ«λ κΊ½μ κΈ°νΈ(>)λ₯Ό λ£λλ€. μ΄ μ νμλ <div>μ μμ μμμΈ <h1>μ μ ννλ μ νμλ€.
μΈμ νμ μ νμ
div + p { color: red; }
μΈμ νμ μ νμλ μ νμ μ¬μ΄μ +λ₯Ό λ£μ΄ νννλ€.
'TOOLS > Frontend' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
CSS λ¬Έλ² (3) : κ°μ ν΄λμ€/ μμ, μ νμ κ°μ κ΅¬μ²΄μ± μ μ (0) | 2024.03.14 |
---|---|
CSS λ¬Έλ² (1) : CSSμ μ μ, HTMLμμ μ°κ²° λ°©λ² (0) | 2024.03.10 |
HTML μμλ³ λ¬Έλ² νκ·Έ μ 리 (0) | 2024.03.10 |