CSS Float

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Javascript Course - Mastering the Fundamentals
Javascript Course - Mastering the Fundamentals
By Mrinal Bhattacharya
Free
star4.8
Enrolled: 1000
Javascript Course - Mastering the Fundamentals
Javascript Course - Mastering the Fundamentals
Mrinal Bhattacharya
Free
4.8
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Overview

Float is a CSS property that positions the element to the left or right in its parent container. It enables the text, images, or other inline elements to wrap around the floating element. Elements other than the floating element also become part of the flow. There is where clear property comes into action. It clears the area on either side of the floating element by specifying its position.

Scope

In this article, we will learn about the float property in CSS, and the flow of the topics will be as discussed below-

  • We'll start with a brief introduction of the float property in CSS, along with a real-life example of it.
  • Then, we will learn about the various values the float property can take and the examples of its uses to develop a better understanding.
  • Then, We'll learn about what does the float do in CSS? and how we can position float elements in CSS?
  • Lastly, We'll discuss how we can float elements in CSS and clear the float property in CSS.

Introduction

Every one of us must have read magazines and newspapers in our life. While skimming through the pages, you must have noticed the text wrapped in images for every section. There is a lot of text with pictures on their right, left, over, or below. It makes it look more attractive and easier to see both text and image simultaneously. It also saves a lot of blank space by wrapping the content. The same goes well for web pages. Like print media, websites also have texts wrapped around images. We apply the float property to make the images flow to the left or right edges, and the text wrap around them. css float example

But it's not only limited to texts and images. There is a lot more to explore and experiment with it.

Let's get into the depth of how we make this happen on our web pages by knowing about them one by one.

What Is Float In CSS?

Float is a CSS property that signifies the position of an element horizontally that is either left or right. Elements are not allowed to float vertically. There is no up and down float value. It can also take values like inherit, initial, and none, which we will discuss one by one later in the article.

The float property gets ignored by elements with absolute or fixed positioning. They are not a part of the flow of the web page. Absolutely positioned elements neither get affected by the position of other elements nor affect other elements.

Syntax

CSS float values

CSS Float property can take any of the four values :

  • right : The floating element is positioned on the right side of its parent container.

Example :

Output :

CSS Float Values In this example, we have floated the word "Scaler" to the right, and the remaining text gets wrapped around it.

  • left : The floating element is present on the left side of its parent container.

Example :

Output :

another example of CSS Float Values

Here, the first letter T embedded in a span is floated left, and all the text is wrapped around it.

  • inherit : It inherits its floating value from the parent element.

Example :

Output :

CSS Float Values example

  • none : The element does not float and is displayed where it appears in the text.

Example :

Output :

example of CSS Float Values

Here, the span element does not float to the sides and is displayed at the same place in the text.

How to use float in CSS ?

Float in CSS is used by writing the styling element and the defined float property.

Example of float : right

Output :

Example of float right

Example of float : left

Output :

Example of float left

What Does Float Do In CSS ?

The float property in CSS enables us to enfold elements around images. Besides wrapping text around the images, it creates engaging web designs, floating links of horizontal navbars, floating content sections, and texts.

How To Position Floating Elements ?

A floated element is out of the flow of the container though being a part of it. Being out of the flow here means it separates itself from the rest of the page by creating a mini layout. They don't appear in the order they are in the source.

Thus, they can only be positioned at the extreme left or right of the surrounding block. It floats to the extreme left or right until it encounters the edge of the container or a floating element. Floating elements are also aligned as high as possible.

Example :

Output :

positioning floating elements In this example, there are three colored divs. Two float towards the left, while the third is to the right. As we know, a floated element shifts to the left or right until it encounters the edge of the box or the next floating element. Thus the second left square is positioned to the right of the first. It would continue until they filled the containing box.

How Do You Float Multiple Elements In CSS?

We use the float property to float content boxes side by side.

Example :

Output :

float multiple elements in CSS

It also displays a grid of images side by side. Take the screen width as 100% and divide it by the number of images to get the container width of every image.

Example :

Output :

example of float multiple elements in CSS Here, we have floated four images side by side until they encounter the next floating element to their left or fill the container. We use the clearfix hack to take care of the layout flow and we add the box-sizing property to make sure that the image container doesn't break due to extra padding.

Example :

We can also create horizontal navbars using a set of floating links.

Output :

another example of float multiple elements in CSS

We can combine many CSS properties with float to create a web page layout. Here's how.

Output :

float multiple elements in CSS example We have created a horizontal menu by floating links. Then we created two columns for the side menu and the content. The content column floats to the right edge of the side menu filling the container. This creates a beautiful web layout only using CSS.

How To Clear The Float Property In CSS ?

Elements beside a floating element float around it. Here is where the clear property comes to the rescue. Clear property specifies the position of the element next to a floating element. It clears the floated elements on either side of the element and pushes it to the next line.

Clear property values

  • none : It is the default value that specifies that the next element is not pushed below the floating element.

  • left : The element is pushed below the left floating elements or the element having float: left property.

Output :

Clear Property Values

  • right : The element is pushed below the right floating element or element with float: right property.

Output :

Clear Property Values example

  • both : If there is left floating element, we have to clear its left and vice versa. Else the next element is not pushed below the floating element.

Here, comes the use of both. It pushes the element below both left and right floating elements.

Output :

example of Clear Property Values

  • inherit : The clear value gets inherited from the parent element to the current element.

Conclusion

  • Wrapping of text around images and other elements can be done using the float property. You can clear the flow of the elements that have become part of it using the clear property.

Learning how to use and clear the float property in CSS :

  • Helps you design clean layouts that captivate your audience.
  • Helps to maintain the flow of the web page.
  • Minimizes the white space and creates a maximum area for the relevant content.