CSS overflow property explained
Home - Tutorials - CSS tutorials
This article tries to teach you how to use the CSS overflow property in your web design.
Tutorial info:
| Name: | CSS overflow property explained |
| Total steps: | 1 |
| Category: | CSS tutorials |
| Date: | 2010-07-15 |
| Level: | Beginner |
| Product: | See complete product |
| Viewed: | 4112 |
Bookmark CSS overflow property explained
Step 1 - CSS overflow property
CSS overflow property explained
The css overflow property defines how to handle the internal content if it is bigger than the parent box itself.
Let’s suppose the following simple situation: You have a 100x100 div box and some line of text in the div as follows:

The HTML code which results the output above is this:
<style type="text/css">
.parent {
width:100px;
height:100px;
background-color: #aaa;
}</style><div class="parent">
This_is_a_demo_text_inside_the_div_box.<br/>This is a demo text inside the div box.<br/>
This is a demo text inside the div box.<br/>
</div>
As you can see the text doesn’t fit into the div. In other words it overflows it. Usually this is not what you want. There are two solution for this problem:
- Remove the width and height property of the div and so it’s size will increase to hold all of the content.

- Set the css overflow property to control the appearance of the internal content. The following values can be set for the overflow property:
- visible : This is the default value and results the output as seen on the first image.
- hidden : Any content outside of the parent box will be invisible.
- scroll : A scrollbar will be added to the parent box independent from the size of the internal content
![]()
- auto : A scrollbar will be added to the parent box but only if it is necessary. Note the difference between the images with only a short text.
![]()
- inherit : The value is inherited from it’s parent element overflow value.
Float property effect on default overflow behaviour
During the design of your layout it can happen that you set the float property of a child element. In this case the result will be a bit interesting Let’s see the following 2 code and their output:
This is the basic code. Now without float and overflow settings:
<style type="text/css">
.parent {
padding:5px;
background-color: #aaa;
}.child {
width:50px;
height:50px;
background-color: red;
}</style><div class="parent">
<div class="child"></div>
</div>
And it results the following output:

Now try to align the red box to the right using the float:right css syntax:
And the result is not what we probably want:

We can solve this problem by using the overflow property on the parent element and set it to hidden like here:
<style type="text/css">
.parent {
padding:5px;
background-color: #aaa;
overflow: hidden;
}.child {
width:50px;
height:50px;
background-color: red;
float:right;
}</style><div class="parent">
<div class="child"></div>
</div>
And now the result is what we want:

I hope this small description helps you to take advantage of the css overflow property opportunities.
Tags: CSS overflow propert, overflow property, css overflow, css, overflow
| CSS overflow property explained - Table of contents |
|---|
| Step 1 - CSS overflow property |



