【问题标题】:Applying Max-Width to a Fixed-Positioned Div Within a Relative-Positioned Div?将最大宽度应用于相对定位的 Div 中的固定位置的 Div?
【发布时间】:2018-01-27 14:50:14
【问题描述】:

将相对 div 中的固定 div 向右对齐,同时仍保留继承的 max-width 的最佳方法是什么?

更新(2018 年 1 月 24 日):我已经用解决方案回答了这个问题。见here

请参阅以下 sn-p 以获得更多参考:

body {
  margin: 0;
  padding: 0;
  border: 0;
}

.container {
  width: 100%;
}

.max-width {
  margin: 0 auto;
  max-width: 500px;
  height: 1000px;
  position: relative;
  box-sizing: border-box;
  background-color: lightgrey;
}

.box {
  max-width: inherit;
  width: 20%;
  height: 20px;
  position: fixed;
  background: blue;
  float: right;
  color: white;
  text-align: center;
  right: 0;
}
<div class="container">
  <div class="max-width">
    <div class="box">fix to right?</div>
  </div>
</div>

【问题讨论】:

    标签: html css css-position positioning


    【解决方案1】:

    固定元素的位置总是相对于视口/窗口,而不是任何其他元素。

    您唯一能做的(使用 CSS)是使用 right: calc(50% - 250px); 使其位置与 500 像素宽的居中“父”元素的右边框对齐,但这仅在屏幕为大于或等于“父”元素的max-width

    在 cmets 之后添加:另外使用 right: 0 为宽度低于 500 像素的屏幕添加媒体查询(感谢 @MrLister)

    body {
      margin: 0;
      padding: 0;
      border: 0;
    }
    
    .container {
      width: 100%;
    }
    
    .max-width {
      margin: 0 auto;
      max-width: 500px;
      height: 1000px;
      position: relative;
      box-sizing: border-box;
      background-color: lightgrey;
    }
    
    .box {
      max-width: inherit;
      width: 20%;
      height: 20px;
      position: fixed;
      top: 0;
      right: calc(50% - 250px);
      background: blue;
      float: right;
      color: white;
      text-align: center;
    }
    
    @media (max-width: 500px) {
      .box {
        right: 0px;
      }
    }
    <div class="container">
      <div class="max-width">
        <div class="box">fix to right?</div>
      </div>
    </div>

    【讨论】:

    • 还有一个带有right:0的媒体查询,用于更窄的屏幕。
    • 好吧,我确实喜欢你回答的结果,但这只是解决了问题的一半......因为我需要固定位置的 div 保持最大宽度 div 固有的 20% 宽度,因为当我将 div 拉伸超过 500px 最大宽度时,固定 div 也会扩展......
    • @nrweb 你从来没有在问题中提到过。无论如何,答案是一样的:使用媒体查询。
    • 我自己开发了一个解决方案,但感谢您的指导。
    【解决方案2】:

    如果你这样做会怎样:

    CSS

    body {
      margin: 0;
      padding: 0;
      border: 0;
    }
    
    .container {
      width: 100%;
    }
    
    .max-width {
      margin: 0 auto;
      max-width: 500px;
      height: 1000px;
      position: relative;
      box-sizing: border-box;
      background-color: lightgrey;
    }
    
    .box {
      max-width: inherit;
      width: 20%;
      height: 20px;
      position: fixed;
      top: 0;
      right: calc(50% - 250px);
      background: blue;
      float: right;
      color: white;
      text-align: center;
    }
    
    @media screen and (max-width: 500px) {
    .box {
          right: 0;
        }
    }
    
    @media screen and (min-width: 501px) {
    .box {
          width: 100px; /* 100px is 20% of the max-width */
        }
    }
    

    HTML

    <div class="container">
      <div class="max-width">
        <div class="box">fix to right?</div>
      </div>
    </div>
    

    【讨论】:

    • 感谢您的建议,但这似乎并没有解决问题...将其应用于 sn-p,它只是将 .box 扩展到了最大宽度容器之外。
    • 这并不能完全解决当前的问题,因为我需要 I 让固定位置的 div 保持最大宽度 div 的 20%。当我将窗口拉伸超过 500 像素最大宽度时,固定 div 也会扩展,这是不希望的。
    • 如果您添加另一个大于 500 像素的媒体查询怎么办?我已经编辑了我的帖子
    • 我自己想出了一个解决方案,感谢您的帮助。
    【解决方案3】:

    想出了点办法。毕竟是可以做到的!

    body {
      margin: 0;
      padding: 0;
      border: 0;
      width: 100%;
    }
    
    .max-width {
      max-width: 500px;
      height: 2000px;
      margin: auto;
      background-color: lightgrey;
      position: relative;
    }
    
    .box1 {
      position: relative;
      width: 20%;
      height: 100px;
      background-color: yellow;
      text-align: center;
    }
    
    .container {
      position: absolute;
      width: 60%;
      background-color: purple;
      height: 100%;
      margin: 0 auto;
      left: 0;
      right: 0;
      top: 0;
      text-align: center;
    }
    
    .wrap-box {
      position: fixed;
      max-width: 500px;
      width: 100%;
      height: 100%;
      left: 50%;
      transform: translateX(-50%);
      top: 0;
    }
    
    .wrap-box > div.box2 {
      width: 20%;
      height: 100px;
      background-color: blue;
      position: absolute;
      top: 0;
      right: 0;
      text-align: center;
    }
    
    .wrap-box > div.box3 {
      width: 20%;
      height: 100px;
      background-color: green;
      position: absolute;
      bottom: 0;
      right: 0;
      text-align: center;
    }
    <div class="max-width">
      <div class="box1">position: relative, width: 20%</div>
      <div class="container">
        position: absolute, width: 60%
        <div class="wrap-box">
          <div class="box2">position: fixed (top), width: 20%</div>
          <div class="box3">position: fixed (bottom), width: 20%</div>
        </div>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 2011-08-17
      • 1970-01-01
      • 2017-01-27
      • 1970-01-01
      • 2017-06-03
      • 2019-02-18
      • 1970-01-01
      • 1970-01-01
      • 2011-12-12
      相关资源
      最近更新 更多