CSS float 속성은 상자나 콘텐츠의 위치를 지정하거나 서식을 지정하는 데 사용됩니다. 개발자는 CSS float를 사용하여 요소를 왼쪽 또는 오른쪽으로 배치할 수 있습니다.
float 속성은 다음 값 중 하나를 가질 수 있습니다. -
- 왼쪽 − 요소는 컨테이너의 왼쪽에 떠 있습니다.
- 맞습니다 − 요소는 컨테이너의 오른쪽에 떠 있습니다.
- 없음 - 요소는 부동하지 않습니다. 기본값입니다.
- 상속 − 요소는 상위 요소의 부동 소수점 값을 상속합니다.
예
CSS Float 속성의 예를 살펴보겠습니다 -
<!DOCTYPE html>
<html>
<head>
<title>CSS Float</title>
<style>
form {
width:70%;
margin: 0 auto;
text-align: center;
}
input[type="button"] {
border-radius: 10px;
}
#container {
display: flex;
flex-direction: column-reverse;
justify-content: center;
align-items: center;
}
.child{
height: 40px;
width: 40px;
color: white;
border: 4px solid black;
}
.orange{
background-color: #FF8A00;
}
.red{
background-color: #F44336;
}
.violet{
background-color: #C303C3;
}
.green{
background-color: #4CAF50;
}
.blue{
background-color: #03A9F4;
}
.yellow{
background-color: #FEDC11;
}
#left{
display: flex;
float: left;
}
#right{
display: flex;
float: right;
}
</style>
</head>
<body>
<form>
<fieldset>
<legend>CSS-Float</legend>
<div id="container">
<div class="child orange"></div><div class="child red"></div><div class="child violet"></div><div class="child green"></div><div class="child blue"></div><div class="child yellow"></div>
</div><br>
<input type="button" value="float-->left" onclick="floatDecider('left')">
<input type="button" value="float-->right" onclick="floatDecider('right')">
<div><div id="left"></div><div id="right"></div></div>
</fieldset>
</form>
<script>
var left = document.getElementById('left');
var right = document.getElementById('right');
function floatDecider(direction){
var allChildren = document.getElementsByClassName('child');
if(direction === 'left')
left.insertAdjacentElement('beforeend',allChildren[0]);
else
right.insertAdjacentElement('afterbegin',allChildren[0]);
}
</script>
</body>
</html> 출력
이것은 다음과 같은 출력을 생성합니다 -
버튼을 클릭하기 전에 -

'플로트-->왼쪽 클릭 ' 버튼 4번 -

'float-->오른쪽 클릭 t' 버튼 -
