html - "overflow-y: scroll;"隐藏水平线上溢出的元素

标签 html css position overflow

我有一个 parent div,它包含一个绝对定位的 child div。

child div 位于parent div 的初始边界之外。渲染正常,没有任何问题。

我添加了一个 overflow-y: scroll;parent div,现在 child 元素被隐藏了,尽管添加了 overflow-x: 可见;.

CSS

#parent{
     position: relative;
     overflow-y: scroll;
     overflow-x: visible;
}
#child{
     position: absolute;
     left: -50px;
}

还有一个 Fidle

最佳答案

我们在 stackoverflow 上找到了这个,但你不会开心。

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

简而言之,答案是这样的

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.

他的回答更深入地讨论了解释这种情况的 W3 规范。

几乎你的 overflow-x 是不可见的,因为它会变成 auto。 (该样式的哪一部分隐藏了它的内容。)如果您的 overflow-y 与它有任何不同。

编辑:

不过,您可以尝试将此作为该规范的解决方法。

HTML

/*Necessary styles for example*/
#parent{
  display: inline-block;
  position: relative;
}
#absolute-child {
    position: absolute;
    left: -50px;
}
#child{
    overflow-y: scroll;
}

/*Below here is styles only for display purposes*/
body {
  display:flex;
  justify-content: center;
}

#absolute-child {
    background-color: red;
    color: white;
}

#child{
    border: solid 1px black;
    width: 200px;
    height: 200px;
    overflow-y: scroll;
}

#child {
    border: solid 1px black;
    width: 200px;
    height: 200px;
}
<div id=parent>
    <div id=absolute-child>
        This is the child
    </div>
    <div id=child>
        This is the child
    </div>
</div>

http://jsfiddle.net/BFLQr/

给我一​​点时间解释一下我做了什么。

编辑

所以首先我基本上必须将您的父 div 移动为现在存在的父 div 的子 div。这有点奇怪,但这是我唯一能想到的。现在的父 div 通过 display: inline-block 应用了“shrink to fit”样式,这将它包裹在它的子 div 周围。

由于 absolute 位置被推出文档流,这意味着您的 absolute 位置子元素不会影响其新的“收缩以适合”父元素的宽度或高度。 “缩小以适合”父级也有显示 relative 这让你的 absolute 位置 div 可以根据它定位。此外,由于父级现在显示 inline-block 以便将其居中,因此您必须在其包含元素上使用 text-align center。这意味着您还需要在#parent 或#child 元素上将文本 left 对齐。

希望这对您有所帮助。 :)

附言我编辑了 fiddle ,它在 #child 元素上有一个不必要的相对位置。我还向新父级添加了文本左对齐

关于html - "overflow-y: scroll;"隐藏水平线上溢出的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22955465/

相关文章:

javascript - 当向其中添加更多元素时,插入到 div block 中的表会水平重叠

java - 获取特定像素的 x,y 位置

html - 绝对定位的 div 出现在其他一切之上

css - position:absolute;不将元素粘贴到父级可滚动高度的底部

jquery - 隐藏在其他元素上的 CSS 菜单

android - 文本对齐 :center not working in CDATA in string. xml

javascript - 使用响应式网页设计和 javascript 设计面包屑

html - 如何根据 ionic 应用程序构建类型在 css 中应用 if else?

css - 智能手机的字体和字体大小单位

css - 没有谷歌字体的 Bootstrap CSS 2 (Bootswatch)