【问题标题】:Overflow visible not working with position absolute as child element溢出可见不使用绝对位置作为子元素
【发布时间】:2021-09-04 07:41:12
【问题描述】:

对于下面的 HTML 列表,我只想让它只能垂直滚动,默认情况下它应该在水平方向可见。

即使在将溢出-x 设置为可见之后,它也被视为滚动

<div class="container">
    <div class="inner-container">
        <div>Item 1</div>
        <div class="hover-arrow"></div>
    </div>
    <div class="inner-container">
        <div>Item 2</div>
        <div class="hover-arrow"></div>
    </div>
    <div class="inner-container">
        <div>Item 3</div>
        <div class="hover-arrow"></div>
    </div>
    <div class="inner-container">
        <div>Item 4</div>
        <div class="hover-arrow"></div>
    </div>
</div>


.container {
   width: 100px;
   overflow-y: scroll;
   overflow-x: visible;
   border: 1px solid black;
   height: 200px;
}

.inner-container {
  position: relative;
  height: 100px;
  background: grey;
}

.hover-arrow {
   position: absolute;
   right: -20px;
   bottom: 0;
   border-left: 20px solid grey;
   border-top: 30px solid transparent;
   border-bottom: 30px solid transparent;
}

JS fiddle 也一样 https://jsfiddle.net/m148ujcn/

【问题讨论】:

    标签: html css css-selectors


    【解决方案1】:

    根据W3Specs

    'overflow-x' 和 'overflow-y' 的计算值与其指定的值相同,除了某些与 'visible' 的组合是不可能的:如果一个被指定为 'visible' 而另一个是“滚动”或“自动”,然后“可见”设置为“自动”。如果‘overflow-y’相同,‘overflow’的计算值等于‘overflow-x’的计算值;否则,它是'overflow-x'和'overflow-y'的一对计算值。

    简而言之:

    如果我们将 overflow-x 或 overflow-y 设置为 visible 和其他 其他的,则 visible 值被视为 auto

    在您的小提琴中进行了一些更改检查:JSFiddle

    <div class="wrapper">
     <div class="container">
        <div class="inner-container">
          <div style="padding: 20px 20px 20px 30px;">Item 1</div>
          <div class="hover-arrow"></div>
        </div>
        <div class="inner-container">
          <div style="padding: 20px 20px 20px 30px;">Item 2</div>
         <div class="hover-arrow"></div>
      </div>
      <div class="inner-container">
         <div style="padding: 20px 20px 20px 30px;">Item 3</div>
          <div class="hover-arrow"></div>
         </div>
      </div>
     </div>
    

    【讨论】:

    • 遇到了问题,但在您的解决方案中,您将 overflow-x 标记为隐藏,因为我希望它可见(div : hover-arrow)应该可见
    • 问题不在于溢出属性,而在于悬停箭头 css 本身。您已使用 position: absolute 和 right 作为 -20px。如果您的动机是在一行中有两个 div,那么您应该在两个 div 上都使用 float: left 和 clear: 都在它之后。右:-20px 正在增加 div 的实际框大小。如果你做对了,这也可以工作:0px。
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多