๋ด์ฉ ์์ฝ
- CSS ๋ฐ์ค ๋ชจ๋ธ
- content : ํ ์คํธ๋ ์ด๋ฏธ์ง๊ฐ ๋ค์ด๊ฐ๋ ์ค์ ๋ด์ฉ ์์ญ
- padding : ๋ด์ฉ๊ณผ ํ ๋๋ฆฌ ์ฌ์ด ๋ด๋ถ ์ฌ๋ฐฑ
- border : ํ ๋๋ฆฌ, ์์์ ๊ฒฝ๊ณ
- margin : ์์ ์ธ๋ถ์ ์ฌ๋ฐฑ, ๋ค๋ฅธ ์์์์ ๊ฐ๊ฒฉ
<div class="container">๋ฐ์ค ๋ชจ๋ธ</div>
.container {
width : 200px;
padding : 20px;
border : 5px solid black;
margin : 15px;
}
- CSS ์ฐ์ ์์
!important(์ฐ์ ์ ์ฉ) > style ์์ฑ (์ธ๋ผ์ธ ์คํ์ผ) > id ์ ํ์ > class ์ ํ์ > ํ๊ทธ ์ ํ์
๋ค๋ง !important๋ ๊ฐ๋ ์ฑ์ด๋ ์ฑ๋ฅ, ์ ์ง๋ณด์ ์ธก๋ฉด์์ ์ฌ๋ฌ๋ชจ๋ก ์ข์ง ์๊ธฐ ๋๋ฌธ์ ๋๋๋ก ์ฌ์ฉํ์ง ์๋ ๊ฒ์ด ์ข๋ค.
<p id="unique" class="inline-style highlight">CSS ์ฐ์ ์์</p>
p {
color: blue; /* ํ๊ทธ ์ ํ์ */
}
.highlight {
color: red; /* ํด๋์ค ์ ํ์ */
}
#unique {
color: green; /* ID ์ ํ์ */
}
p.inline-style {
color: yellow !important; /* !important */
}
- css ๋จ์
๊ณ ์ ๋จ์
- px (ํฝ์ ) - ํ๋ฉด์ ๊ณ ์ ๋ ํฌ๊ธฐ, ๋๋ฐ์ด์ค ํ๋ฉด ํฌ๊ธฐ์ ๊ด๊ณ์์ด ์ผ์ ํ๊ฒ ์ ์ง
์๋ ๋จ์
- em, rem : ๊ธ๊ผด ํฌ๊ธฐ์ ์๋์ ์ธ ๋จ์
- em : ๋ถ๋ชจ ์์์ ๊ธ๊ผด ํฌ๊ธฐ์ ๋น๋กํจ (1em : ๋ถ๋ชจ ๊ธ๊ผด ํฌ๊ธฐ)
- rem : ๋ฃจํธ ์์(html)์ ๊ธ๊ผด ํฌ๊ธฐ์ ๋น๋กํจ
- % : ๋ถ๋ชจ ์์ ํฌ๊ธฐ์ ์๋์ ์ธ ๋ฐฑ๋ถ์จ
- vw, vh : ๊ฐ๊ฐ ๋ทฐํฌํธ(ํ๋ฉด) ๋๋น, ๋์ด์ ๋น๋กํ๋ ๋จ์
์ค์ต ๊ณผ์
<style>
/* 1. ๋ฐ์ค ํจ๋ฉ, ํ
๋๋ฆฌ, ๋ง์ง ์ค์ */
.box {
padding : 10px;
border : 3px solid blue;
margin : 20px;
}
/* 2. ์ ๋ชฉ์ ๊ธฐ๋ณธ ์์์ ๋
น์ */
h1 {
color : green;
}
/* 3. ์ ๋ชฉ์ ํด๋์ค .highlight๊ฐ ์์ ๋๋ ๋นจ๊ฐ์ */
.highlight {
color : red;
}
/* 4. id๊ฐ special์ธ ๊ฒฝ์ฐ ๋
ธ๋์ */
#special {
color : yellow;
}
/* 5. ๋จ์๋ฅผ ์ ์ ํ ์ฌ์ฉํด ํฌ๊ธฐ ๋ฐ ๊ธ์ ํฌ๊ธฐ๋ฅผ ์ค์ ํ์ธ์ */
.content {
width: 50%; /* 50%๋ก ๋ฐ์ค ํฌ๊ธฐ๋ฅผ ์ค์ */
font-size: 1.2em; /* ๊ธ์ ํฌ๊ธฐ ์ค์ */
}
</style>
</head>
<body>
<h1 class="highlight">๋ฐ์ค ๋ชจ๋ธ ์ค์ต</h1>
<h1 id="special">์ฐ์ ์์ ์ค์ต</h1>
<div class="box content">
์ด๊ณณ์ ๋ฐ์ค ๋ชจ๋ธ์ด ์ ์ฉ๋ ์ฝํ
์ธ ์
๋๋ค.
</div>
width : 100%์ผ ๋๋ ๋ฐ์ค ํฌ๊ธฐ๋ก๋ง ๋ฐ์ง๋ฉด ๋ธ๋ผ์ฐ์ ์ ๋๋น์ 100%์ด์ง๋ง, ๊ฐ๋ก ์คํฌ๋กค์ด ์๊ธฐ๋ ๊ฒ์ ๋ณผ ์๊ฐ ์๋ค.
์ด๋ ๋ฐ์ค ๋ชจ๋ธ์ padding๊ฐ๊ณผ margin ๊ฐ์ด ์ ์ฉ๋์ด์์ธ๋ฐ box-sizing ์์ฑ์ผ๋ก ํด๊ฒฐํ ์ ์๋ค.
box-sizing : ์์์ ๋๋น์ ๋์ด๋ฅผ ๊ณ์ฐํ๋ ๋ฐฉ๋ฒ์ ์ง์ ํ๋ ์์ฑ
box-sizing์ ๊ฐ
- content-box(์ด๊ธฐ๊ฐ) : weight, height ์์ฑ์ด ์ฝํ ์ธ ์์ญ๋ง ๊ณ์ฐ(padding๊ณผ border ์ ์ธ)
- border-box : weight, height๋ฟ๋ง์ด ์๋ padding, border ๊ฐ๋ ์์์ ํฌ๊ธฐ๋ก ๊ณ์ฐํจ.
<style>
.box {
padding: 10px;
border: 3px solid blue;
margin: 20px;
}
.content {
width: 100%;
font-size: 1.2em;
}
.cont-box {
box-sizing: content-box;
}
.bord-box {
box-sizing: border-box;
}
</style>
<div class="box content cont-box">
content-box
</div>
<div class="box content bord-box">
border-box
</div>
๊ทธ๋๋ ๊ฐ๋ก ์คํฌ๋กค์ด ์ฌ์ ํ ๋จ์์๋๋ฐ, ์ด๋ border-box๊ฐ๋ margin ๊ฐ์ ์ ์ธํ๊ธฐ ๋๋ฌธ์ด๋ค.
.box ์ ์ง์ ํ๋ margin ๊ฐ์ ์์ ์ฃผ๋ฉด
border-box๋ ๊ฐ๋ก ์คํฌ๋กค์ ๋๊ธฐ์ง ์์๋ ํ์ด์ง์ ๋ฑ ๋ง๊ฒ ํ์๋๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
(๊ฐ๋ก ์คํฌ๋กค์ ์์ ๊ณ ์ถ๋ค๋ฉด ๋๋ฒ์งธ ์ด๋ฏธ์ง์ ๊ฐ์ด content-box๋ฅผ border-box๋ก ์์ ํด์ฃผ๋ฉด ๋๋ค.)
'์ค์ฆ์ฝ๋ฉ์ค์ฟจ ํ๋ก ํธ์๋ 10๊ธฐ > ๋ฒ ์ด์ค์บ ํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋น๊ธฐ๋ ํธ๋ 10 - JavaScript ์ด๋ก (0) | 2025.02.22 |
---|---|
์๋ฐ์ ์นผ๋ผ 3 - ์ํ ๋ฏน ํด๋น (0) | 2025.02.20 |
๋น๊ธฐ๋ ํธ๋ 8 - CSS ์ด๋ก (1) (0) | 2025.02.16 |
๋น๊ธฐ๋ ํธ๋ 7 - HTML ์ด๋ก (0) | 2025.02.15 |
๋น๊ธฐ๋ ํธ๋ 6 - Git๊ณผ GitHub (0) | 2025.02.14 |