Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What are Floats in CSS?

In this blog post, we will talk about a concept in CSS called "floats," which is crucial for controlling the layout of elements on a web page. If you're new to programming or web development, don't worry! We'll try to avoid jargon and explain things in simple terms with real-life examples to help you understand.

What is CSS?

Before we dive into floats, let's quickly cover what CSS is. CSS stands for Cascading Style Sheets. It is a language used for describing the look and formatting of a document written in HTML (Hypertext Markup Language). In simple words, HTML is like the skeleton of a web page, and CSS is the skin that makes it look beautiful.

What are Floats?

Floats, in the context of CSS, are a property that allows you to "float" an element to the left or right of its container. This means that other elements will wrap around the floated element, filling the space beside it. Floats are often used for creating multi-column layouts or placing images next to text.

Imagine you're arranging a room with a table and chairs. You can think of the table as the floated element, and the chairs as the other elements on the web page. If you "float" the table to the left, the chairs will move to the right and fill the space beside the table.

Let's illustrate this with some code. First, let's create a simple web page with three div elements inside a container div:

<!DOCTYPE html>
<html>
<head>
<style>
  .container {
    width: 100%;
    border: 1px solid black;
  }

  .box {
    width: 100px;
    height: 100px;
    border: 1px solid red;
  }
</style>
</head>
<body>

<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

</body>
</html>

By default, these div elements will display as block elements, meaning they will stack on top of each other. Now, let's use the float property to make them float to the left:

.box {
  float: left;
  width: 100px;
  height: 100px;
  border: 1px solid red;
}

With this change, the three div elements will now sit side-by-side, floating to the left of the container. You can also float elements to the right by setting the float property to right.

Clearing Floats

When using float, you might notice that the parent container doesn't automatically adjust its height to accommodate the floated elements. This is because the parent container doesn't consider the height of the floated elements when calculating its height.

To fix this, you can use the clear property. The clear property specifies which sides of an element other floating elements are not allowed. In other words, it forces the element to start on a new line, below any floated elements.

Let's add a new div element to our previous example and clear the floats:

<!DOCTYPE html>
<html>
<head>
<style>
  .container {
    width: 100%;
    border: 1px solid black;
  }

  .box {
    float: left;
    width: 100px;
    height: 100px;
    border: 1px solid red;
  }

  .clear {
    clear: both;
  }
</style>
</head>
<body>

<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
  <div class="clear"></div>
</div>

</body>
</html>

Now, the parent container will correctly adjust its height to include the floated elements.

Floats vs. Flexbox and Grid

While floats were once the go-to method for creating multi-column layouts, modern CSS has introduced new layout techniques like Flexbox and Grid, which are more powerful and easier to use. Floats can still be useful in specific situations, but it's essential to be aware of these newer techniques.

Flexbox

Flexbox, short for Flexible Box Layout, is a CSS layout mode that allows you to create complex layouts with less code and greater flexibility. With Flexbox, you can create one-dimensional layouts, either in rows or columns, and easily adjust the size and position of the elements within the layout.

Here's an example of how you can create the same three-column layout using Flexbox:

<!DOCTYPE html>
<html>
<head>
<style>
  .container {
    display: flex;
    width: 100%;
    border: 1px solid black;
  }

  .box {
    width: 100px;
    height: 100px;
    border: 1px solid red;
  }
</style>
</head>
<body>

<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

</body>
</html>

As you can see, the Flexbox solution is more straightforward and doesn't require any additional clearing elements.

Grid

The CSS Grid Layout is another layout technique that allows you to create two-dimensional layouts with rows and columns. It is more powerful than Flexbox and can handle complex layouts with ease.

Here's an example of how you can create the same three-column layout using CSS Grid:

<!DOCTYPE html>
<html>
<head>
<style>
  .container {
    display: grid;
    grid-template-columns: repeat(3, 100px);
    grid-gap: 10px;
    width: 100%;
    border: 1px solid black;
  }

  .box {
    height: 100px;
    border: 1px solid red;
  }
</style>
</head>
<body>

<div class="container">
  <div class="box"></div>
  <div class="box"></div>
  <div class="box"></div>
</div>

</body>
</html>

In this example, we use the display: grid property to create a grid layout and define three 100px-wide columns using the grid-template-columns property.

Conclusion

In this blog post, we've covered the basics of floats in CSS, including how to use the float property, how to clear floats, and how floats compare to modern layout techniques like Flexbox and Grid. While floats can still be useful in specific situations, it's essential to be familiar with more powerful and flexible layout techniques like Flexbox and Grid for creating complex web page layouts. Happy coding!