Skip to main content

Create Equal Width Boxes

With the float property, it is easy to float boxes of content side by side.

What is box-sizing? When you add padding or borders, a floated box can break to a new line. The box-sizing property includes the padding and border inside the box's total width and height, so the layout does not break.

Example

Three boxes float side by side at one-third width each, with box-sizing: border-box keeping the padding inside.

Prompt
An HTML page that floats three equal-width boxes side by side using box-sizing. In an internal <style> block, set * box-sizing to border-box, then make a .box class that floats left, is 33.33% wide, has padding and a border. Add three boxes so they sit side by side without breaking the layout.
Output: complete HTML page

Images Side By Side

The same grid of floated boxes can be used to display images side by side.

Example

Three image containers float side by side at one-third width each.

Prompt
An HTML page that floats three image containers side by side. In an internal <style> block, set * box-sizing to border-box, then make an .img-container class that floats left, is 33.33% wide, with small padding. Put a colored box (image stand-in) inside each. Add three containers.
Output: complete HTML page

Create Boxes With Equal Heights

It is not easy to make floating boxes the same height when their content differs. A quick fix is to set a fixed height on the boxes.

Example

A fixed height of 500px keeps the floated boxes the same height even with different content.

Prompt
An HTML page that floats boxes side by side with equal heights, using a fixed height. In an internal <style> block, set * box-sizing to border-box, make a .box class that floats left at 33.33% width with padding and a border, and give the boxes a fixed height of 500px so they all match. Add two boxes with different amounts of text.
Output: complete HTML page

Navigation Menu

You can also use float to build a navigation menu, by floating the list items so the links sit next to each other. PromptCookbook links to a full example for this.


All CSS Float Properties

A summary of the float-related properties:

  • box-sizing — sets how the total width and height of an element are calculated.
  • clear — sets what happens with the element next to a floated element.
  • float — sets whether and how an element floats.
  • overflow — sets what happens when content overflows an element.