The overflow property controls what happens when content is too big for its container. It helps keep your page neat and easy to read.
Overflow property in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
overflow: visible | hidden | scroll | auto;visible shows all content, even if it spills out.
hidden cuts off extra content without scrollbars.
Examples
CSS
overflow: visible;CSS
overflow: hidden;CSS
overflow: scroll;CSS
overflow: auto;Sample Program
This example shows four boxes with the same size but different overflow settings. You can see how the text behaves differently in each box.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Overflow Property Example</title> <style> .box { width: 12rem; height: 6rem; border: 2px solid #333; margin: 1rem; padding: 0.5rem; font-family: Arial, sans-serif; font-size: 1rem; } .visible { overflow: visible; background-color: #e0f7fa; } .hidden { overflow: hidden; background-color: #ffe0b2; } .scroll { overflow: scroll; background-color: #c8e6c9; } .auto { overflow: auto; background-color: #f3e5f5; } </style> </head> <body> <h1>Overflow Property Demo</h1> <p>Each box has fixed size. The text inside is longer than the box.</p> <div class="box visible"> <strong>overflow: visible;</strong><br /> This text is too long for the box and spills outside. </div> <div class="box hidden"> <strong>overflow: hidden;</strong><br /> This text is too long for the box but is cut off and hidden. </div> <div class="box scroll"> <strong>overflow: scroll;</strong><br /> This text is too long for the box and scrollbars always show. </div> <div class="box auto"> <strong>overflow: auto;</strong><br /> This text is too long for the box and scrollbars appear only if needed. </div> </body> </html>
Important Notes
Scrollbars with scroll always show, even if not needed, which can look cluttered.
auto is usually best for user-friendly scrolling.
Use hidden carefully because content can be lost and inaccessible.
Summary
The overflow property controls how extra content is handled inside a box.
Use visible, hidden, scroll, or auto depending on how you want to show or hide extra content.
Choosing the right overflow helps keep your page clean and easy to use.
Practice
1. What does the CSS
overflow property control in a webpage layout?easy
Solution
Step 1: Understand the role of overflow
The overflow property manages what happens when content is bigger than its container.Step 2: Match property to behavior
It controls if extra content is visible, hidden, or scrollable inside the box.Final Answer:
How extra content inside a box is shown or hidden -> Option AQuick Check:
Overflow controls extra content display [OK]
Hint: Overflow controls extra content visibility inside boxes [OK]
Common Mistakes:
- Confusing overflow with text color or font
- Thinking overflow changes box size
- Mixing overflow with border styles
2. Which of the following is the correct CSS syntax to hide overflow content inside a box?
easy
Solution
Step 1: Recall CSS property syntax
CSS uses property: value; format to set styles.Step 2: Identify correct syntax for overflow hidden
The correct way isoverflow: hidden;with colon and semicolon.Final Answer:
overflow: hidden; -> Option BQuick Check:
CSS uses colon between property and value [OK]
Hint: Remember CSS uses colon between property and value [OK]
Common Mistakes:
- Using equals sign instead of colon
- Missing colon or semicolon
- Combining property and value without separator
3. Given this CSS and HTML, what will you see in the browser?
<style>
.box {
width: 100px;
height: 50px;
overflow: scroll;
border: 1px solid black;
}
</style>
<div class='box'>This is a very long text that will not fit inside the box.</div>medium
Solution
Step 1: Analyze box size and overflow setting
The box is fixed at 100px by 50px withoverflow: scroll;.Step 2: Understand overflow: scroll behavior
This forces scrollbars to appear so user can scroll to see all content.Final Answer:
The box shows scrollbars to see hidden text -> Option CQuick Check:
Overflow scroll adds scrollbars [OK]
Hint: Overflow scroll always shows scrollbars [OK]
Common Mistakes:
- Thinking overflow scroll hides content
- Assuming box grows to fit text
- Confusing scroll with auto behavior
4. You want to hide extra content inside a fixed-size box but your CSS uses
overflow: visible;. What is the problem and how to fix it?medium
Solution
Step 1: Understand overflow: visible behavior
Overflow visible means extra content spills outside the box and is shown.Step 2: Fix by changing overflow to hidden
To hide extra content, useoverflow: hidden;which clips content inside the box.Final Answer:
Visible overflow shows extra content; change tohiddento hide it -> Option DQuick Check:
Visible shows overflow, hidden hides it [OK]
Hint: Visible shows overflow; hidden hides it [OK]
Common Mistakes:
- Thinking visible hides content
- Confusing visible with scroll or auto
- Assuming visible causes errors
5. You have a container with dynamic content that sometimes fits and sometimes overflows. You want scrollbars only when needed. Which overflow value should you use and why?
hard
Solution
Step 1: Understand dynamic content overflow needs
Content size changes, so scrollbars should appear only if needed.Step 2: Choose overflow: auto for conditional scrollbars
autoadds scrollbars only when content is too big, keeping layout clean otherwise.Final Answer:
overflow: auto;because it shows scrollbars only when content overflows -> Option AQuick Check:
Auto adds scrollbars only if needed [OK]
Hint: Use overflow auto for scrollbars only when needed [OK]
Common Mistakes:
- Using scroll which always shows scrollbars
- Using visible which never hides overflow
- Using hidden which hides overflow without scroll
