Floating Elements with CSS

CSSWeb DevelopmentFront End Technology

The CSS float property is used for positioning or formatting a box or content. Developer can position element towards left or right with CSS float.

The float property can have one of the following values −

  • left − The element floats to the left of its container
  • right − The element floats to the right of its container
  • none − The element does not float. It is default value
  • inherit − The element inherits the float value of its parent


Let’s see an example of CSS Float property −

 Live Demo

<!DOCTYPE html>
<title>CSS Float</title>
form {
   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;
   height: 40px;
   width: 40px;
   color: white;
   border: 4px solid black;
   background-color: #FF8A00;
   background-color: #F44336;
   background-color: #C303C3;
   background-color: #4CAF50;
   background-color: #03A9F4;
   background-color: #FEDC11;
   display: flex;
   float: left;
   display: flex;
   float: right;
<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>
<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>
   var left = document.getElementById('left');
   var right = document.getElementById('right');
   function floatDecider(direction){
      var allChildren = document.getElementsByClassName('child');
      if(direction === 'left')


This will produce the following output −

Before clicking any button −

Clicking ‘float-->left’ button 4 times −

Clicking ‘float-->right’ button −

Updated on 08-Jan-2020 11:03:39