【问题标题】:How to avoid overlapping between two divs positioning absolute inside a div positioning relative?如何避免在 div 相对定位中的两个绝对定位 div 之间重叠?
【发布时间】:2014-01-31 16:27:38
【问题描述】:

如果页面有足够的空间来承载所有 div,则以下代码有效,但如果我至少调整页面大小,则两个 div 定位绝对重叠。我怎样才能避免这种情况?

#div-chatroom {
  position: relative;
  height: calc(100% - 70px);
  /* IE9+ and future browsers */
  height: -moz-calc(100% - 70px);
  /* Firefox */
  height: -webkit-calc(100% - 70px);
  /* Chrome, Safari */
  padding: 0;
  text-align: center;
  margin: 0;
  border-right: 2px solid #333333;
  overflow: auto;
}

#div-messages {
  position: absolute;
  top: 10px;
  bottom: 110px;
  left: 10px;
  right: 10px;
  min-height: 200px;
  overflow: auto;
}

#div-sending {
  position: absolute;
  bottom: 10px;
  left: 10px;
  right: 10px;
  height: 100px;
}
<div id="div-chatroom">
  <div id="div-messages">messages here</div>
  <div id="div-sending">sending tools here</div>
</div>

更新 #1
根据需要 JSFiddle 上的代码(但如果两个 div 有 position: absolute 它似乎不起作用)。

【问题讨论】:

    标签: css html absolute overlap


    【解决方案1】:

    position:absolute 的全部目的是让元素不被依赖或页面上的任何其他元素(除了它的父元素)你实际上无法实现这一点。如果您想相应地对齐它们,您可以使用绝对位置以外的位置样式来执行此操作。将您的代码发布到 jsfiddle,然后我们将能够提供良好的解决方案。

    【讨论】:

      【解决方案2】:

      好的,我通过改变方法得到了相同的结果。

      CSS (JSFiddle):

      #div-chatroom {
          position: relative;
          height: calc(100% - 70px); /* IE9+ and future browsers */
          height: -moz-calc(100% - 70px); /* Firefox */
          height: -webkit-calc(100% - 70px); /* Chrome, Safari */
          padding: 0;
          text-align: center;
          margin: 0;
          border-right: 2px solid #333333;
          background-color: #ffffff;
          overflow: auto;
      }
      
      #div-messages {
          position: relative;
          margin: 0;
          padding: 0;    
          min-height: 200px;
          height: calc(100% - 100px); /* IE9+ and future browsers */
          height: -moz-calc(100% - 100px); /* Firefox */
          height: -webkit-calc(100% - 100px); /* Chrome, Safari */
          background-color: green;
          overflow: auto;
      }
      
      #div-sending {
          position: relative;
          margin: 0;
          padding: 0;    
          height: 100px;
          background-color: red;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-16
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 2015-07-18
        • 1970-01-01
        相关资源
        最近更新 更多