(43) [CSS] 1μΌ CSS κΈ°μ΄
CSSλ?
μ€νμΌμ HTML λ¬Έμ λ΄μ μ체μ μ’ λ₯,ν¬κΈ°,μ, μ¬λ°±λ±μ
μ§μ νμ¬ μ¬μ©μμ web browser νκ²½μ μκ΄μμ΄ μΌμ ν νλ©΄μ 보μ¬μ£Όλ κΈ°λ₯.
CSS μ§μ λ°©λ²
- μΈλΌμΈ μ€νμΌ : inline Styleμ HTML tag μμ style μμ±μ μ¬μ©νμ¬ μ§μ μ§μ νλ€.
- λ΄λΆ μ€νμΌ μνΈ : μ€νμΌ μνΈμ κΈ°λ³Έμ μΈ μ¬μ© λ°©λ²μΌλ‘ htmlμ <head>~</head> μ¬μ΄μ μ½μ νμ¬ <style type="css/text" media="κ°">...<style>
- μΈλΆ μ€νμΌ μνΈ
- imported style sheet : μ΄λ°©μμ κ²°κ³Όμ μΌλ‘ linked styleμ κ°κ³ μμΉλ embedded λ°©μκ³Ό λ§μ°¬ κ°μ§λ‘ style block μμ λ€μ΄κ°λ€.
ν¬κΈ° λ¨μ
- px : ν½μ μ λλ°μ΄μ€ ν΄μλμ λ°λΌ μλμ μΈ ν¬κΈ°λ₯Ό κ°λλ€. λλ°μ΄μ€μ λ°λΌ ν½μ (νμ)μ ν¬κΈ°λ λ€λ₯΄κΈ° λλ¬Έμ ν½μ μ κΈ°μ€μΌλ‘ νλ λ¨μλ λͺ ννμ§ μλ€.
- % : λ°±λΆλ₯ λ¨μμ μλ λ¨μ. μμμ μ§μ λ μ¬μ΄μ¦(μμλ μ¬μ΄μ¦λ λν΄νΈ μ¬μ΄μ¦)μ μλμ μΈ μ¬μ΄μ¦λ₯Ό μ€μ νλ€.
- em : λ°°μ λ¨μ μλ λ¨μ. μμ μ§μ λ μ¬μ΄μ¦λ₯Ό μ€μ νλ€.
- rem : root em μ΄λΌλ λ»μΌλ‘, HTML λ¬Έμμ root μμμΈ <html>μ κ°λ¦¬ν€λ©°, μ΄ μμμ μ§μ λ ν¬κΈ°λ₯Ό κΈ°μ€μΌλ‘ μλμ μΈ κ°μ κ°μ§κ² λλ€.
- Viewport λ¨μ : λ°μν μΉμ νλ©΄μ ν¬κΈ°μ λμ μΌλ‘ λμνκΈ° μν΄ % λ¨μλ₯Ό μμ£Ό μ¬μ©νμ§λ§, % λ¨μλ emκ³Ό κ°μ΄ μμμ μν΄ λΆλͺ¨ μμμ μλμ μν₯μ λ°λλ€. Viemport λ¨μλ μλμ μΈ λ¨μλ‘ viewportλ₯Ό κΈ°μ€μΌλ‘ ν μλμ μ¬μ΄μ¦λ₯Ό μλ―Ένλ€.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.box{
width: 350px; height: 200px;
border: 3px dotted gray;
font-size: 14px;
font-weight: bold;
text-align: center;
padding: 2em;
/* 2em = 2 x κΈμν¬κΈ° = 2x14 = 28px*/
}
</style>
</head>
<body>
<h3> λ¨μ(Units) : px </h3>
<div class="box">κΈμν¬κΈ°: 14px</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
body{
font-size: 14px;
}
.box{
width: 350px; height: 200px;
border: 3px dotted gray;
font-size: 120%; /*λΆλͺ¨ν¬κΈ°x120%=14*1.2=16.8px*/
font-weight: bold;
text-align: center;
padding: 2em /*16.8px*2=33.6px*/;
}
</style>
</head>
<body>
<h3> λ¨μ(Units) : % </h3>
<div class="box">κΈμν¬κΈ°: 14px * 120% = 16.8px</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
body{
font-size: 14px;
}
.box{
width: 350px; height: 200px;
border: 3px dotted gray;
font-size: 1.2em; /*λΆλͺ¨ν¬κΈ°x1.2=14*1.2=16.8px*/
font-weight: bold;
text-align: center;
padding: 2em
/*κΈμν¬κΈ°*2=16.8x2=33.6px ν¨λ©μ κΈμν¬κΈ°λ₯Ό λ°λΌκ°κ³ , κΈμλ λΆλͺ¨ν¬κΈ°λ₯Ό λ°λΌκ°*/;
}
</style>
</head>
<body>
<h3> λ¨μ(Units) : em </h3>
<div class="box">κΈμν¬κΈ°: 1.2em = 14px * 1.2 = 16.8px</div>
</body>
</html>
νλ‘νΌν°
- inherit : μΈν€λ¦Ώ ν€μλλ₯Ό μ¬μ©ν μμ±μ λΆλͺ¨ μμλ‘λΆν° ν΄λΉ μμ±μ κ³μ° κ°μ μμ λ°μ μ¬μ©νλ€. all λ¨μΆ μμ±μ ν¬ν¨ν λͺ¨λ μμ±μ μ¬μ©ν μ μλ€.
- initial : μμ±μ μ΄κΈ°κ°(κΈ°λ³Έκ°)μ μμμ μ μ©νλ€. μ΄κΈ°κ°μ λΈλΌμ°μ κ° μ§μ νλ€. λͺ¨λ μμ±μμ μ¬μ©ν μ μμΌλ©°, allμ μ§μ ν κ²½μ° λͺ¨λ CSS μμ±μ μ΄κΈ°κ°μΌλ‘ μ¬μ€μ νλ€.
- uri : uriλ url (μ£Όμ)+urm (μμμ΄λ¦)λ‘, μΉ μμμ λ¬Έμμ λ°°κ²½ κ·Έλ¦Όμ΄λ μμκ° μλ listμμ iconμ λ£μλ urlμ΄λ uriλ₯Ό κΈ°μ νλ€.
- !important : λͺ¨λ style κ·μΉμ μ°μ νλ©° inline style,id selector,class selectorλ€λ‘ κ°μ style μμ±μ μ μΈνλλΌλ κ·Έ κ°μ λͺ¨λ 무μνλ©° !import μ μΈν μμ± κ°μ μ μ©μν¨λ€.
μμ
μμ(λΆλͺ¨,μ‘°μ)μμμ μ μ©λ νλ‘νΌν°λ₯Ό νμ(μμ,μμ)μμκ° λ¬Όλ € λ°λ κ²μ μλ―Ένλ€. νλ‘νΌν°μλ μμμ΄ λλ κ²κ³Ό λμ§ μλ κ²μ΄ μλ€.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.text-red{
color: red;
border: 1px solid #bcbcbc;
padding: 10px;
}
</style>
</head>
<body>
<h3> μμ(Inheritance) </h3>
<div class="text-red">
<h1>μμ μ</h1>
<p>λ¬Έλ¨ <span>span</span></p>
<button>λ²νΌ 1</button>
<button style="color: inherit;">λ²νΌ 2</button> <!-- μΈν€λ¦Ώ ν€μλλ‘ λΆλͺ¨μμ± μμ -->
</div>
<hr>
<div>
<p> colorλ νμ μμμ μμλλ μμ±μ΄μ§λ§ button νκ·Έ μ²λΌ μμμ λ°λΌ μμ λ°μ§ μλ κ²½μ°λ μ‘΄μ¬ </p>
<p>
<b>μμλλ μμ±</b>
<br>visibility, opacity, font, color, line-height, text-align, white-space λ±
</p>
<hr>
<p>
<b>μμλλ μλ μμ±</b>
<br>width, height, margin, padding, border, box-sizing, display, background, vertical-align,
text-decoration, position, top/right/bottom/left, z-index,
overflow, float λ±
</p>
</div>
</body>
</html>
μΊμ€μΊμ΄λ©
μμλ νλ μ΄μμ CSSμ μΈμ μν₯μ λ°μ μ μλ€. μ΄λ μΆ©λμ νΌνκΈ° μν΄ CSSμ μ© μ°μ μμκ° νμνλ° μ΄λ₯Ό μΊμ€μΊμ΄λ©μ΄λΌκ³ νλ€. μΊμ€μΊμ΄λ© μΈκ°μ§ κ·μΉ μ€μλ(CSSκ° μ΄λμ μ μΈ λμ΄μλμ§μ λ°λΌμ μ°μ μμκ° λ¬λΌμ§λ€.) λͺ μλ(λμμ λͺ ννκ² νΉμ ν μλ‘ λͺ μλκ° λμμ§κ³ μ°μ μμκ° λμμ§λ€.) μ μΈμμ (μ μΈλ μμμ λ°λΌ μ°μ μμκ° μ μ©λλ€. μ¦, λμ€μ μ μΈλ μ€νμΌμ΄ μ°μ μ μ©λλ€).
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="style3.css">
<style type="text/css">
div{
background: navy;
}
</style>
</head>
<body>
<h3> Cascading Order </h3>
<div>
CSSκ° μ΄λμ μ μΈ λμλμ§μ λ°λΌμ μ°μ μμκ° λ¬λΌμ§λ€.
</div>
<p>
1) embedded style sheet : style μμ<br>
2) embedded style sheet λ΄μ @import λ¬Έ<br>
3) linked style sheet : link νκ·Έλ‘ μ°κ²°λ CSS νμΌ<br>
4) linked style sheet λ΄μ @import λ¬Έ<br>
5) λΈλΌμ°μ λν΄νΈ μ€νμΌμνΈ<br>
</p>
</body>
</html>
μ νμ(Select)λ?
μ νμλ styleμ μ μ©νκ³ μ νλ HTML μμλ‘, μνλ μμλ₯Ό μ ννλλ°
μ¬μ©λλ ν¨ν΄μ΄λ€. μ¦, styleμ μ μ©νκ³ μ νλ HTML μμλ₯Ό
μ λ ν°λ‘ νΉμ νκ³ μ νλ μμμ μ€νμΌμ μ μνλ€.ꡬ문νμ
Selector
- μ 체 μ λ ν°(*) : HTML λ¬Έμ λ΄μ λͺ¨λ μμλ₯Ό μ ννλ€. html μμλ₯Ό ν¬ν¨ν λͺ¨λ μμκ° μ νλλ€.
- νκ·Έ μ λ ν° : μ§μ λ νκ·Έλͺ μ κ°μ§λ μμλ₯Ό μ ννλ€.
- ID μ λ ν° : id μ΄νΈλ¦¬λ·°νΈ κ°μ μ§μ νμ¬ μΌμΉνλ μμλ₯Ό μ ννλ€. id μ΄νΈλ¦¬λ·°νΈ κ°μ μ€λ³΅λ μ μλ μ μΌν κ°μ΄λ€.
- ν΄λμ€ μ λ ν° : class μ΄νΈλ¦¬λ·°νΈ κ°μ μ§μ νμ¬ μΌμΉνλ μμλ₯Ό μ ννλ€.
- μ΄νΈλ¦¬λ·°νΈ μ λ ν° : μ£Όμ΄μ§ μ΄νΈλ¦¬λ·°νΈμ μ‘΄μ¬μ¬λΆλ κ·Έ κ°μ λ°λΌ μμλ₯Ό μ ννλ€.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
/*λͺ¨λ μμ*/
*{
font-size: 15px;
font-family: "λ§μ κ³ λ",λμ,sans-serif;
}
/*νκ·Έ μ νμ*/
p{background: yellow;}
label {color: blue;font-weight: bold;}
/*id μ νμ*/
#box{
width: 300px;
padding: 10px;
border: 3px dashed gray;
}
/*ν΄λμ€ μ νμ*/
.red{color: red;}
.underline{text-decoration: underline;}
</style>
</head>
<body>
<h3>Selector : κΈ°λ³Έ μ
λ ν°</h3>
<div>
<div id="box">
<label>νλ‘κ·Έλλ°</label>
<span class="red">μλ°</span>
<span>CμΈμ΄</span>
<span class="red underline">μ½νλ¦°</span>
<p>
<label>λ°μ΄ν°λ² μ΄μ€</label>
<span class="red">μ€λΌν΄</span>
<span class="underline">mysql</span>
<span>λΉ
λ°μ΄ν°</span>
</p>
</div>
<div>
<label>μΉ</label>
<span>HTML</span>
<span>CSS</span>
<span>javascript</span>
<p>
<label>μΉνλ‘κ·Έλ¨</label>
<span>JSP</span>
<span>PHP</span>
<span>ASP.NET</span>
</p>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
.box{
color: tomato;
}
div.box{
width: 300px; height: 50px; border: 3px dotted gray;
}
/*OR*/
lable,span{color: blue; font-weight: bold;}
</style>
</head>
<body>
<h3>λ€μ€ 쑰건 μ νμ</h3>
<p class="box">
AND μ°μ° μ νμ :λͺ¨λ 쑰건μ λ§μ‘±νλ μμ μ ν. μ νμλΌλ¦¬ λμ΄μ°κΈ° μμ΄ λΆμ¬ μ΄λ€.
</p>
<br>
<div class="box"></div>
<br>
<div><p class="box">μμ </p></div>
<hr>
<p>
μ νμ(Selector) λͺ©λ‘(OR μ°μ°) : μ νμ λͺ©λ‘(,)μ μΌμΉνλ λͺ¨λ μμλ₯Ό μ ν
</p>
<div>
<p>κ³Όλͺ©</p>
<label>μΉ</label>
<span>HTML</span>
<span>CSS</span>
<span>μλ°μ€ν¬λ¦½νΈ</span>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
p{height: 20px;}
/* name=subject μΈ μμ */
input[name=subject]{
background: yellow;
}
/* name μμ±μ΄ μ‘΄μ¬νλ μμ */
input[name]{
border: 1px solid blue;
}
/*nameμ΄ aλ‘ μμνλ μμ*/
input[name^=a]{border-right:3px solid red; }
/*titleμ΄ λ²νΌμΌλ‘ λλλ μμ, "λ²νΌ"μ²λΌ ν΄λ κ°λ₯*/
input[title$=λ²νΌ]{background: green;}
/*nameμ΄ xκ° ν¬ν¨λ μμ*/
input[name*=x]{border-left:3px solid orange; }
/*titleμ λ΄μ©μ΄λΌλ λ¨μ΄(λμ΄μ°κΈ°λ‘ꡬλΆ)κ° μ‘΄μ¬νλ μμ*/
input[title~=λ΄μ©]{background: gray;}
/* pμμμ€ langκ° en μ΄κ±°λ en-λ‘ μμνλ μμ */
p[lang|=en]{
color: red;
}
</style>
</head>
<body>
<h3>μ΄νΈλ¦¬λ·°νΈ μ
λ ν°(Attribute Selector) : μ£Όμ΄μ§ μ΄νΈλ¦¬λ·°νΈμ μ‘΄μ¬μ¬λΆλ κ·Έ κ°μ λ°λΌ μμλ₯Ό μ ν</h3>
<form>
<p> <input type="text" name="subject" title="κ³Όλͺ© μ
λ ₯"> </p>
<p> <input type="text" name="ax1" title="1μ₯ μ λͺ© μ
λ ₯"> </p>
<p> <input type="text" name="as2" title="1μ₯ λ΄μ© μ
λ ₯"> </p>
<p> <input type="text" name="bs1" title="2μ₯ μ λͺ© μ
λ ₯"> </p>
<p> <input type="text" name="bx2" title="2μ₯ λ΄μ© μ
λ ₯"> </p>
<p> <input type="file" name="selectFile"> </p>
<p>
<input type="button" value="μ
λ ₯μλ£" title="μ
λ ₯ λ²νΌ">
<input type="reset" value="μ
λ ₯μ·¨μ" title="μ·¨μ λ²νΌ">
</p>
</form>
<div style="margin-top: 30px;">
<p lang="en">JAVA</p>
<p lang="en-us">JAVA</p>
<p lang="en-gb">JAVA</p>
<p lang="us">JAVA</p>
<p lang="no">JAVA</p>
</div>
</body>
</html>
κ²°ν©μ
- μΈμ νμ κ²°ν©μ ; μΈμ νμ κ²°ν©μ(+)λ μμμ μ§μ ν μμμ λ°λ‘ λ€μμ μμΉνλ νμ μμλ§ μ ννλ€.
- μΌλ° νμ κ²°ν©μ : μΌλ° νμ κ²°ν©μ(~)λ λκ°μ μ νμ μ¬μ΄μ μμΉνμ¬ λ€μͺ½ μ νμμ μμμ μμͺ½ μ νμ μμμ λΆλͺ¨ μμκ° κ°κ³ , λ€μͺ½ μ νμμ μμκ° λ€μ μμΉν λ μ ννλ€.
- μμ κ²°ν©μ : μμ κ²°ν©μ(>)λ λ κ°μ CSS μ νμ μ¬μ΄μ μμΉνμ¬ λ€μͺ½ μ νμμ μμκ° μμͺ½ μ νμ μμμ λ°λ‘ λ°μ μμΉν κ²½μ°μλ§ μ ννλ€.
- μμ(νμ)κ²°ν©μ : λ³΄ν΅ ν μΉΈμ 곡백 λ¬Έμλ‘ νννλ μμ κ²°ν©μ()λ λκ°μ μ νμλ₯Ό μ‘°ν©νμ¬, λ€μͺ½ μ νμ μμμ μ‘°μμ μμͺ½ μ νμ μμκ° μ‘΄μ¬ν κ²½μ° μ ννλ€.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
/*μΈμ νμ κ²°ν©μ(+)*/
label+span{
background: yellow;
}
/*μΌλ° νμ κ²°ν©μ(~)*/
label~span{
color: blue; font-weight: bold;
}
/*μμ κ²°ν©μ(>)*/
.subject>lable{
color: tomato;
font-weight: bold;
}
/*μμ κ²°ν©μ(곡백)*/
.subject lable{
background: #ccc;
}
</style>
</head>
<body>
<h3>Selector : κ²°ν©μ(Combinator)</h3>
<div class="box">
<div class="subject">
<label>νλ‘κ·Έλλ°</label>
<span>μλ°</span>
<span>CμΈμ΄</span>
<span>μ½νλ¦°</span>
<p>
<label>λ°μ΄ν°λ² μ΄μ€</label>
<span>μ€λΌν΄</span>
<span>mysql</span>
<span>λΉ
λ°μ΄ν°</span>
</p>
</div>
<div class="subject">
<label>μΉ</label>
<span>HTML</span>
<span>CSS</span>
<span>javascript</span>
<p>
<label>μΉνλ‘κ·Έλ¨</label>
<span>JSP</span>
<span>PHP</span>
<span>ASP.NET</span>
</p>
</div>
</div>
<div>
<span>End...</span>
</div>
</body>
</html>
κ°μ ν΄λμ€
μ νμμ μΆκ°νλ ν€μλλ‘, μ νν μμκ° νΉλ³ν μνμ¬μ¬μΌ λ§μ‘±ν μ μλ€. ex) λ§μ°μ€κ° μ¬λΌμ μμ λμλ§ κΈμ¨ μμ λ°κΎΈκ³ μΆμλ. λ§ν¬λ₯Ό λ°©λ¬Ένμ λμ μμ§ λ°©λ¬Ένμ§ μμμλ
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
/*λ°©λ¬Ένμ§ μμ λ§ν¬μΌ λ*/
a:link{color: orange;}
/*λ°©λ¬Έν λ§ν¬μΌ λ */
a:visited {color: green;}
/*λ§μ°μ€λ₯Ό μ¬λ €λμ λ*/
a:hover {color: maroon;}
/*ν΄λ¦λ μν*/
a:active {color: blue;}
/*ν¬μ»€μ€λ₯Ό λ°μλ*/
input[type=text]:focus,input[type=password]:focus{
outline: none;
border: 1px solid red;
}
</style>
</head>
<body>
<h3>κ°μ ν΄λμ€(Pseudo-classes) : λ§ν¬ μ
λ ν°(Link pseudo-classes) </h3>
<div>
<a href="#">λ§ν¬</a> |
<a href="https://google.com">ꡬκΈ</a>
</div>
<div>
<p> <input type="text"> </p>
<p> <input type="password"> </p>
<p> <input type="button" value="νμΈ"> </p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
input:enabled+span{
color: blue;
}
input:disabled{color: tomato;}
input:checked+span{color: red;}
</style>
</head>
<body>
<h3>κ°μ ν΄λμ€(Pseudo-classes) : UI μμ μν μ
λ ν° </h3>
<div>
<p> <input type="text" value="default"> <span>default</span> </p>
<p> <input type="text" readonly value="readonly"> <span>readonly</span> </p>
<p> <input type="text" disabled value="disabled"> <span>disabled</span></p>
<p> <input type="checkbox"> <span>μ ν1</span> </p>
<p> <input type="checkbox" checked> <span>μ ν2</span> </p>
<p> <input type="button" value="νμΈ"> </p>
</div>
</body>
</html>