Code has been added to clipboard!

CSS Float: Learn to Make Elements Float to the Left or the Right

Reading time 3 min
Published Nov 2, 2016
Updated Jan 21, 2020

TL;DR — The CSS float property is for positioning elements to the left or the right side of their containers. Differently than position property, the floated element stays as a part of the flow of the page.

Float Left
Float Right

 

Making Elements Float

Elements with CSS float property affect the layout of other elements. The text content and images surround floated elements.

In the example, you see CSS float in action:

Example
.float-box {
    float: left;
    width: 200px;
    height: 100px;
    margin: 20px;
    border: 5px solid black; 
}  
	
.other-box {
   clear: left;
}

If you have several HTML elements inside one container, you need to determine how they float.

The float CSS property is commonly used for wrapping text around images. However, it can be utilized for other elements too.

There are four possible values for the CSS float property: 

  • left: elements float on the left side.
  • right: elements float on the right side.
  • none: elements do not float.
  • inline-start: elements float on the start side of the containing block.
  • inline-end: elements float on the end side of the containing block.

The example below makes CSS float images to the left of the text.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu eros eu arcu posuere rutrum vitae in magna. Sed et quam sit amet arcu tempor imperdiet at nec mauris. Mauris facilisis pretium velit, at sodales justo interdum vel.

Example
img {
   float: left;
}

The example below makes an image float to the right of the text.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu eros eu arcu posuere rutrum vitae in magna. Sed et quam sit amet arcu tempor imperdiet at nec mauris. Mauris facilisis pretium velit, at sodales justo interdum vel.

Example
img {
   float: right;
}

The example below prevents the element from floating:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam eu eros eu arcu posuere rutrum vitae in magna. Sed et quam sit amet arcu tempor imperdiet at nec mauris. Mauris facilisis pretium velit, at sodales justo interdum vel.

Example
img {
   float: none;
}

Imitating float: center

There is no property called CSS float: center. However, you can use a combination of properties to reach a similar result.

One option is to use display: flex and justify-content: center:

Example
.example1 {
  width: 100%;
  height: 100%;
  border: 2px solid #cc3f85;
  display: flex;
  justify-content: center;
}

.example2 {
  width: 50%;
  border: 2px solid #66d9ef;
}

Controlling Overflow

If the floating element is taller than the element containing it, then the floating element steps out of its container. You can fix this issue with the overflow property. Paired with an auto value, it stretches the container to be big enough for the floating element.

In the example, we fix the overflow problem using the overflow:auto method:

Without overflow: auto

In this example, the image is taller than the element containing it, and it is floated, so it overflows outside of its container.

With overflow: auto

Add a class with overflow: auto; to the containing element, to fix this issue.

Example
.clearfix {
    overflow: auto;
}

Note: the overflow property is not specifically designed for clearing floats. Use it carefully to avoid unwanted results.

The following method is preferred over the overflow:auto way of fixing this issue. It adds hidden content after the parent element (it clears the float).

Most modern websites use this strategy:

Example
.clearfix:after {
    content: ".";
    display: block;
    height: 0;
    clear: both;
    visibility: hidden;
}

CSS Float: Useful Tips

  • The CSS float property does not work if elements have display: none.
  • If the float property has inherit value, the element receives the float value from its parent.
Tutorial
CSS3 Features
Syntax
Classes
ID Selectors
Attribute Selectors
Stylesheets
Inline
Internal
External
Box Model
Children Selectors
Pseudo Classes
Pseudo Elements
Variables
Counters
Text
Fonts
Web Fonts
Backgrounds
Background Images
Colors
Gradients
Opacity / Transparency
Shadow Effects
Transitions
Tooltip
Transform
Animations
Layout — Display
Layout — Position
Layout — Float
Layout — Clear
Layout — Horizontal & Vertical Align
Multiple Columns
Introduction
Responsive Web Design — Viewport
Responsive Web Design — Grid View
Responsive Web Design — Media Queries
Responsive Web Design — Flexbox Layout
Responsive Web Design — Images
Responsive Web Design — Videos
Borders
Margin
Padding
Width
Height
Outline
Links
Lists
Tables
Dropdown Menu
Navigation Bar
Images
Image Gallery
Border Images
Forms
Rounded Corners
Buttons
Box-Sizing
Selector Reference
Pagination Examples
Code Examples
CSS3 Browser Support Reference
Functions Reference
Speech Module Reference
Units
Web Safe Font Combinations
Cheat Sheet
:hover
@font-face
@keyframes
@media
align-content
align-items
align-self
all
animation
backface-visibility
background
background-clip
background-color
background-image
background-origin
background-position
background-size
border
border-image
border-radius
border-style
box-shadow
box-sizing
color
columns
filter
flex
flex-basis
flex-direction
flex-flow
flex-grow
flex-shrink
flex-wrap
font
font-family
font-size
font-size-adjust
font-stretch
font-style
font-weight
hanging-punctuation
justify-content
line-height
margin
offset
opacity
order
outline
overflow
padding
perspective
position
resize
tab-size
text-align
text-decoration
text-emphasis
text-transform
text-shadow
transform
transition-property
translate
vertical-align
word-break
word-wrap
z-index