<!DOCTYPE html>
<html>
<head>
<style>
img {
float: left;
margin-right: 10px;
}
</style>
</head>
<body>
<img src="https://poiemaweb.com/img/doug.jpg">
<div>The float property specifies whether an element should float to the left, right, or not at all.
Note: Absolutely positioned elements ignore the float property!
Note: Elements next to a floating element will flow around it. To avoid this, use the clear property or the clearfix hack.</div>
</body>
</html>
float을 적용하면 해당 요소가 다른 요소 위에 떠 있게 함 (기본 레이아웃에서 벗어나 요소의 모서리가 페이지의 왼쪽이나 오른쪽으로 이동)
해당 요소의 position이 absolute일 때, float 사용 불가능
| property 값 | description |
|---|---|
| none | 요소가 떠 있게 하지 않음 (기본값) |
| right | 요소를 오른쪽으로 이동 |
| left | 요소를 왼쪽으로 이동 |
<!DOCTYPE html>
<html>
<head>
<style>
.box {
color: white;
font-weight: bold;
font-size: 50px;
border-radius: 6px;
width: 100px;
height: 100px;
margin: 10px;
padding: 10px;
}
.d1, .d2 {
float: left;
}
.d1 {
background-color: red;
}
.d2 {
background-color: orange;
}
.d3, .d4 {
float: right;
}
.d3 {
background-color: red;
}
.d4 {
background-color: orange;
}
</style>
</head>
<body>
<div class="container">
<div class="box d1"> 1 </div>
<div class="box d2"> 2 </div>
<div class="box d3"> 3 </div>
<div class="box d4"> 4 </div>
</div>
</body>
</html>
div {
width: 960px;
margin: 0 auto;
}
<!DOCTYPE html>
<html>
<head>
<style>
.box {
color: white;
font-weight: bold;
font-size: 30px;
line-height: 50px;
height: 50px;
margin: 0 10px;
padding: 10px;
}
.d1 {
background-color: red;
}
.d2 {
background-color: orange;
}
</style>
</head>
<body>
<div class="box d1"> div </div>
<div class="box d2"> div </div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.box {
color: white;
font-weight: bold;
font-size: 30px;
line-height: 50px;
height: 50px;
margin: 0 10px;
padding: 10px;
}
.d1 {
float: left;
background-color: red;
}
.d2 {
background-color: orange;
}
</style>
</head>
<body>
<div class="box d1"> float: left; </div>
<div class="box d2"> div </div>
</body>
</html>
문제 : float 선언된 요소가 float 선언하지 않은 다음 요소 위에 떠 있는 상태에서는 두 요소 간 margin이 제대로 표현되지 않음
해결 방법 : float 선언하지 않은 요소에 overflow:hidden;을 선언함
<!DOCTYPE html>
<html>
<head>
<style>
.box {
color: white;
font-weight: bold;
font-size: 30px;
line-height: 50px;
height: 50px;
margin: 0 10px;
padding: 10px;
}
.d1 {
float: left;
background-color: red;
}
.d2 {
overflow: hidden;
background-color: orange;
}
</style>
</head>
<body>
<div class="box d1"> float: left; </div>
<div class="box d2"> div </div>
</body>
</html>
문제 : float 선언된 2개의 자식 요소를 포함하는 부모 요소의 높이가 비정상값을 가짐
<!DOCTYPE html>
<html>
<head>
<style>
.container {
color: white;
text-align: center;
padding: 10px;
background-color: #def0c2;
}
.d1, .d2 {
float: left;
width: 50%;
padding: 20px 0;
}
.d1 {
background-color: #59b1f6;
}
.d2 {
background-color: #ffb5b4;
}
</style>
</head>
<body>
<div class="container">
<div class="d1"> 1 </div>
<div class="d2"> 2 </div>
</div>
<div style="background:red;padding:10px;color:white"> 3 </div>
</body>
</html>
.container {
...
overflow: hidden;
}
<!DOCTYPE html>
<html>
<head>
<style>
.container {
color: white;
text-align: center;
padding: 10px;
background-color: #def0c2;
overflow: hidden;
}
.d1, .d2 {
float: left;
width: 50%;
padding: 20px 0;
}
.d1 {
background-color: #59b1f6;
}
.d2 {
background-color: #ffb5b4;
}
</style>
</head>
<body>
<div class="container">
<div class="d1"> 1 </div>
<div class="d2"> 2 </div>
</div>
<div style="background:red;padding:10px;color:white"> 3 </div>
</body>
</html>
다른 방법들
부모 요소에 float 선언 -> 부모 요소의 너비가 float 선언된 2개의 자식 요소 content를 표현할 수 있을 만큼만으로 줄어듬 (권장 X)
부모 요소 영역이 끝나기 직전에 빈 요소를 만들고 clear: both;를 설정 <- 의미 없는 빈 요소를 사용해야함 (권장 X)
<!DOCTYPE html>
<html>
<head>
<style>
.container {
color: white;
text-align: center;
padding: 10px;
background-color: #def0c2;
}
.d1, .d2 {
float: left;
width: 50%;
padding: 20px 0;
}
.d1 {
background-color: #59b1f6;
}
.d2 {
background-color: #ffb5b4;
}
.clear {
height: 0;
clear: both;
}
</style>
</head>
<body>
<div class="container">
<div class="d1"> 1 </div>
<div class="d2"> 2 </div>
<div class="clear"></div>
</div>
<div style="background:red;padding:10px;color:white"> 3 </div>
</body>
</html>
해결 방법 2 : ::after 가상 요소 선택자를 이용 (CSS2에서는 :after)
부모 요소에 사전에 작성한 clearfix 클래스를 추가하거나, 해당 요소를 선택하여 클리어 문법을 선언하면 됨
.clearfix:after {
content: "";
display: block;
clear: both;
}
/* or */
selector:after {
content: "";
display: block;
clear: both;
}
<!DOCTYPE html>
<html>
<head>
<style>
.container {
color: white;
text-align: center;
padding: 10px;
background-color: #def0c2;
}
.clearfix:after {
content: "";
display: block;
clear: both;
}
.d1, .d2 {
float: left;
width: 50%;
padding: 20px 0;
}
.d1 {
background-color: #59b1f6;
}
.d2 {
background-color: #ffb5b4;
}
</style>
</head>
<body>
<div class="container clearfix">
<div class="d1"> 1 </div>
<div class="d2"> 2 </div>
</div>
<div style="background:red;padding:10px;color:white"> 3 </div>
</body>
</html>
또 다른 방법 : 자식요소에 float 대신 display: inline-block;을 선언
<!DOCTYPE html>
<html>
<head>
<style>
.container {
color: white;
text-align: center;
padding: 10px;
background-color: #def0c2;
}
.d1, .d2 {
display: inline-block;
width: 50%;
padding: 20px 0;
}
.d1 {
background-color: #59b1f6;
}
.d2 {
background-color: #ffb5b4;
}
</style>
</head>
<body>
<div class="container">
<div class="d1"> 1 </div>
<div class="d2"> 2 </div>
</div>
<div style="background:red;padding:10px;color:white"> 3 </div>
</body>
</html>
문제 : display: inline-block; 선언한 두 요소가 가로 정렬 되지 않음
- 원인 : 두 요소 사이에 정의하지 않은 공백(4px)이 자동 지정되어 부모 요소의 width를 초과함
해결방법 : 부모 요소에 font: 0;을 선언하여 두 요소 사이에 정의하지 않은 공백 제거
<!DOCTYPE html>
<html>
<head>
<style>
.container {
color: white;
text-align: center;
padding: 10px;
background-color: #def0c2;
/*폰트 사이즈를 0으로 지정하여 두 요소 사이에 정의하지 않은 공백 제거*/
font-size: 0;
}
.d1, .d2 {
display: inline-block;
width: 50%;
padding: 20px 0;
/* 폰트 사이즈 재지정 */
font-size: 1rem;
}
.d1 {
background-color: #59b1f6;
}
.d2 {
background-color: #ffb5b4;
}
</style>
</head>
<body>
<div class="container">
<div class="d1"> 1 </div>
<div class="d2"> 2 </div>
</div>
<div style="background:red;padding:10px;color:white"> 3 </div>
</body>
</html>