[2023.03.03] 8. CSS Box Model
2023. 3. 3. 20:56ใDev./CSS
๐CSS Box Model์ ๋ํด ์ ๋ฆฌ!
CSS Box Model
- HTML ์์๋ฅผ ๊ฐ์ธ๋ ์์
- ์์, ํจ๋ฉ, ํ ๋๋ฆฌ, ๋ง์ง์ผ๋ก ๊ตฌ์ฑ๋จ
- ์์: ํ ์คํธ, ์ฌ์ง ๋ฑ ๋ณด์ฌ์ค ๋์
- ํจ๋ฉ: ์์ ์ฃผ๋ณ ์์ญ์ ๊ฐ์ผ๋ค.
- ํ ๋๋ฆฌ: ์์์ ํจ๋ฉ์ ๊ฐ์ธ๋ ํ ๋๋ฆฌ
- ๋ง์ง: ํ ๋๋ฆฌ ๋ฐ์ ์์ญ์ ๊ฐ์ผ๋ค.
- CSS Box Model์ ๋ธ๋ก ๋ฐ์ค์ ์ ์ฉ๋๋ฉฐ, ์ธ๋ผ์ธ ๋ฐ์ค๋ ์ผ๋ถ ๋์๋ง ์ฌ์ฉํ๋ค.
inline ์์๋ width, height, ์ํ margin ๊ฐ ์ ์ฉ X
width
- ์์์ ๋๋น
- ๊ธฐ๋ณธ๊ฐ์ ์ฝํ
์ธ ์์ญ์ ๋๋น์ง๋ง box-sizing ์์ฑ์ ์ฌ์ฉํ์ฌ ํ
๋๋ฆฌ ์์ญ์ ๋๋น๋ฅผ ์ค์
- auto: ๊ธฐ๋ณธ๊ฐ. ๋ธ๋ผ์ฐ์ ๊ฐ ๊ณ์ฐํ์ฌ ์ง์
- min-content: ์ต์ ๋๋น
- max-content: ์ปจํ ์ธ ๋ด์ฉ์ ์ ํธ ๋๋น
- fit-content: ๊ฐ๋ฅํ ๊ณต๊ฐ์ ์ต๋ํ ์ฌ์ฉํ์ง๋ง max-content ๋ณด๋ค๋ ์์ ๋๋น
height
- ์์์ ๋์ด
- auto: ๊ธฐ๋ณธ๊ฐ. ๋ธ๋ผ์ฐ์ ๊ฐ ๊ณ์ฐํ์ฌ ์ง์ (์์์ ์์ ๊ธฐ์ค์ผ๋ก ์กฐ์ )
padding
- ๋จ์ถ ์์ฑ
- padding-top padding-right padding-bottom padding-left ์์ผ๋ก ์์ฑ (์๊ณ ๋ฐฉํฅ)
p {
padding: 10px; /* top, right, bottom, left ๋ชจ๋ 10px */
padding: 10px 20px; /* top, bottom :10px, left, right:20px */
padding: 10px 20px 30px; /* top:10px, left,right:20px, bottom:30px */
padding: 10px 20px 30px 40px;
}
margin
- ๋จ์ถ ์์ฑ
- margin-top margin-right margin-bottom margin-left ์์ผ๋ก ์์ฑ (์๊ณ ๋ฐฉํฅ)
/* ๊ฐ์ด๋ฐ ๋ฐฐ์น */
p{
width: 400px;
margin: auto;
}
/* ์ค๋ฅธ์ชฝ ๋ฐฐ์น */
p{
width: 400px;
margin: auto 0 auto auto;
}
- margin auto๋ ์ํ ์ ๋ ฌ์ ํ ์ ์์ง๋ง ์์ง ์ ๋ ฌ์ ์ ์ฉํ ์๋ ์๋ค.
๐ค๋ง์ง๋ณํฉ ํ์ (Margin Collapsing)


๐ก๋ง์ง๋ณํฉ ํ์ ํด๊ฒฐ๋ฐฉ๋ฒ
- ๋ถ๋ชจ ์์์ overflow ์์ฑ ๊ฐ์ ์ ์ฉ.
- ๋ถ๋ชจ ์์์ display: inline-block ๊ฐ์ ์ ์ฉ.
- ๋ถ๋ชจ ์์์ border ๊ฐ์ ์ ์ฉ.
- ๋ถ๋ชจ ์์์ display:flow-root์ ์ฌ์ฉ. (IE ์ง์ ๋ถ๊ฐ)
๋ง์ง๋ณํฉ ํ์์ ๊ตณ์ด ํด๊ฒฐํ ํ์๋ ์๋ค! ์ด ํ์์ ์๊ณ ์๋ ์ํ๋ก ์์ ์ ์งํํ๋ฉด ๋๋ค.
border
- ํ ๋๋ฆฌ ์ง์
- ํ ๋๋ฆฌ๋ ์์๊ฐ ์ฐจ์งํ๋ ์ ์ฒด ๋๋น, ๋์ด์ ์ผ๋ถ
- ๋จ์ถ์์ฑ
- ์ ์ ๋๊ป, ์คํ์ผ, ์์์ ์ง์
- border-width border-style border-color
๐งทborder-style
border-style - CSS: Cascading Style Sheets | MDN
border-style CSS ๋จ์ถ ์์ฑ์ ์์ ํ ๋๋ฆฌ ๋ค ๋ฉด์ ์คํ์ผ์ ์ง์ ํฉ๋๋ค.
developer.mozilla.org
๐งทborder-image
border-image - CSS: Cascading Style Sheets | MDN
The border-image CSS property draws an image around a given element. It replaces the element's regular border.
developer.mozilla.org
border-radius
- ์์์ ๋ชจ์๋ฆฌ๋ฅผ ๋ฅ๊ธ๊ฒ ์ฒ๋ฆฌ
- ๋จ์ถ ์์ฑ
- boder-top-left-radius border-top-right-radius border-bottom-right-radius border-bottom-left-radius
์์๋ก ์์ฑ(์๊ณ ๋ฐฉํฅ)
See the Pen Untitled by astinaus (@astinaus) on CodePen.
box-sizing
- content-box: ๊ธฐ๋ณธ๊ฐ. width, height์ border, padding ํฌํจ X
- border-box: width, height์ border, padding ํฌํจ.
- width = ์ฝํ ์ธ ๋๋น + border + padding
overflow, overflow-x, overflow-y
- ๋ฐ์ค๋ณด๋ค ์ฝํ
์ธ ๊ฐ ๋ ์ปค์ ์ฝํ
์ธ ๊ฐ ๋์น ๊ฒฝ์ฐ ์ฒ๋ฆฌ๋ฐฉ๋ฒ ์ง์
- visible: ๊ธฐ๋ณธ๊ฐ. ๋ฐ์ค๋ฅผ ๋๋ ์ฝํ ์ธ ๋ฅผ ๊ทธ๋๋ก ์ถ๋ ฅ
- hidden: ์์์ ํฌ๊ธฐ๋งํผ ๋ง์ถ๊ธฐ ์ํด ์๋ผ๋. ์คํฌ๋กค๋ฐ X
- scroll: ์์์ ํฌ๊ธฐ๋งํผ ์๋ผ๋ด๊ณ ์คํฌ๋กค๋ฐ O
- auto: ์๋์ผ๋ก ์ฝํ ์ธ ๊ฐ ๋์น ๊ฒฝ์ฐ ์คํฌ๋กค๋ฐ ๋ ธ์ถ
background
- ๋จ์ถ ์์ฑ
- background-attachment
- fixed: ๋ทฐํฌํธ์ ๊ณ ์
- local: ์์ ์ฝํ ์ธ ์ ๋ํด ๊ณ ์ . ์คํฌ๋กค์ด ์กด์ฌํ๋ฉด ๋ฐฐ๊ฒฝ์ ์ฝํ ์ธ ์ ํจ๊ป ์คํฌ๋กค.
- scroll: ๋ฐฐ๊ฒฝ ์์ ์์ฒด์ ๋ํด ๊ณ ์ . ์์์ ์คํฌ๋กค์ด ์กด์ฌํด๋ ๋ฐฐ๊ฒฝ์ ํจ๊ป ์คํฌ๋กค ๋์ง ์์.
- background-clip: ์์์ ๋ฐฐ๊ฒฝ์ด ํ
๋๋ฆฌ, ์์ชฝ ์ฌ๋ฐฑ, ์ฝํ
์ธ ์์ ์ค ์ด๋๊น์ง ์ฐจ์งํ ์ง ์ง์
- border-box
- padding-box
- content-box
- background-color: ๋ฐฐ๊ฒฝ ์์ ์ค์
- background-image: ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ์ค์
- background-origin: ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง ๋ฐฐ์น ๊ธฐ์ค ์กฐ์
- background-position: ๋ฐฐ๊ฒฝ์ ์์น๋ฅผ ์ค์
- background-repeat: ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ์ด๋ป๊ฒ ๋ฐ๋ณตํ ๊ฒ์ธ์ง ์ค์ .
- repeat: ๋ฐ๋ณต
- no-repeat : ๋ฐ๋ณตํ์ง ์์
- repeat-x : x์ถ์ผ๋ก๋ง ๋ฐ๋ณต
- repeat-y: y์ถ์ผ๋ก๋ง ๋ฐ๋ณต
- background-size
- contain : ์ด๋ฏธ์ง๊ฐ ์๋ฆฌ๊ฑฐ๋ ์ฐ๊ทธ๋ฌ์ง์ง ์๋ ํ๋ ๋ด์์ ์ ์ผ ํฌ๊ฒ ์ค์ . ์ฌ๋ฐฑ์ด ๋ฐ์ํ ์ ์๋ค.
- cover : ์ด๋ฏธ์ง๊ฐ ์ฐ๊ทธ๋ฌ์ง์ง ์๋ ํ๋ ๋ด์์ ์ ์ผ ํฌ๊ฒ ์ค์ . ์ด๋ฏธ์ง์ ๊ฐ๋ก์ธ๋ก๋น๊ฐ ์์์ ๋ค๋ฅด๋ค๋ฉด ์ด๋ฏธ์ง๋ฅผ ์ธ๋ก ๋ฐฉํฅ ๋๋ ๊ฐ๋ก ๋ฐฉํฅ์ผ๋ก ์๋ผ๋ด์ด ์ฌ๋ฐฑ์ด ์๊ธฐ์ง ์๋๋ก ์ค์ ํฉ๋๋ค.
box-shadow
- ๊ทธ๋ฆผ์ ํจ๊ณผ๋ฅผ ์ถ๊ฐ
- offset-x offset-y blur-radius spread-radius color ์์๋ก ์์ฑ
opacity
- ๋ถํฌ๋ช ๋๋ฅผ ์ค์
- 0 - 1 ์ฌ์ด์ ์ซ์๋ฅผ ์ง์ ํ ์ ์๋ค. (0: ํฌ๋ช , 1: ๋ถํฌ๋ช )
'Dev. > CSS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[2023.03.08] 10. display: float (0) | 2023.03.08 |
---|---|
[2023.03.06] 9. display ์์ฑ / position / reset.css (0) | 2023.03.06 |
[2023.03.03] 7. CSS ์ ํ์(๊ฐ์ ํด๋์ค / ๊ฐ์ ์์) (0) | 2023.03.03 |
[2023.03.03] 6. CSS ํ ์คํธ ์์ฑ (0) | 2023.03.03 |
[2023.03.02] 5. CSS ๋จ์ ์ ๋ฆฌ (0) | 2023.03.02 |