【问题标题】:How to expand absolutely positioned child element to fullscreen width?如何将绝对定位的子元素扩展到全屏宽度?
【发布时间】:2021-08-06 14:28:26
【问题描述】:

我的水平条在左右两边都有一些边距。 我想在栏后面添加叠加层并希望拉伸到全屏宽度(就像模态对话框叠加层一样)但叠加层的顶部位置应该与栏的顶部位置相同,并且覆盖层的底部位置应该与栏的底部位置相同. 但是,由于左右间距,我无法将其拉伸到全屏宽度。所以我尝试添加等于左/右边距值的负左/右位置以使其全屏显示。 然而,这个不合适的解决方案似乎是合适的,因为左/右的边距将来可以是任何值,并且不是硬编码的。 有没有理想的通用解决方案。

下面是sn-p的代码:

<div class="parent">
  <div class="bar"></div>
  <div class="overlay"></div>
</div>
.parent {
  position: relative;
  margin-left: 50px; --> Margins can be any value not just hardcoded
  margin-right: 50px;
}

.bar {
  height: 50px;
  background: blue;
}
.overlay {
  z-index: -1;
  position: absolute;
  background: purple;
  left: -50px; --> Set negative position to stretch it to fullscreen but not seems to be proper solution as margin can be of any value
  top: 0;
  right: -50px;
  bottom: 0;
}

【问题讨论】:

    标签: html css frontend padding margin


    【解决方案1】:

    您可以将 overlay 移出父元素,并删除所有这些边距。

    body {
      margin: 0;
    }
    .parent {
      position: relative;
      padding-left: 50px; 
      padding-right: 50px;
    }
    
    .bar {
      height: 50px;
      background: blue;
    }
    .overlay {
      z-index: -1;
      position: absolute;
      background: purple;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
    }
    <div class="parent">
      <div class="bar"></div>
      <div class="overlay"></div>
    </div>

    【讨论】:

    • 嗨@prettyInPink,这不起作用,因为覆盖的顶部位置应该与栏的顶部位置相同,唯一的问题是左右应该伸展到全屏。
    • 所以叠加层应该和条的高度相同?更新示例。
    • 是的,应该和条的高度相同,并且顶部位置应该总是和条相同。
    • 你能检查我更新的答案吗?将 parent 设置为 relative 时,绝对定位元素不会溢出。由于父级没有设置任何高度并适应内部子级.bar的高度,因此覆盖将在.bar的顶部和底部开始和结束
    【解决方案2】:

    我想你想要这个

    <div class="parent"> 
        <div class="overlay">
            <div class="bar"></div>
        </div> 
     </div>
    

    添加这些样式

    body {
        margin:0;
    }
    
    .parent { 
        margin-left: 50px;
        margin-right: 50px; 
    }
    
    .bar { height: 50px; background: blue; }
      
    .overlay { 
        z-index: -1; 
        position: absolute; 
        background: purple; 
        left: 0;
        top: 0;    
        right: 0; 
        bottom: 0; 
    }
    

    【讨论】:

    • 嗨@Dinesh Rout,实际上覆盖应该更宽才能拉伸到全屏,并且它的顶部位置应该与栏的顶部位置相同,并且与底部位置相同,如果我的栏位于中间或页面叠加顶部/底部位置的底部位置应与其不同步。
    【解决方案3】:

    position: fixed 用于.overlay。并设置左右0。更多关于fixed

    body {
      margin: 0
    }
    .parent {
      position: relative;
      margin-left: 50px;
      margin-right: 50px;
    }
    .bar {
      height: 50px;
      background: blue;
    }
    .overlay {
      z-index: -1;
      position: fixed;
      background: purple;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
    }
    <div class="parent">
      <div class="bar"></div>
      <div class="overlay"></div>
    </div>

    【讨论】:

    • 嗨@PrakashM,感谢您的解决方案,但是这并非在所有情况下都有效,如果我的栏位于页面底部的某个位置,那么由于顶部,叠加层仍将保持在顶部:0和固定位置,它的顶部位置应该与栏的顶部位置保持同步,唯一的问题是左右应该拉伸到全屏,这就是为什么我给出了绝对位置,但由于这个左/右位置由于边距而受到限制左右的。
    猜你喜欢
    • 2014-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-22
    • 1970-01-01
    相关资源
    最近更新 更多