header-logo

CODE WITH FAHIM

CSS Overflow

In CSS, overflow controls what happens when the content of an element is too big to fit inside its container (like text or images spilling out of a box).
Here’s the basic idea: imagine you’ve got a div with a fixed width and height — if the content inside it goes beyond those limits, overflow decides whether to hide it, show scrollbars, or just let it spill out.

🌊 What “Overflow” Really Means:

When you set a width or height on an element, the browser draws a “box” for it (thanks to the CSS box model).
If the content inside that box — like text, images, or other elements — becomes larger than the box itself, that’s called overflow.

By default, the browser just lets that extra stuff spill out beyond the border. But with the overflow property, you take control.

Syntax:

overflow: visible | hidden | scroll | auto;


Values:

Value Description
visible Default. Content is not clipped — it overflows and remains visible outside the box.
hidden The overflow is clipped, and the extra content is not visible.
scroll The overflow is clipped, but scrollbars always appear, even if not needed.
auto The browser adds scrollbars only if necessary.



Details of overflow values:

1.Overflow:auto(Smart option)



Example:
.box { width: 200px; height: 100px; border: 2px solid #444; overflow: auto; } <div class="box"> <p>This is a long paragraph that overflows the box boundaries...</p> <div>


Output:

This is a long paragraph that overflows the box boundaries...



Here, if the text is too long, scrollbars will appear so you can scroll to see the rest.


You can also control horizontal and vertical overflow separately:

overflow-x: hidden; /* horizontal */ overflow-y: scroll; /* vertical */



2. overflow: visible; (Default)



Example:
.box { width: 200px; height: 100px; border: 2px solid red; overflow: visible; } <div class="box">➡️ You’ll see text or images popping out beyond the box — messy but sometimes intentional (for artistic effects).<div>


Output:
➡️ You’ll see text or images popping out beyond the box — messy but sometimes intentional (for artistic effects).



3. overflow: hidden;



Example:
.box { width: 200px; height: 100px; border: 2px solid green; overflow: hidden; } <div class="box">➡️ Great when you want to hide what’s overflowing without letting users scroll.(The text is hidden.)<div>


Output:
➡️ Great when you want to hide what’s overflowing without letting users scroll.The text is hidden


4.overflow: scroll;



Example:
.box { width: 200px; height: 100px; border: 2px solid blue; overflow: scroll; } <div class="box">➡️ Scrollbars appear even if your text fits perfectly — so use it sparingly.</div>


Output:

➡️ Scrollbars appear even if your text fits perfectly — so use it sparingly.



🎨 Real-World Uses of overflow: