CSS Float
The CSS float property specifies how an element should float.
CSS Float
The float property specifies how an element should float.
The float property has the following values:
left- The element floats to the left of its containerright- The element floats to the right of its containernone- The element does not float (will be displayed just where it occurs in the text). This is defaultinherit- The element inherits the float value of its parent
Example 1: Float Right
In this example, the image will float to the right in the paragraph.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa.
Example 2: Float Left
In this example, the image will float to the left in the paragraph.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa.
Example 3: No Float
In this example, the image will not float.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa.
Example 4: Clear
The clear property specifies what elements can float beside the cleared element and on which side.
Without clear:
With clear:
Example 5: The Clearfix Hack
If an element is taller than the element containing it, and it is floated, it will overflow outside of its container. We can fix this using the "clearfix hack".
Without clearfix:
Lorem ipsum dolor sit amet.
With clearfix:
Lorem ipsum dolor sit amet.