7.1 float ์์ฑ
float
์ clear
์์ฑ์ ์ฌ์ฉํ ๋ ๋ค๋๋ ์์๋ค์ ์น ๋์์ธ์์ ๋ณต์กํ ๋ ์ด์์์ ๋ง๋๋ ์ด๊ธฐ ๋ฐฉ๋ฒ ์ค ํ๋์์ด.
์์ฆ์ Flexbox๋ Grid ๊ฐ์ ํ๋์ ์ธ ๋ฐฉ๋ฒ๋ค์ด ๋ ๋ง์ด ์ฐ์ด์ง๋ง, float
์ clear
์ ์ฌ์ฉ๋ฒ์ ์๋ ๊ฑด ์ฌ์ ํ ์ค์ํด.
float ์์ฑ
float
์์ฑ์ ์์๋ฅผ ์ผ์ชฝ์ด๋ ์ค๋ฅธ์ชฝ์ผ๋ก "๋ ๋ค๋๊ฒ" ํ๊ณ , ํ
์คํธ๋ ๋ค๋ฅธ ์์๋ค์ด ๊ทธ๊ฒ์ ๊ฐ์ธ๋๋ก ํด.
์ปฌ๋ผ ๋ ์ด์์์ด๋ ์ด๋ฏธ์ง ์ฃผ๋ณ์ ํ
์คํธ๋ฅผ ๋ฐฐ์นํ ๋ ํนํ ์ ์ฉํ์ง.
๋ฌธ๋ฒ:
.element {
float: left; /* ๋๋ right */
}
float ์์ฑ์ ๊ฐ:
left
: ์์๊ฐ ์ผ์ชฝ์ผ๋ก ๋ ๋ค๋๊ณ , ๋ค๋ฐ๋ฅด๋ ์์๋ค์ด ์ค๋ฅธ์ชฝ์์ ๊ฐ์ธ์ค๋คright
: ์์๊ฐ ์ค๋ฅธ์ชฝ์ผ๋ก ๋ ๋ค๋๊ณ , ๋ค๋ฐ๋ฅด๋ ์์๋ค์ด ์ผ์ชฝ์์ ๊ฐ์ธ์ค๋คnone
: ๊ธฐ๋ณธ๊ฐ, ์์๊ฐ ๋ ๋ค๋์ง ์์inherit
: ๋ถ๋ชจ ์์๋ก๋ถํฐ ๊ฐ์ ์์
float ์ฌ์ฉ ์์ :
.float-left {
float: left;
width: 200px;
height: 200px;
background-color: lightblue;
}
.float-right {
float: right;
width: 200px;
height: 200px;
background-color: lightgreen;
}
.content {
background-color: lightgrey;
padding: 10px;
}
<div class="float-left">Left Float</div>
<div class="float-right">Right Float</div>
<div class="content">
<p>This is some content that will wrap around the floated elements. The left floated element will be placed to the left, and the right floated element will be placed to the right.</p>
</div>
7.2 clear ์์ฑ
clear
์์ฑ์ ๋ ๋ค๋๋ ์์๋ค ์ฃผ๋ณ์ ๋์์ ์ ์ดํด. ํน์ ๋ฐฉํฅ์์ ๋ ๋ค๋๋ ์์๋ค์ด ์์๋ฅผ ๊ฐ์ธ์ง ๋ชปํ๋๋ก ํ ์ ์์ด.
๋ฌธ๋ฒ:
.element {
clear: left; /* ๋๋ right */
}
clear ์์ฑ์ ๊ฐ:
left
: ์์๋ ์ผ์ชฝ์์ ๊ฐ์ธ์ง ์์right
: ์์๋ ์ค๋ฅธ์ชฝ์์ ๊ฐ์ธ์ง ์์both
: ์์๋ ์ด๋ ์ชฝ์์๋ ๊ฐ์ธ์ง ์์none
: ๊ธฐ๋ณธ๊ฐ, ์์๊ฐ ์์ชฝ์์ ๊ฐ์ธ์ง
clear ์ฌ์ฉ ์์ :
.float-left {
float: left;
width: 200px;
height: 200px;
background-color: lightblue;
}
.float-right {
float: right;
width: 200px;
height: 200px;
background-color: lightgreen;
}
.clear {
clear: both;
background-color: lightcoral;
padding: 10px;
}
.content {
background-color: lightgrey;
padding: 10px;
}
<div class="float-left">Left Float</div>
<div class="float-right">Right Float</div>
<div class="clear">Cleared Element</div>
<div class="content">
<p>This content will not wrap around the floated elements because of the cleared element above.</p>
</div>
"clearfix" ๊ธฐ๋ฒ์ ์์ :
.container::after {
content: "";
display: table;
clear: both;
}
<div class="container">
<div class="sidebar">Sidebar Content</div>
<div class="main">Main Content</div>
</div>
7.3 float ์ฌ์ฉ์ ์ฅ๋จ์
์ฅ์
-
์ฌ์ด ์ฌ์ฉ๋ฒ:
float
๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ๋จํ ๋ ์ด์์์ ์ฝ๊ฒ ๋ง๋ค ์ ์์ด
-
ํญ๋์ ๋ธ๋ผ์ฐ์ ์ง์:
float
๋ ๋ชจ๋ ํ๋์ ์ธ ๋ธ๋ผ์ฐ์ ์์ ์ง์๋๊ณ , ์๋ ๋ฒ์ ์์๋ ์ง์๋ผ
-
๋ ์ด์์์ ์ ์ฐ์ฑ:
- ์ด๋ฏธ์ง๋ฅผ ํ ์คํธ๋ก ๊ฐ์ธ๋ ๊ฒ๋ถํฐ ๋ค์ค ํ ๋ ์ด์์๊น์ง ๋ค์ํ ๋ ์ด์์์ ๋ง๋ค ์ ์์ด
๋จ์
-
๋ณต์กํ ๋ ์ด์์ ๊ด๋ฆฌ์ ์ด๋ ค์:
float
๋ ๋ณต์กํ ๋ ์ด์์์ ๋ง๋ค ๋ ํนํ ์์ง ์ ๋ ฌ์ด ํ์ํ ๋ ๋ฌธ์ ๋ฅผ ์ผ์ผํฌ ์ ์์ด
-
๋ถ๋ชจ ์์์ ๋์ด ๋ฌธ์ :
- ๋ชจ๋ ์์ ์์๊ฐ ๋ ๋ค๋๋ฉด ๋ถ๋ชจ ์์๊ฐ "์ถ์"๋ ์ ์์ด. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด
clearfix
๊ฐ์ ๊ธฐ๋ฒ์ด ํ์ํด
- ๋ชจ๋ ์์ ์์๊ฐ ๋ ๋ค๋๋ฉด ๋ถ๋ชจ ์์๊ฐ "์ถ์"๋ ์ ์์ด. ์ด๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด
-
๊ตฌ์ ๋ฐฉ๋ฒ:
- Flexbox์ Grid ๊ฐ์ ํ๋์ ๋ฐฉ๋ฒ๋ค์ด ๋ ๊ฐ๋ ฅํ๊ณ ์ ์ฐํ ๋ ์ด์์ ๋ฐฉ๋ฒ์ ์ ๊ณตํด์ค
float์ clear๋ฅผ ์ฌ์ฉํ ๋ ์ด์์ ๋ง๋ค๊ธฐ ์์
float
์ clear
๋ฅผ ์ฌ์ฉํ์ฌ ๋ ๊ฐ์ ์ปฌ๋ผ ๋ ์ด์์์ ๋ง๋๋ ๋ฐฉ๋ฒ์ด์ผ.
์ด ์์ ์์๋ ์ฌ์ด๋๋ฐ(.sidebar
)์ ๋ฉ์ธ ์ฝํ
์ธ (.main
)๋ฅผ ๊ฐ์ง ๋ ๊ฐ์ ์ปฌ๋ผ ๋ ์ด์์์ ๋ง๋ค์ด.
.sidebar
์ .main
์์๋ ์ผ์ชฝ์ผ๋ก ๋ ๋ค๋๊ณ , clear: both;
๊ฐ ์๋ .footer
์์๋ ๊ฐ์ธ๋ ๊ฒ์ ๋ฐฉ์งํด.
.container {
width: 100%;
}
.sidebar {
float: left;
width: 25%;
background-color: lightblue;
padding: 10px;
}
.main {
float: left;
width: 75%;
background-color: lightgreen;
padding: 10px;
}
.footer {
clear: both;
background-color: lightcoral;
padding: 10px;
text-align: center;
}
<div class="container">
<div class="sidebar">Sidebar Content</div>
<div class="main">Main Content</div>
<div class="footer">Footer Content</div>
</div>
float
์ clear
๋ ์ฌ์ ํ ์น ๊ฐ๋ฐ์๋ค์ ๋๊ตฌ ์์์์ ์ค์ํ ๋๊ตฌ์ผ, ๋น๋ก ๋ ํ๋์ ์ธ ๋ฐฉ๋ฒ๋ค์ด ๋์ค๊ธด ํ์ง๋ง.
๊ทธ๊ฒ๋ค์ ๋ํ ์ดํด๋ ์ค๋๋ ํ๋ก์ ํธ๋ฅผ ์ง์ํ๊ณ ์
๋ฐ์ดํธํ๋ ๋ฐ ๋์์ด ๋๊ณ , ๊ฐ๋จํ ๋ ์ด์์์ ๋ง๋๋ ๋ฐ์๋ ์ฌ์ ํ ์ ์ฉํด.
Flexbox์ Grid ๊ฐ์ ํ๋์ ์ธ ๋ฐฉ๋ฒ๋ค์ด ๋ณต์กํ๊ณ ๋ฐ์ํ์ธ ๋ ์ด์์์ ๋ง๋๋ ๋ฐ ๋ ์ ํธ๋๊ธด ํ์ง๋ง, float
์ clear
์ ๋ํ ์ง์์ ์ฌ์ ํ ์ ํจํด.
GO TO FULL VERSION