The behavior you're looking for – overflow-y: scroll and overflow-x: visible where the visible content actually spills out – is unfortunately not directly possible on a single element due to how overflow properties interact.
Here's why:
According to the CSS spec, if one of overflow-x or overflow-y is set to scroll (or auto) and the other is visible, the visible value will effectively behave as auto or scroll to avoid ambiguity and ensure consistent rendering. This means your horizontally overflowing content would still be clipped or become scrollable, rather than truly spilling out.
The Solution: Use Nested Elements
To achieve this correctly, you need to use two nested elements:
An outer-wrapper: This element will define the overall width and height of your visible area and will be responsible for allowing the horizontal content to spill out (overflow-x: visible). It must not have overflow-y: scroll.
An inner-scroll-area: This element will be contained within the outer-wrapper and will be responsible for handling the vertical scrolling (overflow-y: scroll). Its horizontal overflow should be visible (which is the default, so you often don't need to explicitly set overflow-x: visible on it).
How to Implement This Correctly
HTML Structure:
html
<div class="outer-wrapper">
<div class="inner-scroll-area">
<div class="content">
<p>This content is narrow enough vertically, but very wide horizontally. It should spill out to the right.</p>
<p>This is more content to make the vertical scrollbar appear.</p>
<p>More lines...</p>
<p>Even more lines...</p>
<p>Keep adding lines until it needs to scroll vertically.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. This is a very long line to demonstrate horizontal overflow.</p>
<p>Last line for vertical scroll.</p>
</div>
</div>
</div>
CSS:
css
.outer-wrapper {
/* This defines the overall visible frame for your content */
width: 300px; /* Desired visible width */
height: 200px; /* Desired visible height */
border: 2px solid #3498db; /* To visualize the wrapper boundary */
overflow-x: visible; /* Allows horizontal content to spill out */
/* Important: Do NOT set overflow-y: scroll here, it would clip or make visible behave as auto */
overflow-y: hidden; /* Or visible, it won't matter as the inner div handles y-scrolling */
/* Optional: Background or other styling for the outer frame */
background-color: #ecf0f1;
}
.inner-scroll-area {
/* This element handles the vertical scrolling */
width: 100%; /* It takes the full width of its parent (.outer-wrapper) */
height: 100%; /* It takes the full height of its parent (.outer-wrapper) */
overflow-y: scroll; /* This makes the vertical scrollbar appear */
overflow-x: visible; /* (Default) Allows its children to spill horizontally. Crucial that this is not hidden or scroll. */
/* If you added a border here, you'd see it inside the outer-wrapper,
and it would only clip the content if its overflow-x was not visible. */
}
.content {
/* This is your actual content */
background-color: #a2d2ff; /* To visualize the content area */
min-height: 1px; /* Ensure it has some height even if empty */
padding: 10px;
box-sizing: border-box;
/* Make some content wider than the outer-wrapper */
/* You can make a specific line or div wider, or let content naturally push */
/* For explicit horizontal overflow, you can set a min-width on a child element
or rely on very long non-breaking text. */
white-space: pre-wrap; /* Allows text to wrap, but the long line will still overflow */
}
/* Example to make a specific line/element within .content very wide */
.content p:last-of-type {
width: 500px; /* This makes the content overflow horizontally */
background-color: #ffcccc;
border: 1px dashed red;
}
/* For demonstration purposes, position other elements nearby to see the spill */
body {
padding: 50px;
background-color: #f0f0f0;
}
.sibling-element {
margin-top: 20px;
width: 400px;
height: 50px;
background-color: #d1e7dd;
border: 1px solid #28a745;
padding: 10px;
}
Explanation:
.outer-wrapper: This is your window. It has a fixed width and height. By setting overflow-x: visible, you are telling it, "If my children extend past my horizontal boundaries, let them show!" It does not manage vertical scrolling.
.inner-scroll-area: This element is positioned directly inside the outer-wrapper and takes up 100% of its parent's width and height. Critically, it has overflow-y: scroll;. This is where the vertical scrollbar appears. Because its overflow-x is visible (the default value, so you don't always need to explicitly write it), it allows its own children (the .content div) to extend horizontally beyond its own boundaries.
.content: This is your actual content. If its height exceeds the .inner-scroll-area's height, the vertical scrollbar will activate. If its width exceeds the .outer-wrapper's width (because the .inner-scroll-area allows it), it will spill out horizontally.
Key Considerations:
z-index: If the horizontally overflowing content needs to appear above other elements on your page, you might need to use z-index on the outer-wrapper (or the overflowing content itself) to ensure it's rendered on top.
Ancestor Clipping: Make sure no ancestor of your .outer-wrapper has overflow: hidden (or overflow-x: hidden) set, otherwise, it will clip the horizontally overflowing content regardless of your outer-wrapper's settings.
Performance: For very large amounts of content, especially with many widely varying widths, consider performance implications. This nested overflow approach is generally efficient for typical use cases.