简体   繁体   English

CSS溢出-x:可见; 和溢出-y:隐藏; 导致滚动条问题

[英]CSS overflow-x: visible; and overflow-y: hidden; causing scrollbar issue

Suppose you have some style and the markup:假设您有一些样式和标记:

 ul { white-space: nowrap; overflow-x: visible; overflow-y: hidden; /* added width so it would work in the snippet */ width: 100px; } li { display: inline-block; }
 <div> <ul> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> <li>1</li> <li>2</li> <li>3</li> <li>4</li> <li>5</li> <li>6</li> <li>7</li> <li>8</li> <li>9</li> </ul> </div>

When you view this.当你看到这个。 The <ul> has a scroll bar at the bottom even though I've specified visible and hidden values for overflow x/y. <ul>在底部有一个滚动条,尽管我已经为溢出 x/y 指定了可见和隐藏值。

(observed on Chrome 11 and opera (?)) (在 Chrome 11 和 Opera (?) 上观察到)

I'm guessing there must be some w3c spec or something telling this to happen but for the life of me I can't work out why.我猜肯定有一些 w3c 规范或某些东西告诉这种情况发生,但对于我的生活,我无法弄清楚为什么。

JSFiddle JSFiddle

UPDATE:- I found a way to achieve the same result by adding another element wrapped around the ul .更新:-我找到了一种通过添加另一个包裹在ul周围的元素来实现相同结果的方法。 Check it out.一探究竟。

After some serious searching it seems i've found the answer to my question:经过一番认真的搜索,我似乎找到了我的问题的答案:

from: http://www.brunildo.org/test/Overflowxy2.html来自: http ://www.brunildo.org/test/Overflowxy2.html

In Gecko, Safari, Opera, 'visible' becomes 'auto' also when combined with 'hidden' (in other words: 'visible' becomes 'auto' when combined with anything else different from 'visible').在 Gecko、Safari、Opera 中,当与“隐藏”组合时,“可见”也变为“自动”(换句话说:当与“可见”不同的任何其他内容组合时,“可见”变为“自动”)。 Gecko 1.8, Safari 3, Opera 9.5 are pretty consistent among them. Gecko 1.8、Safari 3、Opera 9.5 非常一致。

also the W3C spec says: W3C 规范也说:

The computed values of 'overflow-x' and 'overflow-y' are the same as their specified values, except that some combinations with 'visible' are not possible: if one is specified as 'visible' and the other is 'scroll' or 'auto', then 'visible' is set to 'auto'. 'overflow-x' 和 'overflow-y' 的计算值与它们的指定值相同,除了某些与 'visible' 的组合是不可能的:如果一个被指定为 'visible' 而另一个是 'scroll'或“自动”,然后“可见”设置为“自动”。 The computed value of 'overflow' is equal to the computed value of 'overflow-x' if 'overflow-y' is the same;如果 'overflow-y' 相同,'overflow' 的计算值等于 'overflow-x' 的计算值; otherwise it is the pair of computed values of 'overflow-x' and 'overflow-y'.否则它是'overflow-x'和'overflow-y'的一对计算值。

Short Version:精简版:

If you are using visible for either overflow-x or overflow-y and something other than visible for the other, the visible value is interpreted as auto .如果您对overflow-xoverflow-y使用visible ,而另一个不是visible ,则visible值被解释为auto

another cheap hack, which seems to do the trick:另一个便宜的黑客,这似乎可以解决问题:

style="padding-bottom: 250px; margin-bottom: -250px;" on the element where the vertical overflow is getting cutoff, with 250 representing as many pixels as you need for your dropdown, etc.在垂直溢出被截断的元素上, 250代表下拉列表所需的像素数等。

I originally found a CSS way to bypass this when using the Cycle jQuery plugin.在使用 Cycle jQuery 插件时,我最初发现了一种绕过此问题的 CSS 方法。 Cycle uses JavaScript to set my slide to overflow: hidden , so when setting my pictures to width: 100% the pictures would look vertically cut, and so I forced them to be visible with !important and to avoid showing the slide animation out of the box I set overflow: hidden to the container div of the slide. Cycle 使用 JavaScript 将我的幻灯片设置为overflow: hidden ,因此当我将图片设置为width: 100%时,图片看起来会垂直切割,因此我使用!important强制它们可见,并避免将幻灯片动画显示在框我设置overflow: hidden到幻灯片的容器 div。 Hope it works for you.希望对你有效。

UPDATE - New Solution:更新 - 新解决方案:

Original problem -> http://jsfiddle.net/xMddf/1/ (Even if I use overflow-y: visible it becomes "auto" and actually "scroll".)原始问题-> http://jsfiddle.net/xMddf/1/ (即使我使用overflow-y: visible它变成“自动”实际上是“滚动”。)

#content {
    height: 100px;
    width: 200px;
    overflow-x: hidden;
    overflow-y: visible;
}

The new solution -> http://jsfiddle.net/xMddf/2/ (I found a workaround using a wrapper div to apply overflow-x and overflow-y to different DOM elements as James Khoury advised on the problem of combining visible and hidden to a single DOM element.)新解决方案-> http://jsfiddle.net/xMddf/2/ (我发现了一种解决方法,使用包装器div 将overflow-xoverflow-y应用于不同的 DOM 元素,正如James Khoury建议的结合visiblehidden到单个 DOM 元素。)

#wrapper {
    height: 100px;
    overflow-y: visible;
}
#content {
    width: 200px;
    overflow-x: hidden;
}

I've run into this issue when trying to build a fixed positioned sidebar with both vertically scrollable content and nested absolute positioned children to be displayed outside sidebar boundaries .我在尝试构建一个固定位置的侧边栏时遇到了这个问题,该侧边栏既包含垂直滚动的内容,又包含嵌套的绝对定位子项以显示在侧边栏边界之外

My approach consisted of separately apply:我的方法包括单独应用:

  • an overflow: visible property to the sidebar element overflow: visible属性
  • an overflow-y: auto property to sidebar inner wrapper一个overflow-y: auto属性

Please check the example below or an online codepen .请查看下面的示例或在线 codepen

 html { min-height: 100%; } body { min-height: 100%; background: linear-gradient(to bottom, white, DarkGray 80%); margin: 0; padding: 0; } .sidebar { position: fixed; top: 0; right: 0; height: 100%; width: 200px; overflow: visible; /* Just apply overflow-x */ background-color: DarkOrange; } .sidebarWrapper { padding: 10px; overflow-y: auto; /* Just apply overflow-y */ height: 100%; width: 100%; } .element { position: absolute; top: 0; right: 100%; background-color: CornflowerBlue; padding: 10px; width: 200px; }
 <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p> <div class="sidebar"> <div class="sidebarWrapper"> <div class="element"> I'm a sidebar child element but I'm able to horizontally overflow its boundaries. </div> <p>This is a 200px width container with optional vertical scroll.</p> <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p> </div> </div>

There is now a new way of addressing this issue - if you remove position: relative from the container which needs to have the overflow-y visible, you can have overflow-y visible and overflow-x hidden, and vice versa (have overflow-x visible and overflow-y hidden, just make sure the container with the visible property is not relatively positioned).现在有一种解决这个问题的新方法- 如果你从需要让溢出-y可见的容器中删除position: relative ,你可以让溢出-y可见和溢出-x隐藏,反之亦然(有溢出- x 可见和溢出-y 隐藏,只要确保具有可见属性的容器没有相对定位)。

See this post from CSS Tricks for more details - it worked for me: https://css-tricks.com/popping-hidden-overflow/有关更多详细信息,请参阅 CSS Tricks 的这篇文章 - 它对我有用: https ://css-tricks.com/popping-hidden-overflow/

For my use case, adding overflow-x:visible; overflow-y:clip对于我的用例,添加overflow-x:visible; overflow-y:clip overflow-x:visible; overflow-y:clip onto the div that has the overflow seems to give me the desired effect of hiding overflow on the Y axis while not giving me a scrollbar on the X axis (i have a carousel slider that was loading images full-size before scaling them back down again, and these images were taking up 75% of the page height on load, hence wanting no overflow-y ). overflow-x:visible; overflow-y:clip到有溢出的 div 上似乎给了我在 Y 轴上隐藏溢出的预期效果,而没有在 X 轴上给我一个滚动条(我有一个轮播滑块,它之前正在加载全尺寸图像再次缩小它们,这些图像在加载时占据了页面高度的 75%,因此不需要overflow-y )。

No parent wrapper div was needed, just a fixed height set on the overflowing element.不需要父包装 div,只需在溢出元素上设置一个固定height I realise this solution may not work for everyone, but it could certainly help some.我意识到这个解决方案可能并不适合所有人,但它肯定可以帮助一些人。

I used the content + wrapper approach... but I did something different than mentioned so far: I made sure that my wrapper's boundaries did NOT line up with the content's boundaries in the direction that I wanted to be visible .我使用了内容 + 包装器的方法......我做了一些与目前为止提到的不同的事情:我确保我的包装器的边界没有在我想要可见的方向上与内容的边界对齐。

Important NOTE: It was easy enough to get the content + wrapper, same-bounds approach to work on one browser or another depending on various CSS combinations of position , overflow-* , etc., but I never could use that approach to get them all correct (Edge, Chrome, Safari, etc.).重要说明:根据positionoverflow-*等的各种 CSS 组合,让 content + wrapper、same-bounds 方法在一个或另一个浏览器上工作很容易,但我永远无法使用这种方法来获得它们全部正确(Edge、Chrome、Safari 等)。

But when I had something like:但是当我有类似的东西时:

 #hack_wrapper { position:absolute; width:100%; height:100%; overflow-x:hidden; } #content_wrapper { position:absolute; width:100%; height:15%; overflow:visible; }
 <!-- #hack_wrapper div created solely for this purpose --> <div id="hack_wrapper"> <div id="content_wrapper"> ... this is an example of some content with far too much horizontal content... like, way, way, way too much content. </div> </div>

... all browsers were happy. ...所有浏览器都很高兴。

I was facing the same issue, the following solution worked (styles are applied to the parent block)我遇到了同样的问题,以下解决方案有效(样式应用于父块)

overflow-y: visible;
overflow-x: clip;

A small "hack" that works very well if you only want the first row visible (but still need overflow):如果您只希望第一行可见(但仍需要溢出),那么一个小“hack”效果很好:

set gap really high so you are sure the second row is pushed out of the screen - eg:将间隙设置得非常高,以便您确定第二行被推出屏幕 - 例如:

gap: 10000rem;

It is really hacky but works great for something like a desktop nav with menus that need to overflow...它真的很hacky,但非常适合带有需要溢出的菜单的桌面导航......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM