CSS Tutorial for Beginners Part 22 - CSS float and clear(examples) | Learn Web Technologies

Опубликовано: 02 Июль 2026
на канале: Learn Web Technologies
25
3

CSS Playlist
   • CSS Tutorial for Beginners  

HTML Playlist
   • Html tutorial for beginners  

Sign up for my newsletter to receive weekly updates
http://bit.ly/2RRFktj

Join our facebook page
bit.ly/2OKQYV7

The float Property
The float property is used for positioning and formatting content e.g. let an image float left to the text in a container.

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 (will be displayed just where it occurs in the text). This is default
inherit - The element inherits the float value of its parent

The clear Property
The clear property specifies what elements can float beside the cleared element and on which side.

The clear property can have one of the following values:

none - Allows floating elements on both sides. This is default
left - No floating elements allowed on the left side
right- No floating elements allowed on the right side
both - No floating elements allowed on either the left or the right side
inherit - The element inherits the clear value of its parent
The most common way to use the clear property is after you have used a float property on an element.

When clearing floats, you should match the clear to the float: If an element is floated to the left, then you should clear to the left. Your floated element will continue to float, but the cleared element will appear below it on the web page.

The clearfix Hack
If an element is taller than the element containing it, and it is floated, it will "overflow" outside of its container:
Then we can add overflow: auto; to the containing element to fix this problem:
The overflow: auto clearfix works well as long as you are able to keep control of your margins and padding (else you might see scrollbars). The new, modern clearfix hack however, is safer to use, and the following code is used for most webpages:

Grid of Boxes / Equal Width Boxes
With the float property, it is easy to float boxes of content side by side:

What is box-sizing?
You can easily create three floating boxes side by side. However, when you add something that enlarges the width of each box (e.g. padding or borders), the box will break. The box-sizing property allows us to include the padding and border in the box's total width (and height), making sure that the padding stays inside of the box and that it does not break.
----------------------------------------------------------------------------------------------------------------

#LearnWebTechnologies #CSS #CSSTutorialForBeginners