【问题标题】:Taking size of overflown content into account for positioning考虑溢出内容的大小进行定位
【发布时间】:2018-10-14 09:10:44
【问题描述】:

是否有一种仅限 CSS/HTML 的方法来考虑溢出内容的大小?考虑下面的例子:

.outerZeroSize {
  width: 0px;
  height: 0px;
  overflow: visible visible;
}

.innerContent {
  width: 200px;
  height: 300px;
}

.belowContent {
  
}
<div class="outerZeroSize">
  <div class="innerContent">
    Some content<br/>
    Some more content <br/>
    And some more
  </div>
</div>
<div class="belowContent">Content below</div>

div belowContent 中的文本最终位于 innerContent 文本的顶部。

问题:由于innerContentheight300px,我希望belowContent 出现在300px 下方outerZeroSize 下方(而不是在其顶部)。这可以用 CSS/HTML 实现吗?

约束:

  • 假设 .outerZeroSize.innerContent 不能更改(HTML 和 CSS 部分都不是),因为它们来自第三方 (React-Virtualized Table)。
  • 更新:.innerContent 的高度可以动态变化,300px 只是一个示例(因此也不能选择将.belowContent 偏移相同的数量)。
  • 可以更改的是.belowContent(HTML 和 CSS),当然也可以在.outerZeroSize div 周围添加内容。

Related question(从 React 虚拟化表的角度来看)。

【问题讨论】:

    标签: html css overflow


    【解决方案1】:

    只需将 300px margin-top 添加到belowContent

    .outerZeroSize {
      width: 0px;
      height: 0px;
      overflow: visible visible;
    }
    
    .innerContent {
      width: 200px;
      height: 300px;
    }
    
    .belowContent {
      margin-top: 300px;
    }
    <div class="outerZeroSize">
      <div class="innerContent">
        Some content<br/>
        Some more content <br/>
        And some more
      </div>
    </div>
    <div class="belowContent">Content below</div>

    另一种解决方案是将belowContent 移动到outerZeroSize 中。

    .outerZeroSize {
      width: 0px;
      height: 0px;
      overflow: visible visible;
    }
    
    .innerContent {
      width: 200px;
      height: 300px;
    }
    
    .belowContent {
      width: 200px;
    }
    <div class="outerZeroSize">
      <div class="innerContent">
        Some content<br/>
        Some more content <br/>
        And some more
      </div>
      <div class="belowContent">Content below</div>
    </div>

    【讨论】:

    • 感谢昆汀:这是原始问题的正确答案。但是,我忘记添加一个重要细节:300px 高度不是固定的,而是动态变化的(这些是表中的行)。我会相应地更新问题。 (根据将其包含在outerZeroSize 中,这不是一个选项,如原始问题所述。)
    • 它确实改变了一切。您是否有机会覆盖 CSS 文件中的 outerZeroSize 样式?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多