Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We briefly came in contact with the overflow property during the chapter on floats. In this movie I want to discuss the overflow property in a little bit more detail and how it can help us when dealing with positioned elements. To do that, I have the overflow.htm file opened up and if I look through the code of this file, after a section where we talk about the overflow property a little bit, we have this section with a class name of container and it has a lot of text in it. If I preview this in a browser, you can see that right now the text is just forming the height of the section itself, so we don't have a defined height on this section. So because of that, the height is actually being determined by the content, so there really isn't any overflow going on at all.
Now this is typical for your elements. If you don't declare a specific height, for example, the contents of the element forms the height. Now I want to go right back up here for just a minute and talk about the different values of the overflow property and what it can do for us. First off, we have to understand what it means for an element to be in overflow. If you have an element that you have a defined width and height on it and the content no longer fits within that element, it is considered to be overflow. Our properties here, visible, hidden, scroll, auto, and inherit, all describe what should happen to the content of an element where we have overflow.
Now visible basically says, okay, just display anyway. Even though it's going to overflow the content edge of the element, it doesn't matter; the content still needs to be visible. Hidden is going to basically clip the content so that any content that would normally overflow it is not visible. Scroll tells the browser to put scrollbars on the element, and that way if you do have overflow content, you can scroll through the content. It should be noted that scroll puts scrollbars on the content whether it needs it or not, which is where auto comes in.
Auto is your way of basically saying, hey, give me scrollbars if I need them. If I don't, then please don't. Then inherit obviously says whatever my parent element is doing. So let's go back to our code and trigger overflow so that we can see these properties in action. So I am going to go back into my code, I am going to scroll up to my styles, and where it says container here, I am going to add a height to this. And I am just going to give it a height of 300 pixels. Now that is not nearly tall enough to display this content. So if I save this, go back into the browser, and refresh the page, now if I scroll down into the content, you can see, the container no longer is able to contain all the content-- it's only 300 pixels tall--and the content is allowed to overflow outside the content width.
Okay, so you can see by looking at this that visible is the default for elements, so anytime overflow is triggered, if that property is not set explicitly, visible is considered to be the value. So if we go back into our code and we go to the styles of container, if I set overflow to visible and save it and refresh the page, we don't see any difference. Now on the other hand, if I change that value to hidden, save it, and refresh the page, you can see what happens to our overflow content.
It's trimmed off, and notice that the page is not even scrolling down that far anymore. That content is gone and will not affect content around it. Of course, the downside is you can't read that content anymore. Even if I go down and highlight and try to push it down, you can't see it. It's totally hidden. It's clipped off. Now if I go in and tell it to scroll, if I save that and preview, you can see that now over here on the right-hand side of this element, and this element only, I get scrollbars that allow me to scroll through the content.
This is one of the coolest things that we can do for positioned elements. Let's say, for example, that you have some type of a notice that you're going to need to position using absolute positioning and floating over other elements, or to the side. You may not be able to have a flexible height on that element. Perhaps you need it to be a specific size. But you're not 100% sure how much content is going to go inside of it. Well, now the overflow property allows me to go ahead and put scrollbars on the element if I need it, and that way the users can still read through all the content, even though it's restricted in size.
Now of course, there is a big difference between scroll and auto, and if I go back into the CSS, if I choose auto, notice that if I refreshed my page right now, we don't really see any change. We see a slight change, but really, scrollbars are still there. However, if I go and get rid of the height property, save that, and test it, notice that the scrollbars go away. They are no longer necessary, because the content is no longer overflowing. Of course, if you had scroll instead of auto, notice that you get the scrollbars, whether you need them or not.
So that sort of negatively impacts my element. So if you're not 100% certain whether you're going to need the scrollbars or not, then just go ahead and use auto. Just be aware of the fact that adding the scrollbars is going to impact the width of the element slightly, so if you are sort of planning for whether the scrollbars are going to be there or not, you need to account for them within your layout. So I think you can see pretty quickly here how handy the overflow property is when creating layouts, especially for positioned elements. In some layouts, position elements will be restricted to specific widths and heights, and by using the overflow property, you can control exactly how the browser displays any content that doesn't fit within those regions.
Get unlimited access to all courses for just $25/month.Become a member
58 Video lessons · 60025 Viewers
61 Video lessons · 92692 Viewers
82 Video lessons · 104187 Viewers
56 Video lessons · 107169 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.