Course contentsShow
← Web Development
Lesson 246 of 2,9824. CSS Layout FoundationsFree lesson

overflow-x and overflow-y for directional control

Control horizontal and vertical overflow independently with directional overflow properties.

overflow-x and overflow-y for directional control

You'll learn how to control horizontal and vertical overflow separately, giving you precise control over scrolling behavior in each direction.

The Core Idea

Instead of using the single overflow property that affects both directions equally, CSS provides overflow-x and overflow-y to control horizontal and vertical overflow independently. This means you can have horizontal scrolling while hiding vertical overflow, or vice versa.

Think of it like a camera viewfinder: overflow-x controls what happens when content extends left or right beyond your frame, while overflow-y handles content that extends up or down. You might want a horizontal scrollbar for a wide data table but never want vertical scrolling on that same element.

The Properties

.horizontal-scroll {
  overflow-x: auto;   /* Horizontal scrollbar when needed */
  overflow-y: hidden; /* Never show vertical overflow */
}

.vertical-only {
  overflow-x: hidden; /* Hide horizontal overflow */
  overflow-y: scroll; /* Always show vertical scrollbar */
}

Both properties accept the same values you learned previously: visible, hidden, scroll, and auto.

Common Use Cases

Horizontal scrolling galleries: Set overflow-x: auto and overflow-y: hidden to create sideways-scrolling image galleries.

Fixed-height chat boxes: Use overflow-y: auto with overflow-x: hidden to allow vertical scrolling while preventing horizontal overflow from breaking your layout.

Data tables: Apply overflow-x: auto to make wide tables scrollable without affecting the page's vertical scroll.

Key Takeaway: overflow-x and overflow-y let you control horizontal and vertical overflow independently, allowing precise directional scrolling behavior rather than applying the same overflow rule to both axes.