λμ΄μ νλ‘νΈκ° μ΄λ»κ² μλνλμ§ λ¬»κ³ μΆμ΅λλ€. μΈλΆ divμ λ΄μ©μ΄μλ λ΄λΆ divκ° μμ΅λλ€. λμ΄λ λ΄λΆ divμ λ΄μ©μ λ°λΌ λ€λ₯Ό μ μμ§λ§ λ΄ λ΄λΆ divκ° μΈλΆ divλ₯Ό μ€λ²νλ‘νλ κ²μ²λΌ 보μ λλ€. μ¬λ°λ₯Έ λ°©λ²μ 무μμ λκΉ?
<html>
<body>
<div style="margin:0 auto;width: 960px; min-height: 100px; background-color:orange">
<div style="width:500px; height:200px; background-color:black; float:right"></div>
</div>
</body>
</html>
λ΅λ³
λΆλ μμλ 컨ν μ΄λ μμμ λμ΄μ μΆκ°λμ§ μμΌλ―λ‘ μ§μ°μ§ μμΌλ©΄ 컨ν μ΄λ λμ΄κ° μ¦κ°νμ§ μμ΅λλ€ β¦
μκ°μ μΌλ‘ λ³΄μ¬ λλ¦¬κ² μ΅λλ€.
μμΈν μ€λͺ :
<div>
<div style="float: left;"></div>
<div style="width: 15px;"></div> <!-- This will shift
besides the top div. Why? Because of the top div
is floated left, making the
rest of the space blank -->
<div style="clear: both;"></div>
<!-- Now in order to prevent the next div from floating beside the top ones,
we use `clear: both;`. This is like a wall, so now none of the div's
will be floated after this point. The container height will now also include the
height of these floated divs -->
<div></div>
</div>
overflow: hidden;컨ν
μ΄λ μμλ₯Ό μΆκ° ν μλ μμ§λ§ clear: both;λμ μ¬μ© νλ κ²μ΄ μ’μ΅λλ€ .
λν μμλ₯Ό μ€μ€λ‘ μ§μ°κ³ μΆλ€λ©΄ μ¬μ©ν μ μμ΅λλ€.
.self_clear:after {
content: "";
clear: both;
display: table;
}
CSS νλ‘νΈλ μ΄λ»κ² μλν©λκΉ?
floatλ μ νν 무μμ΄κ³ 무μμν©λκΉ?
-
floatμμ±μ λλΆλΆμ μ΄λ³΄μ μ€ν΄νλ€. κΈμ, μ νν 무μμfloatν©λκΉ? μ²μμλfloatμμ±μ΄ λ λ΄λ € μ΄λ―Έμ§ μ£Όμμ ν μ€νΈ νλ¦μ λμleftλλright. @Madara Uchicha μ λ λ€λ₯Έ μ€λͺ μ΄ μμ΅λλ€.λ°λΌμ
floatμμλ₯Ό λλν λ°°μΉ νλ λ° μμ± μ μ¬μ©νλ κ²μ΄ μλͺ» λμμ΅λκΉ? λλ΅μ μλμμ λλ€ .floatμμλ₯Ό λλν μ€μ νκΈ° μν΄ μμ± μ μ¬μ©νλ©΄ μλ¬΄λ° λ¬Έμ κ° μμ΅λλ€ . -
inlineλλblockλ 벨 μμλ₯Ό νλ‘ν νλ©΄ μμκ° μμμ²λΌ λμν©λλ€inline-block. -
λΉμ μ΄ μμλ₯Ό λ κ²½μ°
leftλλrightμwidthμμμλνμ§ μλ νμ΄ λ³΄μ νκ³ μλ λ΄μ©μΌλ‘ μ νλ©λλ€widthλͺ μ μ μΌλ‘ μ μλλ€ β¦ -
floatμμλ₯Ό μ¬μ©ν μ μμ΅λλ€center. μ΄κ²μ λ΄κ° μ΄λ³΄μμκ² νμ λ΄€λ κ°μ₯ ν° λ¬Έμ μ λλ€.μ΄float: center;floatμμ± μ μ ν¨ν κ°μ΄ μλλλ€ .floatμΌλ°μ μΌλ‘float컨ν μΈ λ₯Ό 맨 μΌμͺ½ μ΄λ 맨 μ€λ₯Έμͺ½μΌλ‘ μ΄λ νλ λ° μ¬μ©λ©λλ€ . λ¨μ§κ° μμ΅λλ€ μ¬ μ λν μ ν¨ν κ°floatμμ± μ¦left,right,none(κΈ°λ³Έ) λ°μ΄inherit. -
νλ‘ν λ μμ μμλ₯Ό ν¬ν¨ ν λ λΆλͺ¨ μμκ° μΆμλλ©΄μ΄λ₯Ό λ°©μ§νκΈ° μν΄
clear: both;μμ± μ μ¬μ© νμ¬ μμͺ½μμ νλ‘ν λ μμλ₯Ό μ§μ°λ©΄ λΆλͺ¨ μμκ° μ νμ§ μμ΅λλ€ . μμΈν λ΄μ©μ μ¬κΈ°μμ λ€λ₯Έ λ΅λ³μ μ°Έμ‘° νμμμ€ . -
(μ€μ) λ€μν μμκ° μμ¬μλ κ³³μ μκ°ν΄λ³΄μμμ€. μ°λ¦¬κ° μ¬μ©ν λ
float: left;λλfloat: right;μμκ° μ€ν μλ‘ νλμ© μμ§μ λλ€. λ°λΌμ μΌλ° λ¬Έμ νλ‘μ°μ μμλ λΆλ μμλ³΄λ€ μ€ν λ 벨μ μμΌλ―λ‘ λΆλ μμ λ€μ μ¨κ²¨μ§λλ€. (z-indexμμ ν λ€λ₯΄κΈ° λλ¬Έμ μ΄κ²μ κ΄λ ¨μν€μ§ λ§μμμ€ .)
ν€λ, λ°λ₯ κΈ λ° 2 κ°μ μ΄μ΄μλ κ°λ¨ν 2 μ΄ λ μ΄μμμ΄ νμνλ€κ³ κ°μ νκ³ CSS λΆλ μμμ μλ λ°©μμ μ€λͺ νλ μ¬λ‘λ₯Ό μλ‘ λ€μ΄, μ¬κΈ°μ μ²μ¬μ§μ΄ νμλ©λλ€.
μμ μμμ, μ°λ¦¬λ λ¨μ§ λΉ¨κ°μ μμλ₯Ό λ μ€ νλλ₯Ό μν ν μ μμ΅λλ€ κ² floatλͺ¨λμ left, λλ λΉμ μ ν μ floatμ left, κ·Έλ¦¬κ³ λ λ€λ₯Έ rightλ 3 μ΄μμ κ²½μ° μλ, λ μ΄μμμ λ°λΌ λ¬λΌ μλλΌ float2 μ΄ leftκ²½μ° λ€λ₯Έ rightμ΄ μμ μμλ λ¨μν λ 2 μ΄ λ μ΄μμμ΄ μμΌλ―λ‘ floatνλ leftμ λ€λ₯Έ νλ λ μλ‘ μμ‘΄ ν©λλ€ right.
λ μ΄μμμ λ§λ€κΈ°μν λ§ν¬ μ κ³Ό μ€νμΌμ λν΄μλ μλμμ μμΈν μ€λͺ ν©λλ€ β¦
<div class="main_wrap">
<header>Header</header>
<div class="wrapper clear">
<div class="floated_left">
This<br />
is<br />
just<br />
a<br />
left<br />
floated<br />
column<br />
</div>
<div class="floated_right">
This<br />
is<br />
just<br />
a<br />
right<br />
floated<br />
column<br />
</div>
</div>
<footer>Footer</footer>
</div>
* {
-moz-box-sizing: border-box; /* Just for demo purpose */
-webkkit-box-sizing: border-box; /* Just for demo purpose */
box-sizing: border-box; /* Just for demo purpose */
margin: 0;
padding: 0;
}
.main_wrap {
margin: 20px;
border: 3px solid black;
width: 520px;
}
header, footer {
height: 50px;
border: 3px solid silver;
text-align: center;
line-height: 50px;
}
.wrapper {
border: 3px solid green;
}
.floated_left {
float: left;
width: 200px;
border: 3px solid red;
}
.floated_right {
float: right;
width: 300px;
border: 3px solid red;
}
.clear:after {
clear: both;
content: "";
display: table;
}
λ μ΄μμμ λ¨κ³λ³λ‘ μ΄ν΄λ³΄κ³ float μλ λ°©μμ μ΄ν΄ λ³΄κ² μ΅λλ€.
μ°μ , μ°λ¦¬λ μ£Όμ λνΌ μμλ₯Ό μ¬μ©νμ¬, λΉμ μ λ¨μ§ μ°λ¦¬κ° μ¬μ©, κ·Έκ²μ λΉμ μ λ·°ν¬νΈ μλ€κ³ κ°μ ν μ headerμλ₯Ό ν λΉ heightμ 50pxμ무κ²λ 곡μ κ·Έλμμ΄. νλ‘ν
100%λμ§ μκ±°λ ν λΉνμ§ μλ ν μν κ³΅κ° μ μ°¨μ§νλ μΌλ° λΉ νλ‘ν
λΈλ‘ λ 벨 μμμ
λλ€ inline-block.
μ λν 첫 λ²μ§Έ μ ν¨ν κ° floatμ leftμμ μμ μ¬μ© λλ―λ‘ float: left;for λ₯Ό μ¬μ© .floated_leftνλ―λ‘ left컨ν
μ΄λ μμμ λΈλ‘μ νλ‘ν
νλ €κ³ ν©λλ€ .
μ΄μ΄ μΌμͺ½μΌλ‘ λΉλλ€.
κ·Έλ¦¬κ³ λ€, 보μλ©΄, .wrapperμ ν λΆλͺ¨ μμ λ μ΄λ‘μ ν
λλ¦¬λ‘ λ³΄μμ λ νμ₯λμ§ μμμ§λ§ λ§μ΅λκΉ? μ μ νμ λ€μ λμμ¬ κ²μ
λλ€. μ§κΈμ μ΄μ΄μ λΉλλ€ left.
λ λ²μ§Έ μ΄λ‘ μ€λ©΄μ΄ μ΄ floatμright
μ€λ₯Έμͺ½μ λ λ€λ₯Έ κΈ°λ₯μ΄ λ¬λ€
μ¬κΈ°, μ°λ¦¬λμ΄ 300pxμ°λ¦¬ λμ μ΄ floatλ°λ rightκ²μ΄κ°μ λ΄λ€λ©λλ€μΌλ‘ 첫 λ²μ§Έ μ΄ μμ μμ κ² left, κ·Έλ¦¬κ³ μ΄μ λ μ΄κΈ° λλ¬Έμ left, κ·Έκ²μμ λΉ κ±°ν°λ₯Ό μμ± rightνκ³ , 곡κ°μ μΆ©λΆν κ±°κΈ° λλ¬Έμ right, μ°λ¦¬μ rightλ λ€λλ μμκ° μμ μλ²½νκ² μμμ΅λλ€ left.
μ¬μ ν μμ μμκ° μΆμλμμ΅λλ€. μ΄μ μμ ν΄ λ³΄κ² μ΅λλ€. λΆλͺ¨ μμκ° μ°λ¬μ§λ κ²μ λ§λ λ°©λ²μλ μ¬λ¬ κ°μ§κ° μμ΅λλ€.
- λΉ λΈλ‘ λ 벨 μμλ₯Ό μΆκ°νκ³ λΆλ μμλ₯Ό
clear: both;보μ νλ μμ μμκ° λλκΈ° μ μ μ¬μ© νμμμ€.μ΄ μμλclearλΆλ μμμ λν μ λ ΄ν μ루μ μ΄λ―λ‘ μμ μ μννμ§λ§ κΆμ₯νμ§λ μμ΅λλ€.
λ <div style="clear: both;"></div>μ μ λ€μ .wrapper divκ³Ό κ°μ΄ μΆκ°νμμμ€.
<div class="wrapper clear">
<!-- Floated columns -->
<div style="clear: both;"></div>
</div>
κΈμ, κ·Έκ²μ λ μ μμ λκ³ λ μ΄μ μΆμ λ λΆλͺ¨λ μμ§λ§ DOMμ λΆνμν λ§ν¬ μ
μ μΆκ°νλ―λ‘ overflow: hidden;μλ λλλ‘ μλνλ λΆλ μμ μμλ₯Ό 보μ νλ λΆλͺ¨ μμμ μ¬μ© νλ κ²μ΄ μ’μ΅λλ€ .
μ¬μ© overflow: hidden;μ.wrapper
.wrapper {
border: 3px solid green;
overflow: hidden;
}
κ·Έκ²μ μ°λ¦¬κ° νμν λλ§λ€ μμλ₯Ό μ μ₯ clear floatνμ§λ§ μ΄κ²μΌλ‘ λ€μν μ¬λ‘λ₯Ό ν
μ€νΈ ν box-shadowλ μμ μμ λ₯Ό μ¬μ©νλ νΉμ μ¬λ‘μμ μ€ν¨νμ΅λλ€ .
λ°λͺ¨ (4λ©΄ λͺ¨λμμ κ·Έλ¦Όμλ₯Ό λ³Ό μoverflow: hidden; λ¬Έμ κ° λ°μν©λλ€)
κ·Έλμ μ§κΈ 무μ? μμλ₯Ό μ μ₯νμ§ overflow: hidden;λ§μμμ€. λͺ
νν μμ ν΄νΉμ μν΄ CSSμμ μλ μ€ λν«μ μ¬μ© overflow: hidden;νμμμ€. μμ μμ μ λ§μ°¬κ°μ§λ‘ λΆλͺ¨ μμμμ classμλλ₯Ό νΈμΆνμ¬ μ체 μ 리νμμμ€.
.clear:after {
clear: both;
content: "";
display: table;
}
<div class="wrapper clear">
<!-- Floated Elements -->
</div>
μ¬κΈ°μ κ·Έλ¦Όμλ μλ νλλ‘ μλνλ©° μΆμλμ§ μλλ‘ λΆλͺ¨ μμλ₯Ό μ체μ μΌλ‘ μ§ μλλ€.
λ§μ§λ§μΌλ‘ clearνλ‘ν
μμ λ€μ λ°λ₯ κΈμ μ¬μ© ν©λλ€.
μΈμ float: none;κ° κΈ°λ³Έμ΄κΈ° λλ¬Έμ, μ΄μ¨λ μ¬μ© μ μΈμ λν μ¬μ©νλ―λ‘float: none; ?
λ°μ ν λμμΈμ μνλ€λ©΄ νλ‘ν
λ μμκ° νΉμ ν΄μλμμ λ€λ₯Έ μμ μλλ‘ λ λλ§λλλ‘ ν λμ΄ κ°μ μ¬λ¬ λ² μ¬μ©ν©λλ€. κ·Έ float: none;μμ±μμμ΄ μ€μν μν μν©λλ€.
floatμ μ©ν λ°©λ²μ λν μ€μ μ¬λ‘λ κ±°μ μμ΅λλ€ .
- μ°λ¦¬κ° μ΄λ―Έ λ³Έ 첫 λ²μ§Έ μλ νλ μ΄μμ μ΄ λ μ΄μμμ λ§λλ κ²μ λλ€.
imgλ΄λΆμ νλ‘νΈλ₯Ό μ¬μ©νλ©΄pμ½ν μΈ κ° νλ₯Ό μ μμ΅λλ€.
λ°λͺ¨ (νλ‘ν
imgμ μΈ)
λ°λͺ¨ 2 (μimgλ¬λ€left)
floatκ°λ‘ λ©λ΄ μμ±μ μ¬μ© β λ°λͺ¨
λ λ²μ§Έ μμλ νλ‘ν νκ±°λ μ¬λ°±μ μ¬μ©νμμμ€.
λ§μ§λ§μΌλ‘, μ μ΄λ, λλ λΉμ μ΄ floatνλμ μμλ§μ κ°μ§κ³ leftμμ§λ§ κ·Έλ μ§ μμ μ΄ νΉλ³ν κ²½μ°λ₯Ό μ€λͺ
νκ³ μΆμ΅λλ€.float λ€λ₯Έ μ΄λ»κ²λ©λκΉ?
μ°λ¦¬κ° μ κ±°νλ©΄ κ°μ float: right;μ°λ¦¬μμμ .floated_right classμλ divκ·Ήλ¨μ μΈμμ λ λλ§λλ leftμ΄ λ λμ§ μλ.
κ·Έλμμ΄ κ²½μ°, νλ ν μ μμ΅λλ€ floatνμ¬μ leftλΏλ§ μλλΌ
λλ
λΉμ μ ν μ μ¬μ© margin-leftμ¦ λ μΌμͺ½ μΉΌλΌμ ν¬κΈ°μ λμΌ ν κ²μ΄λ€ 200pxλμ .
λ΅λ³
overflow:autoλ΄λΆ λΆλ divλ₯Ό ν¬ν¨νλ €λ©΄ μμ divμ μΆκ° ν΄μΌν©λλ€.
<div style="margin:0 auto;width: 960px; min-height: 100px; background-color:orange;overflow:auto">
<div style="width:500px; height:200px; background-color:black; float:right">
</div>
</div>
λ΅λ³
νλ‘νΈ λ²κ·Έκ° λ°μνμ΅λλ€ (μ΄ λμμ λνλ΄λ λΈλΌμ°μ μλ‘ μΈν΄ κΈ°μ μ μΌλ‘ λ²κ·ΈμΈμ§ νμ€νμ§λ μμ΅λλ€). μ¬κΈ°μ λ¬΄μ¨ μΌμ΄ μΌμ΄λκ³ μμ΅λκΉ?
μ μμ μΈ μν©μμ λͺ μμ μΈ λμ΄κ° μ€μ λμ§ μμλ€κ³ κ°μ νλ©΄ divμ κ°μ λΈλ‘ λ 벨 μμλ λ΄μ©μ λ°λΌ λμ΄λ₯Ό μ€μ ν©λλ€. μμ divμ νλ¨μ λ§μ§λ§ μμλ₯Ό λμ΄ νμ₯λ©λλ€. λΆννλ, μμλ₯Ό νλ‘ν νλ©΄ λμ΄λ₯Ό κ²°μ ν λ λΆλͺ¨κ° νλ‘ν λ μμλ₯Ό κ³ λ €νμ§ μμ΅λλ€. μ¦, λ§μ§λ§ μμκ° λΆλ μνμ΄λ©΄ μΌλ° μμμ κ°μ λ°©μμΌλ‘ λΆλͺ¨λ₯Ό βμ€νΈλ μΉβνμ§ μμ΅λλ€.
μ²μ°
μ΄ λ¬Έμ λ₯Ό ν΄κ²°νλ λ κ°μ§ μΌλ°μ μΈ λ°©λ²μ΄ μμ΅λλ€. 첫 λ²μ§Έλ βclearingβμμλ₯Ό μΆκ°νλ κ²μ λλ€. μ¦, νλ‘ν λ μμ μλμμλ λ€λ₯Έ μμλ λΆλͺ¨λ₯Ό κ°μ λ‘ λ립λλ€. λ°λΌμ λ€μ htmlμ λ§μ§λ§ μμμΌλ‘ μΆκ°νμμμ€.
<div style="clear:both"></div>
νμλμ΄μλ μλλ©° clear : bothλ₯Ό μ¬μ©νμ¬ λΆλ μμ μμ μμ§ μκ³ κ·Έλ€μμ νμλλλ‘νμμμ€.
κ³Όλ€:
λλΆλΆμ μ¬λλ€μ΄ μ νΈνλ λ λ²μ§Έ λ°©λ²μ λΆλͺ¨ μμμ CSSλ₯Ό λ³κ²½νμ¬ μ€λ²νλ‘κ° β보μ΄λ κ²βμ΄λλλ‘νλ κ²μ λλ€. λ°λΌμ μ€λ²νλ‘λ₯Ό βμ¨κ²¨ μ§βμΌλ‘ μ€μ νλ©΄ λΆλͺ¨κ° νλ‘ν λ μμμ 맨 μλλ‘ νμ₯λ©λλ€. λ¬Όλ‘ λΆλͺ¨μ λμ΄λ₯Ό μ€μ νμ§ μμ κ²½μ°μλ§ ν΄λΉλ©λλ€.
λ΄κ° λ§νλ―μ΄, λ λ²μ§Έ λ°©λ²μ μλ―Έ λ‘ μ μΌλ‘ μλ―Έμλ μμλ₯Ό λ§ν¬ μ
μ μΆκ° ν νμκ° μκΈ° λλ¬Έμ μ νΈλμ§λ§ overflow, νμ ν΄μΌ ν κ²½μ° κ° μμ΅λλ€.μ΄ κ²½μ° μ§μ°κΈ° μμλ₯Ό μΆκ°νλ κ²μ΄ νμ©λλ κ² μ΄μμ
λλ€ .
λ΅λ³
κ·Έκ²μ divμ float λλ¬Έμ
λλ€. overflow: hiddenμΈλΆ μμλ₯Ό μΆκ°νμμμ€ .
<div style="overflow:hidden; margin:0 auto;width: 960px; min-height: 100px; background-color:orange;">
<div style="width:500px; height:200px; background-color:black; float:right">
</div>
</div>
λ΅λ³
νλ‘ν μμκ°μμ λ λΈλΌμ°μ κ° μμλ₯Ό λ λλ§νλ λ°©λ²μ νΌλν©λλ€. νλμ λΈλ‘ μμκ° λΆλ μΈ κ²½μ° (κ·νμ λ΄λΆ div) λΈλΌμ°μ λ μΉ νμ΄μ§μ μ μμ μΈ νλ¦μμ λΆλ μμλ₯Ό μ κ±°νκΈ° λλ¬Έμ λ€λ₯Έ λΈλ‘ μμλ 무μν©λλ€. κ·Έλ° λ€μ νλ‘ν λ divκ° μΌλ° νλ¦μμ μ κ±°λμμΌλ―λ‘ λ΄λΆ divκ°μλ κ²μ²λΌ μΈλΆ divκ° μ±μμ§λλ€. κ·Έλ¬λ μΈλΌμΈ μμ (μ΄λ―Έμ§, λ§ν¬, ν μ€νΈ, ν° λ°μ΄ν)λ λΆλ μμμ κ²½κ³λ₯Ό μ‘΄μ€ν©λλ€. μΈλΆ divμ ν μ€νΈλ₯Ό λμ νλ©΄ ν μ€νΈκ° λ΄λΆ divμ λ°°μΉλ©λλ€.
μ¦, λΈλ‘ μμ (ν€λ, λ¨λ½, div λ±)λ λΆλ μμλ₯Ό 무μνκ³ μ±μ°κ³ μΈλΌμΈ μμ (μ΄λ―Έμ§, λ§ν¬, ν μ€νΈ λ±)λ λΆλ μμμ κ²½κ³λ₯Ό μ‘΄μ€ν©λλ€.
<body>
<div style="float:right; background-color:blue;width:200px;min-height:400px;margin-right:20px">
floating element
</div>
<h1 style="background-color:red;"> this is a big header</h1>
<p style="background-color:green"> this is a parragraph with text and a big image. The text places arrounds the floating element. Because of the image is wider than space between paragrah and floating element places down the floating element. Try to make wider the viewport and see what happens :D
<img src="http://2.bp.blogspot.com/_nKxzQGcCLtQ/TBYPAJ6xM4I/AAAAAAAAAC8/lG6XemOXosU/s1600/css.png">
</p>
λ΅λ³
μ΄κ±° νλ² ν΄λ΄
.parent_div{
display: flex;
}
λ΅λ³
컨ν μ΄λ μμ νμ ν divκ°μλ κ²½μ° μ»¨ν μ΄λ divμ overflow μμ±μ μ¬μ©ν μ μμ΅λλ€.
<div class="cointainer">
<div class="one">Content One</div>
<div class="two">Content Two</div>
</div>
λ€μμ CSSμ λλ€.
.container{
width:100%;/* As per your requirment */
height:auto;
float:left;
overflow:hidden;
}
.one{
width:200px;/* As per your requirment */
height:auto;
float:left;
}
.two{
width:200px;/* As per your requirment */
height:auto;
float:left;
}
ββββββββλλβββββββββ β-
<div class="cointainer">
<div class="one">Content One</div>
<div class="two">Content Two</div>
<div class="clearfix"></div>
</div>
λ€μμ CSSμ λλ€.
.container{
width:100%;/* As per your requirment */
height:auto;
float:left;
overflow:hidden;
}
.one{
width:200px;/* As per your requirment */
height:auto;
float:left;
}
.two{
width:200px;/* As per your requirment */
height:auto;
float:left;
}
.clearfix:before,
.clearfix:after{
display: table;
content: " ";
}
.clearfix:after{
clear: both;
}