Essential CSS
JavaScript Roadmap
Learn JavaScript
Mastering Web Typography
Practical Astro
Understanding Async JS
Unorthodox Tailwind
API Masterclass
Build and Deploy
The Beginner Freelancer
Real World Node
Profile Billing Logout
Essential CSS
Lesson Plan
  1. Introduction
    1. Welcome to Essential CSS
    2. Getting started with the course
    3. What are file formats?
    4. Web standards
    5. What is HTML?
    6. What is CSS?
    7. Devtools, your indispensable best friend
  2. CSS Key Concepts
    1. Some background about CSS
    2. CSS Syntax and Terminology
    3. Selectors
    4. Cascade and Specificity
    5. Origin and Importance
    6. Inheritance
    7. How browsers handle CSS
  3. Basic Mental Model
    1. Everything on the web is a box
    2. The box model
    3. How big is your box?
    4. The relationship between px and em
    5. The `box-sizing` property
    6. The `display` property
    7. Formatting contexts
    8. Block boxes
    9. Inline boxes
    10. What is inline-block?
  4. Flexbox and Grid
    1. The parent-child relationship
    2. Sizing in Flexbox
    3. Flex directions
    4. Basic grid concepts
    5. Setting up a grid
    6. Placing grid items
    7. Box alignment
  5. Building web layouts
    1. Arranging your boxes on the web
    2. Positioning schemes
    3. Floating boxes
  6. Typography
    1. Making your text look good
    2. What is a font?
    3. Typesetting with CSS

Floating boxes

2m:12s

Before Flexbox and Grid became widely supported across all the different browsers, web developers relied on the float property extensively for their different layout needs.

Floats were never meant to be used in this manner, however, it was the best tool developers had at the time, so we made do. It wasn’t straightforward, and often required some precise calculations to get things working just right across different viewports.

These days, float-based layouts are almost obsolete, but that does not mean the float property itself should no longer be used. Rather, it can now return to its intended purpose, which is to “float” content all the way to the left or right while allowing other content to flow around it.

In the previous lesson, you learned about boxes being in normal flow, and boxes being out of flow.

Boxie friends demonstrating normal flow and out-of-flow

There is a third type of behaviour for floated boxes. It was most likely introduced to mimic a commonly-seen printed page layout where text flows around “floating̦” images.

Boxie friends demonstrating floats

Basics of float

Unlike fixed or absolutely positioned boxes which are taken out of normal flow, floated boxes are still part of normal flow. A floated box is shifted all the way to the edge its containing block. Other content will then flow down either the left or right side of this floated box, depending on which edge it’s floated to, and the amount of space available next to it.

A great use case for float would be a long form article that contains images and text. The markup structure might look something like this:

<article>
<p>Cupcake ipsum dolor sit amet soufflé…</p>
<img
src="http://www.cupcakeipsum.com/src/assets/essential-css/green_muffin.png"
class="left-img"
/>
<p>Marzipan caramels sesame snaps wafer brownie…</p>
<p>Lollipop jelly sugar plum sweet marzipan caramels…</p>
<img
src="http://www.cupcakeipsum.com/src/assets/essential-css/blue_muffin.png"
class="right-img"
/>
<p>Croissant pie danish pastry dessert…</p>
<p>Soufflé danish halvah danish tart jelly-o…</p>
</article>

Where the images are interspersed with the text. Without any additional styling, both the images and text will stack on top of one another because the p elements are block elements.

Screenshot showing the p elements and img elements stacked vertically

If we float the first image to the left and the second image to the right by applying the float: left and float: right properties on them respectively, you will end up with a layout that looks like this:

Screenshot showing how text flows around the 2 floated images

As you can see from the screenshot above, the text in the element directly below the floated images are aligned with the top of the images.