CSS- Overflow Property
In this article, we will go over the CSS property overflow.
What is overflow? And what does overflow do?
Let's paint an example. Imagine you have an element in HTML such as a div tag. This div tag area contains a specific width and height. Thus, it can only hold a certain amount of text within its space. After putting more text than the element can hold, what happens with the extra text that exceeds the amount of text the element can hold?
The CSS overflow property can decide what to do with this extra text. Using the different parameters, the overflow property can create a scroll bar so that extra text can be fitted in or overflow can cut off all extra text than above its limit so that nothing extra shows up.
We will go over all the overflow parameters below with illustrations so you can understand all the parameters and options which exist for this property.
The following chart represents all the different overflow parameters. These will all be explained below:
Summary of Overflow Parameters
|overflow: visible||This shows the overflow from the html element, as is. If there is overflow, it shows the spilling over of the text from the html element.|
|overflow: scroll||This displays vertical and horizontal scroll bars so that all overflow can be viewed from within the html element. Scroll bars will be shown whether there is overflow or not.|
|overflow-y: scroll||This displays only the vertical scroll bar.|
|overflow-x: scroll||This displays only the horizontal scroll bar.|
|overflow: auto||This creates a vertical scroll bar if there is overflow. If there is no overflow, no scroll bar is displayed.|
|overflow: hidden||This cuts off all extra text so that it cannot be seen in the browser. However, the information still exists in the html source code.|
Div Tag Without a Height Element
Before we go into each individual overflow parameter, let's create a div tag, since this is what we'll need to illustrate how overflow works.
The first example we will do is a div tag with only a width attribute, 200px, but with no height. We put a pretty lengthy paragraph in this div tag.
The HTML used to create this div block is:
The CSS code used to create this div block is:
Without a height attribute, the div tag will stretch to fit all the text which is entered between it.
In the case of a div tag with no height attribute, overflow will not occur and, thus, the overflow property will not be needed. Since there is no height attribute, there is no height limit. Thus, any text that is entered in between the div tag will make the div tag stretch out vertically, since it doesn't have a vertical (height) limit. The height will be flexible and adjust depending on the content that fills up the div tag.
Div With Height Element
But now let's say we need an element to only be a certain height and none taller, let's say, 200px. If we now specify an absolute height for it to be, now overflow can occur. Let's create the same div tag we did above but now let's make the div tag element 200px in height. The HTML code says the same, except change the div tag name from divnoheight to divheight.
The CSS code will for this div tag will now be:
This is how the element will look with the same paragraph, as above, placed in the div tag:
You can see now how the text spills over. There is text flowing outside of the boundaries of the div tag. This is called overflow and is the subject of this whole article.
By default, without an overflow property being stated in the CSS div tag, it is set to overflow:visible.
If you type in the line overflow:visible into the CSS code for the divheight, you will get the same exact overflow which you see above, with the text spilling over the boundaries of the div tag.
So, with the CSS code, being the following:
This would produce the same exact overflow we saw above:
Now we deal with how to effectively handle overflow so that it doesn't spill over in pages and cause that messy look.
One property we can apply to a div tag where there's overflow is:
This line of code will create vertical and horizontal scrollbars so that extra text exceeding the div tag's space is placed inside the div tag and a user can view all the text placed inside the tag by scrolling down to view it.
Let's see how the element we created above which overflowed previously would now look with this scroll attribute applied to the overflow property.
The full CSS code to create this is:
Thus, the scroll parameter to an overflow property is an effective means to handle overflow of an element.
Note that with the scroll parameter, the div tag will always contain a scroll bar, whether there is overflow or not. It will contain two scroll bars, a vertical scroll bar and a horizontal scroll bar. However, you can see that in the above case, the horizontal scroll bar is defunct and is not needed.
If you want, you can only show one scroll bar, the vertical scroll bar or the horizontal scroll bar, and not both.
Another feature we can add to the CSS code of an HTML element to prevent overflow is overflow:auto.
You will see this is even a more effective way to handle overflow than overflow:scroll for the most part.
The difference between overflow:auto and overflow:scroll is overflow:auto will only create a scroll bar when there is overflow. Overflow:scroll will always have a scroll bar whether there is overflow or not. Again, overflow:auto creates a scroll bar only when there is overflow. Overflow:auto will also take out the defunct horizontal scroll bar which you see above, which is not used. So basically, overflow:auto only adds in elements that are needed when they are needed, while overflow:scroll, by default, has everything always.
Here is how the above text would look with overflow:auto:
There is another overflow property that can be used to deal with overflow called overflow:hidden.
The following CSS code would be placed in the HTML element in which we want to hide extra text.
This cuts off text that exceeds the space in the div tag so that the extra text cannot be seen, even though it still exists.
Let's look at our above paragraph we've been using along with the hidden property applied to it.
The full CSS to create this is:
In certain cases, where you don't want any extra text or any scroll bars, you can apply overflow:hidden to a div tag. However, this is probably not want you want. However, if you
do need this, it is available.