【问题标题】:Responsive 2-columns squares in CSSCSS中的响应式2列正方形
【发布时间】:2017-04-05 21:50:09
【问题描述】:

我正在尝试创建一个响应式页面,其中正方形占全宽的 50%(每行 2 个正方形)。在移动设备(包括 iPhone 6/7 Plus)上,这些方块应该是一个在另一个之下,但这是它不能使用以下代码的地方:

.square {
  width: 100vw;
  height: 100vw;
  float: left;
}

@media (min-width: 1170px) {
  .square {width: 50vw; height: 50vw; }
}

理想情况下我会使用 Bootstrap,但我不知道如何创建正方形。

【问题讨论】:

  • 你设置html, body { margin: 0 }了吗?
  • 是的 - 我认为这与智能手机的“设备像素比”有关,这使得宽度的使用变得过时
  • 不,这实际上是基于视口单元不排除滚动条的事实...为您发布了答案

标签: twitter-bootstrap css responsive-design responsive


【解决方案1】:

引导示例:

<div class="container">
  <div class="row">
    <div class="col-lg-6">
      <div class="square"></div>
    </div>
    <div class="col-lg-6">
      <div class="square"></div>
    </div>
  </div>
</div>

CSS:

.square {
  height: 0;
  padding-bottom: 100%;
  background-color: green;
}

http://codepen.io/Deka87/pen/yMrmKK

【讨论】:

  • 它看起来与我正在寻找的答案一模一样,但正方形对我的代码没有响应,它在移动设备上仍然是 2 列。您的 Codepen 是否包含 Bootstrap?
  • 好的,我错过了一个元标记:stackoverflow.com/questions/9386429/…
  • @Johannes,在 Bootstrap 中也用于支持响应式嵌入。
  • 但是如果有内容,你必须把它放在它上面的另一个容器中(绝对位置),不是吗?
  • @Johannes,正确,您应该将 position:relative; 添加到方形容器并将其内容包装在绝对定位的容器中:position: absolute; top: 0; bottom: 0; left: 0; right: 0;
【解决方案2】:

您正在寻找这样的东西吗? https://jsfiddle.net/5xc8fs0a/

它利用了填充百分比基于宽度这一事实,因此 padding-bottom: 100% 确保高度和宽度完全相同。

.square {
  float: left;
  width: 48%;
  margin-right: 1%;
  background: red;
}

.square:after {
  content: '';
  display: inline-block;
  height: 0;
  padding-bottom: 100%;
}

【讨论】:

  • 很好,但反应迟钝!
  • 它是响应式的,因为当您更改视口时它会响应,它的宽度也会响应。您所说的“不响应”是什么意思 - 它不会为较小的视图叠加?
  • 也许这就是您想要的更多 - 加载了引导程序:jsfiddle.net/90wzj4t0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 2019-01-29
  • 1970-01-01
  • 1970-01-01
  • 2014-02-11
  • 2016-02-03
相关资源
最近更新 更多