These CSS values control how extra content inside a box is shown. They help keep your page neat and easy to use.
Hidden, scroll, auto 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: hidden | scroll | auto;hidden hides extra content without scrollbars.
scroll always shows scrollbars, even if not needed.
auto shows scrollbars only if content is too big.
Examples
CSS
overflow: hidden;CSS
overflow: scroll;CSS
overflow: auto;Sample Program
This page shows three boxes with the same content but different overflow settings. You can try to scroll inside each box or use keyboard focus (Tab key) to see how scrollbars behave.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Overflow Example</title> <style> .box { width: 15rem; height: 7rem; border: 2px solid #333; margin: 1rem; padding: 0.5rem; font-family: Arial, sans-serif; font-size: 1rem; } .hidden { overflow: hidden; background-color: #f8d7da; } .scroll { overflow: scroll; background-color: #d1ecf1; } .auto { overflow: auto; background-color: #d4edda; } </style> </head> <body> <h1>Overflow: hidden, scroll, auto</h1> <section> <article class="box hidden" tabindex="0" aria-label="Box with overflow hidden"> <strong>Hidden:</strong> Extra text is cut off and no scrollbars appear. Try to scroll here. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </article> <article class="box scroll" tabindex="0" aria-label="Box with overflow scroll"> <strong>Scroll:</strong> Scrollbars always show, even if not needed. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </article> <article class="box auto" tabindex="0" aria-label="Box with overflow auto"> <strong>Auto:</strong> Scrollbars appear only if content is too big. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. </article> </section> </body> </html>
Important Notes
Use tabindex="0" on scrollable boxes to allow keyboard users to focus and scroll them.
Scrollbars look different on each operating system and browser.
For horizontal and vertical control separately, use overflow-x and overflow-y.
Summary
hidden hides extra content with no scrollbars.
scroll always shows scrollbars.
auto shows scrollbars only when needed.
Practice
1. What does the CSS property
overflow: hidden; do to extra content that doesn't fit in a container?easy
Solution
Step 1: Understand
This property hides any content that goes beyond the container's size without showing scrollbars.overflow: hidden;behaviorStep 2: Compare with other overflow values
Unlikescrollorauto, it does not provide any way to scroll to hidden content.Final Answer:
It hides the extra content without showing scrollbars. -> Option DQuick Check:
hiddenhides overflow [OK]
Hint: Hidden means no scrollbars, just cut off content [OK]
Common Mistakes:
- Thinking hidden shows scrollbars
- Confusing hidden with auto
- Assuming content resizes container
2. Which of the following is the correct CSS syntax to always show scrollbars on overflow?
easy
Solution
Step 1: Recall CSS overflow values
scrollalways shows scrollbars regardless of content size.Step 2: Verify syntax correctness
The syntaxoverflow: scroll;is valid and forces scrollbars.Final Answer:
overflow: scroll; -> Option BQuick Check:
scrollalways shows scrollbars [OK]
Hint: Scroll means scrollbars always visible [OK]
Common Mistakes:
- Using auto instead of scroll
- Confusing hidden with scroll
- Writing invalid property names
3. Given this CSS and HTML:
What will the user see in the browser?
div {
width: 100px;
height: 100px;
overflow: auto;
border: 1px solid black;
}
This is a very long text that will overflow the box and may require scrolling.
What will the user see in the browser?
medium
Solution
Step 1: Understand
This value shows scrollbars only when content is bigger than the container.overflow: auto;behaviorStep 2: Analyze the content size
The text is longer than 100px width and height, so scrollbars will appear.Final Answer:
Scrollbars appear only if content overflows. -> Option CQuick Check:
autoshows scrollbars if needed [OK]
Hint: Auto means scrollbars only if content is too big [OK]
Common Mistakes:
- Thinking auto always shows scrollbars
- Assuming content resizes container
- Confusing auto with hidden
4. You want to hide overflow content but accidentally wrote
overflow: auto hidden; in your CSS. What will happen?medium
Solution
Step 1: Understand CSS overflow shorthand
overflowcan take one or two values: first for horizontal, second for vertical overflow.Step 2: Analyze two-value syntax
auto hiddenis valid shorthand: horizontal = auto, vertical = hidden.Final Answer:
The first valueautosets horizontal overflow,hiddensets vertical overflow. -> Option AQuick Check:
Two-value overflow sets horizontal and vertical separately [OK]
Hint: Two values set horizontal and vertical overflow separately [OK]
Common Mistakes:
- Thinking invalid syntax causes error
- Assuming both values apply as one
- Ignoring two-value overflow shorthand
5. You have a container with fixed width and height. You want to ensure that if content overflows horizontally, a scrollbar appears, but vertically overflow content is hidden without scrollbars. Which CSS is correct?
hard
Solution
Step 1: Identify horizontal and vertical overflow needs
Horizontal overflow needs scrollbars, vertical overflow should hide content.Step 2: Use directional overflow properties
overflow-x: scroll;forces horizontal scrollbars,overflow-y: hidden;hides vertical overflow.Step 3: Check other options
overflow: scroll;shows scrollbars both directions;overflow: auto;shows scrollbars only if needed both directions;overflow-x: hidden; overflow-y: scroll;reverses the requirement.Final Answer:
overflow-x: scroll; overflow-y: hidden; -> Option AQuick Check:
Directional overflow controls scrollbars separately [OK]
Hint: Use overflow-x and overflow-y for separate scroll control [OK]
Common Mistakes:
- Using single overflow property for different directions
- Confusing scroll and auto
- Forgetting to hide vertical overflow
