Aral Code

Layout Lesson 18 of 33

CSS Overflow, Float and Object-fit

Control content that doesn't fit with overflow, wrap text around images with float, and crop images neatly with object-fit.

Sometimes content doesn't fit its box: a long post in a small card, a wide table, a tall photo. And sometimes you want text to flow around an image, like in a magazine. This lesson covers three tools for these situations: overflow, float and object-fit.

overflow

When content is bigger than its box (usually because you gave the box a fixed height), it overflows. The overflow property decides what happens:

  • visible — the content spills outside the box (default)
  • hidden — the extra content is cut off
  • scroll — scrollbars always appear
  • auto — scrollbars appear only when needed (usually the best choice)
Example Overflow values
<style>
  .box { width: 220px; height: 90px; border: 2px solid #0f5c4d; padding: 8px; margin-bottom: 18px; font: 14px Arial, sans-serif; background: #f3f6f4; display: inline-block; vertical-align: top; margin-right: 10px; }
</style>
<div class="box" style="overflow: visible;"><strong>visible:</strong> Panatang Makabayan. Iniibig ko ang Pilipinas, aking lupang sinilangan, tahanan ng aking lahi.</div>
<div class="box" style="overflow: hidden;"><strong>hidden:</strong> Panatang Makabayan. Iniibig ko ang Pilipinas, aking lupang sinilangan, tahanan ng aking lahi.</div>
<div class="box" style="overflow: auto;"><strong>auto:</strong> Panatang Makabayan. Iniibig ko ang Pilipinas, aking lupang sinilangan, tahanan ng aking lahi.</div>

You can control each direction with overflow-x (sideways) and overflow-y (up and down). You used overflow-x: auto in the Tables lesson to let wide tables scroll on phones.

float

float: left or float: right pushes an element to one side and lets text wrap around it, like a photo in a newspaper article.

Example Text wrapping around an image
<style>
  .photo {
    float: left;
    width: 120px;
    margin: 0 14px 8px 0;
    border-radius: 10px;
  }
  p { font-family: Georgia, serif; line-height: 1.6; }
</style>
<img class="photo" src="/img/jeepney.svg" alt="Jeepney">
<p>The jeepney is one of the most recognizable symbols of the Philippines. The first jeepneys were made from US military jeeps left after World War II. Filipino builders lengthened them, added benches, and decorated them with bright paint, chrome horses and names painted on the sides. For decades, they have carried millions of commuters every day, and are often called the "King of the Road."</p>

The margin on the image keeps the text from touching it. Try changing float: left to float: right.

Clearing floats

Floated elements can "hang out" of their container, and the next section might wrap around them when you don't want it to. The clear property stops this: an element with clear: both starts below any floats.

footer { clear: both; }

To make a container fully wrap its floated children, a modern one-liner works:

.container { display: flow-root; }

object-fit: cropping images neatly

When you force an image into a box with a different shape, say a tall photo into a square, it gets squished or stretched. object-fit fixes this, just like background-size does for background images:

  • fill — stretch to fit (default, may distort)
  • cover — fill the box and crop the extra (most common)
  • contain — fit the whole image inside, with empty space if needed
Example object-fit values
<style>
  img { width: 140px; height: 140px; border: 2px solid #0f5c4d; background: #fdf3dc; margin: 4px; }
  span { display: inline-block; text-align: center; font: 13px monospace; }
</style>
<span><img src="/img/jeepney.svg" alt="" style="object-fit: fill;"><br>fill</span>
<span><img src="/img/jeepney.svg" alt="" style="object-fit: cover;"><br>cover</span>
<span><img src="/img/jeepney.svg" alt="" style="object-fit: contain;"><br>contain</span>

object-fit: cover is how photo galleries and profile pictures stay neat even when the original photos are all different sizes. Use object-position (like top or center) to choose which part stays visible when cropped.

aspect-ratio

aspect-ratio keeps a box at a certain shape. aspect-ratio: 16 / 9 makes a widescreen box; 1 / 1 makes a square. Combined with object-fit: cover, it gives you uniform image cards:

Example Uniform photo cards
<style>
  .grid { display: flex; gap: 10px; }
  .grid img { width: 30%; aspect-ratio: 1 / 1; object-fit: cover; border-radius: 12px; background: #fdf3dc; }
</style>
<div class="grid">
  <img src="/img/mango.svg" alt="Mango">
  <img src="/img/jeepney.svg" alt="Jeepney">
  <img src="/img/halo-halo.svg" alt="Halo-halo">
</div>

Quick Quiz

Check what you learned. Walang grade ito, practice lang. 🙂

1. Which overflow value shows scrollbars only when they are needed?
2. What is float best used for today?
3. A tall photo looks squished in a square box. Which property fixes it?
4. Which property makes an element start below any floated elements?

By Sir Reyel · Last updated 2026-12-04