【问题标题】:keeping the depth of a div responsive保持 div 的深度响应
【发布时间】:2017-01-19 10:47:49
【问题描述】:

我已尝试调整此post 及其alistapart 链接。但是我无法让容器中的三个块做出响应并保持它们的纵横比

<div class="hpGridTopRow">
  <span class="hpBox lightBlue one">
    <p class="hbBoxesp">New to Restore</p>
  </span>
  <span class="hpBox green two">
    <p class="hbBoxesp">Events</p>
  </span>
  <span class="hpBox midBlue three">
    <p class="hbBoxesp">Talks</p>
  </span>
</div>

我需要他们将自己安排到 hpGridTopRow div 的边界,所以我使用 flex - 并且它工作得很好。

但是,布局必须是响应式的 - 容器的纵横比需要随着宽度的变化而保持不变。

这是css

.hpGridTopRow, 
.hpGridBottomRow {
display: -webkit-flex;
    display: flex;
    -webkit-justify-content: space-between;
    justify-content: space-between;
    position: relative;
    padding-bottom: 20%;
    height: 0;
    overflow:hidden;
    background:yellow;
}
.hpBox {    
    width:24.18803418803419%;
    text-align:center;
    height:264px;
    height:100%;
}

.hpBox.one {
    width:49.4017094017094%;
}

.lightBlue {
    background:#15b2d2;
}
.green {
    background:#8cc63f;
}
.midBlue {
    background:#6a8aa8;
}    
.hbBoxesp {
    color:#fff;
    margin-top:40px;
}

我创建了一个jsfiddle - 你可以看到容器背景显示,但不是实际的盒子,并且所述容器的行为是响应式的。

如何让跨度正常运行?

【问题讨论】:

  • 哎呀,那把小提琴烧了我的眼球!
  • 感谢您的有用评论皮特。
  • 如果您要创建一个示例,那么请做一些我们可以使用的事情 - 我花了 30 分钟才可以再次正常看到。由于情况如此糟糕,我只是继续前进,我猜我不是唯一的人 - 所以也许在以后的例子中要记住这一点。
  • 嗯,我喜欢这种颜色,我有一个答案。只是认为你应该继续前进......但我会接受你所说的重新着色。
  • 有幽默感 - 有人喜欢它 :)

标签: html css


【解决方案1】:

我删除了那些多余的颜色并简化了百分比以简化代码。

我的想法是我已将position: relative; 添加到父级,将position: absolute; 添加到子级。这可确保子位置与父位置绑定。

我还为孩子添加了top: 0; bottom: 0;,以使孩子span 跨越高度作为父母div

然后您必须使用leftright 定位将跨度放置在正确的位置。

.hpGridTopRow {
  display: -webkit-flex;
  display: flex;
  -webkit-justify-content: space-between;
  justify-content: space-between;
  position: relative;
  padding-bottom: 20%;
  height: 0;
  overflow: hidden;
  background: yellow;
  position: relative;
}
.hpBox {
  width: 25%;
  text-align: center;
  position: absolute;
  top: 0;
  bottom: 0;
}
.hpBox.one {
  width: 50%;
}
.hpBox.two {
  left: 50%;
}
.hpBox.three {
  right: 0;
}
.lightBlue {
  background: #15b2d2;
}
.green {
  background: #8cc63f;
}
.midBlue {
  background: #6a8aa8;
}
.hbBoxesp {
  color: #fff;
  margin-top: 40px;
}
<div class="hpGridTopRow">
  <span class="hpBox lightBlue one">
    <p class="hbBoxesp">New to Restore</p>
  </span>

  <span class="hpBox green two">
    <p class="hbBoxesp">Events</p>
  </span>

  <span class="hpBox midBlue three">
    <p class="hbBoxesp">Talks</p>
  </span>
</div>

【讨论】:

  • 感谢 sn-p - 以及您的有用解释 - 看起来更好。这是否意味着我不能使用 flex 布局,因为间距现在是错误的 -
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-09
  • 2018-11-22
  • 1970-01-01
  • 2021-04-27
  • 2016-01-09
  • 2011-02-07
相关资源
最近更新 更多