CSS Basics: Dive into the Float Model – Unveiling Secrets to Elevate Your Web Design Game

Deutsch

The CSS Float Model Explained

Normally, block elements are not placed side by side, but rather one below the other. They always start on a new line and occupy the full width of their parent block. If you would like to place multiple block elements side by side, you can use the CSS float property to do so.

Floated elements, or floats for short, are different from anything else in CSS. Floats are technically block elements, but they behave like inline elements in the sense that they (often) don't start on a new line. The rest of the content will try to flow around it.

CSS Property float

Description Possible Values Default Value Category
element flow left
right
none
initial
inherit
none positioning

The value shown in orange is the standard use of the CSS float property.

The functionality of the CSS float property is not really particularly complicate. But when floating elements, problems can arise that are not always easy to understand, especially for beginners.

The float property is used to specify the direction in which an element is shifted. The following values are allowed:

Alternative to float

Use display: inline-block to create a box that combines the behavior of inline and block elements. Inline elements are placed on the baseline like text. They have spacing as the words in the text. The margin and padding properties can be used to customize the spacing of the elements.

Example CSS
/* image container */
figure {
  /* display like an inline element */
  display: inline-block;
}


/* define spacing and width */
figure, figcaption {
  /* no outer outer spacing, all sides */
  margin: 0;

  /* no inner spacing, all sides */
  padding: 0;
  /* define width */
  width: calc(33% - 0.25em);
}


/* set image size */
figure img {
  /* image width: 100% of figure */
  width: 100%;
}
Example HTML
<div class="inline-pics">
<figure>
<img src="/images/godafoss.jpg" alt="Godafoss, Island">
<figcaption>Bildquelle: Pixabay.com<br>Godafoss, Island</figcaption>
</figure>

<figure>
<img src="/images/island.jpg" alt="Island">
<figcaption>Bildquelle: Pixabay.com<br>Island</figcaption>
</figure>

<figure>
<img src="/images/wasserfall.jpg" alt="Wasserfall">
<figcaption>Bildquelle: Pixabay.com<br>Wasserfall</figcaption>
</figure>
</div>
Output of the HTML code
Godafoss, Island
Bildquelle: Pixabay.com
Godafoss, Island
Island
Bildquelle: Pixabay.com
Island
Wasserfall
Bildquelle: Pixabay.com
Wasserfall

This background       represents the screen and is not part of the sample code.

Did you notice the spacing between the three images? Although the inner and outer spacing is set to 0, there is a small gap between the images. This is because there is a space between the <figure> tags. This is comparable to the spaces that separate the words in a text.