Which property is used to move an element to left or right of its container?

The float property in CSS is used for positioning and layout on web pages. A common usage might be floating an image to one side and letting text wrap around it.

.intro-img {
  float: left;
}

Syntax

float = 
  block-start      |
  block-end        |
  inline-start     |
  inline-end       |
  snap-block       |
     |
  snap-inline      |
    |
  left             |
  right            |
  top              |
  bottom           |
  none             |
  footnote         

 = 
  snap-block(  , [ start | end | near ]? )  

 = 
  snap-inline(  , [ left | right | near ]? )  
  • Initial value: none
  • Applies to: all elements, but has no effect if the value of display is none.
  • Inherited: no
  • Computed value: as specified
  • Animation type: discrete

Values

/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
  • none: the element does not float. This is the initial value.
  • float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    0: floats the element to the left of its container.
  • float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    1: floats the element to the right of its container.
  • float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    2: the logical equivalent of
    float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    0 based on the
    float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    4.
  • float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    5: the logical equivalent of
    float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    1 based on the
    float = 
      block-start      |
      block-end        |
      inline-start     |
      inline-end       |
      snap-block       |
         |
      snap-inline      |
        |
      left             |
      right            |
      top              |
      bottom           |
      none             |
      footnote         
    
     = 
      snap-block(  , [ start | end | near ]? )  
    
     = 
      snap-inline(  , [ left | right | near ]? )  
    4.

An element that is floated is automatically

float = 
  block-start      |
  block-end        |
  inline-start     |
  inline-end       |
  snap-block       |
     |
  snap-inline      |
    |
  left             |
  right            |
  top              |
  bottom           |
  none             |
  footnote         

 = 
  snap-block(  , [ start | end | near ]? )  

 = 
  snap-inline(  , [ left | right | near ]? )  
8

What does “float” mean?

To understand the purpose and origin of float, we can look to print design. In a print layout, images may be set into the page such that text wraps around them as needed. This is commonly and appropriately called “text wrap”. Here is an example of that.

Which property is used to move an element to left or right of its container?

In page layout programs, the boxes that hold the text can be told to honor the text wrap, or to ignore it. Ignoring the text wrap will allow the words to flow right over the image like it wasn’t even there. This is the difference between that image being part of the flow of the page (or not). Web design is very similar.

Which property is used to move an element to left or right of its container?

In web design, page elements with the CSS float property applied to them are just like the images in the print layout where the text flows around them. Floated elements remain a part of the flow of the web page. This is distinctly different than page elements that use absolute positioning. Absolutely positioned page elements are removed from the flow of the webpage, like when the text box in the print layout was told to ignore the page wrap. Absolutely positioned page elements will not affect the position of other elements, and other elements will not affect them, whether they touch each other or not.

Demo

This demo shows an article with two images: one set to

/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
1, and one set to
/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
2. Press the “toggle floats” button to toggle the floats off and on.

CodePen Embed Fallback

Floats for layout

Aside from the simple example of wrapping text around images, floats can be used to create entire web layouts.

Which property is used to move an element to left or right of its container?

Floats are also helpful for layout in smaller instances. Take for example this little area of a web page. If we use float for our avatar image, when that image changes size the text in the box will reflow to accommodate it:

Which property is used to move an element to left or right of its container?

This same layout could be accomplished using relative positioning on the container and absolute positioning on the avatar as well. But, when it’s done that way, the text would be unaffected by the avatar, and would not be able to reflow on a size change.

Which property is used to move an element to left or right of its container?

Demo

This demo shows an avatar with

/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
1 applied. Press the “toggle image size” button to see a wider version of the avatar image. Notice that the text reflows to accommodate the image instead of running over the image.

CodePen Embed Fallback

Clearing the float

Float’s sister property is

/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
5. An element that has the
/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
5 property set on it will not move up adjacent to the float like the float desires, but will move itself down past the float. Again an illustration is more helpful than words:

Which property is used to move an element to left or right of its container?

In the above example, the sidebar is floated to the right and is shorter than the main content area. The footer then is required to jump up into that available space as is required by the float. To fix this problem, the footer can be cleared to ensure it stays beneath both floated columns.

#footer {
  clear: both;
}
Which property is used to move an element to left or right of its container?

Clear has four valid values as well. The value

/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
7 is most commonly used, which clears floats coming from either direction. The values
float = 
  block-start      |
  block-end        |
  inline-start     |
  inline-end       |
  snap-block       |
     |
  snap-inline      |
    |
  left             |
  right            |
  top              |
  bottom           |
  none             |
  footnote         

 = 
  snap-block(  , [ start | end | near ]? )  

 = 
  snap-inline(  , [ left | right | near ]? )  
0 and
float = 
  block-start      |
  block-end        |
  inline-start     |
  inline-end       |
  snap-block       |
     |
  snap-inline      |
    |
  left             |
  right            |
  top              |
  bottom           |
  none             |
  footnote         

 = 
  snap-block(  , [ start | end | near ]? )  

 = 
  snap-inline(  , [ left | right | near ]? )  
1 can be used to only clear the float from one direction respectively. The initial value is none, which is typically unnecessary unless it’s used to explicitly remove a
/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
5 value that has been set. The value
#footer {
  clear: both;
}
2 makes the element inherit its parent’s
/* Keyword values */
float: left;
float: right;
float: none;
float: inline-start;
float: inline-end;

/* Global values */
float: inherit;
float: initial;
float: revert;
float: revert-layer;
float: unset;
5 value. Strangely, Internet Explorer did not support this value until IE8.

Clearing only the

float = 
  block-start      |
  block-end        |
  inline-start     |
  inline-end       |
  snap-block       |
     |
  snap-inline      |
    |
  left             |
  right            |
  top              |
  bottom           |
  none             |
  footnote         

 = 
  snap-block(  , [ start | end | near ]? )  

 = 
  snap-inline(  , [ left | right | near ]? )  
0 or
float = 
  block-start      |
  block-end        |
  inline-start     |
  inline-end       |
  snap-block       |
     |
  snap-inline      |
    |
  left             |
  right            |
  top              |
  bottom           |
  none             |
  footnote         

 = 
  snap-block(  , [ start | end | near ]? )  

 = 
  snap-inline(  , [ left | right | near ]? )  
1 float, while less commonly seen in the wild, definitely has its uses.

Which property is used to move an element to left or right of its container?

The great collapse

One of the more bewildering things about working with floats is how they can affect the element that contains them (their “parent” element). If a parent element contains nothing but floated elements, its height collapses to nothing. This isn’t always obvious if the parent doesn’t contain any visually noticeable background, but it is important to be aware of.

Which property is used to move an element to left or right of its container?

As counterintuitive as collapsing may seem, the alternative is worse. Consider this scenario:

Which property is used to move an element to left or right of its container?

If the block element on top were to have automatically expanded to accommodate the floated element, we would have an unnatural spacing break in the flow of text between paragraphs, with no practical way of fixing it. If this were the case, we designers would be complaining much harder about this behavior than we do about collapsing.

Collapsing almost always needs to be dealt with to prevent strange layout and cross-browser problems. We fix it by clearing the float after the floated elements in the container but before the close of the container.

Techniques for clearing floats

If you are in a situation where you always know what the succeeding element is going to be, you can apply the

#footer {
  clear: both;
}
6 value to that element and go about your business. This is ideal as it requires no fancy hacks and no additional elements making it perfectly semantic. Of course things don’t typically work out that way and we need to have more float-clearing tools in our toolbox.

  • The Empty Div Method is, quite literally, an empty div
    #footer {
      clear: both;
    }
    7. Sometimes you’ll see a
    #footer {
      clear: both;
    }
    8 element or some other random element used, but div is the most common because it has no browser default styling, doesn’t have any special function, and is unlikely to be generically styled with CSS. This method is scorned by semantic purists since it has no contextual meaning to the page and is there purely for presentation. Of course, in the strictest sense, they are right. But, it gets the job done and doesn’t hurt anybody.
  • The Overflow Method relies on setting the
    #footer {
      clear: both;
    }
    9 CSS property on a parent element. If this property is set to
    .clearfix:after { 
      content: "";
      visibility: hidden;
      display: block;
      height: 0;
      clear: both;
    }
    0 or
    .clearfix:after { 
      content: "";
      visibility: hidden;
      display: block;
      height: 0;
      clear: both;
    }
    1 on the parent element, the parent will expand to contain the floats, effectively clearing it for succeeding elements. This method can be beautifully semantic as it may not require an additional elements. However, if you find yourself adding a new
    .clearfix:after { 
      content: "";
      visibility: hidden;
      display: block;
      height: 0;
      clear: both;
    }
    2 just to apply this, it is equally as un-semantic as the empty
    .clearfix:after { 
      content: "";
      visibility: hidden;
      display: block;
      height: 0;
      clear: both;
    }
    2 method, and less adaptable. Also bear in mind that the overflow property isn’t specifically for clearing floats. Be careful not to hide content or trigger unwanted scrollbars.
  • The Easy Clearing Method (otherwise known as “clearfix”) uses a clever CSS pseudo selector (
    .clearfix:after { 
      content: "";
      visibility: hidden;
      display: block;
      height: 0;
      clear: both;
    }
    4) to clear floats. Rather than setting the overflow on the parent, you apply an additional class like “clearfix” to it. Then apply this CSS:
.clearfix:after { 
  content: "";
  visibility: hidden;
  display: block;
  height: 0;
  clear: both;
}

This will apply a small bit of content, hidden from view, after the parent element which clears the float. This isn’t quite the whole story, as additional code needs to be used to accomodate for older browsers. 

Also see this snippet which keeps track of the latest and greatest in clearfixes, including the newer “micro clearfix.”

Different scenarios call for different float clearing methods. Take for example a grid of blocks, each of different types.

Which property is used to move an element to left or right of its container?

To better visually connect the similar blocks, we want to start a new row as we please, in this case when the color changes. We could use either the overflow or easy clearing method if each of the color groups had a parent element. Or, we use the empty div method in between each group. Three wrapping divs that didn’t previously exist or three after divs that didn’t previously exist. I’ll let you decide which is better.

Which property is used to move an element to left or right of its container?

Problems with floats

Floats often get beat on for being fragile. The majority of this fragility came from bugs in IE6 and IE7. As those browsers fade into the past, these bugs are fading along with them. But, it’s still worth understanding them if you ever need to debug an “OldIE”.

  • Pushdown is a symptom of an element inside a floated item being wider than the float itself (typically an image). Most browsers will render the image outside the float, but not have the part sticking out affect other layout. Old versions of IE expanded the float to contain the image, often drastically affecting layout. A common example is an image sticking out of the main content push the sidebar down below.
    Which property is used to move an element to left or right of its container?

    Quick fix: Make sure you don’t have any images that do this, use
    .clearfix:after { 
      content: "";
      visibility: hidden;
      display: block;
      height: 0;
      clear: both;
    }
    5 to cut off excess.
  • Double Margin Bug — Another thing to remember when dealing with IE 6 is that if you apply a margin in the same direction as the float, it will double the margin. Quick fix: set
    .clearfix:after { 
      content: "";
      visibility: hidden;
      display: block;
      height: 0;
      clear: both;
    }
    6 on the float, and don’t worry it will remain a block-level element.
  • The 3px Jog is when text that is up next to a floated element is mysteriously kicked away by 3px like a weird forcefield around the float. Quick fix: set a width or height on the affected text.
  • In IE 7, the Bottom Margin Bug was when if a floated parent has floated children inside it, bottom margin on those children is ignored by the parent. Quick fix: using bottom padding on the parent instead.

Alternatives

If you need text wrapping around images, there really aren’t any alternatives for float. Speaking of which, check out this rather clever technique for wrapping text around irregular shapes. But for page layout, there definitely are choices. Eric Sol has an article on A List Apart, Faux Absolute Positioning, which describes a very interesting technique that in many ways combines the flexibility of floats with the strength of absolute positioning.

CSS3 tackles page layout a couple of ways:

  • Flexbox
  • Multi-column Layout
  • Grid Layout

Absolutely positioned floats (e.g. you absolutely position as normal, but the element is still able to affect other elements, like have text wrap around it) were discussed, but I think the idea was shelved due to similarities to other more robust layout ideas.Video

Which property is used to move the HTML element to the right or left within its parent element?

If position: relative; - the right property sets the right edge of an element to a unit to the left/right of its normal position. If position: sticky; - the right property behaves like its position is relative when the element is inside the viewport, and like its position is fixed when it is outside.

Which property is used to set the element to one side of the page?

The left property affects the horizontal position of a positioned element.

What CSS property is used to position an element either right or left while other elements on the page wrap around it?

Essentially, the float property allows us to take an element, remove it from the normal flow of a page, and position it to the left or right of its parent element. All other elements on the page will then flow around the floated element.

What property is used to align elements on the right?

We can align elements using position property with some helper property left | right | top | bottom, which is used to set the element's coordinates. To align elements using position property, we set the element's position, and then using helper property, we set the coordinates of the element.