创建这种布局的最佳实践?

时间:2018-11-13 08:17:42

标签: css twitter-bootstrap

最初,我想在引导程序4中进行这种布局,但是现在我几乎可以确定,如果不使用某些技巧(如将内容加倍,然后将其隐藏在手机/桌面上或使用javaScript进行隐藏),这是不可能的。

对于台式机:

+---+---+
| 1 |   |
+---+   +
| 3 |   |
+---+ 2 +
| 4 |   |
+---+   +
| 5 |   |
+--+----+

对于智能手机:

+---+
| 1 |
+---+
| 3 |
+---+
| 2 |
+---+
| 4 |
+---+
| 5 |
+---+

Bootstrap 4面临的主要问题是列的高度相等,我不知道该怎么做。 我当然可以在两个嵌套的列中执行此操作,但是随后我无法按照自己的意愿重新排列顺序。

在bootstrap 3中,我可以像示例#9那样进行操作: https://www.bootply.com/siUcTuNhUy 但是如何在Bootstrap 4中做到这一点?

请告诉我我想念的东西。

2 个答案:

答案 0 :(得分:1)

不确定Bootstrap中的替代方法,但这可以使用CSS网格完成。

您可以在此处找到带有解决方案的codepen-https://codepen.io/vishwaovi/pen/RqKaMv?editors=1100


涉及的步骤:

1。将包含元素的父级定义为网格。

HTML

var arr = $.map(JSON.parse(_data.data), function (el) { return el });

CSS

<div class="main">
  <div class="section section1"> 1 </div>
  <div class="section section2"> 2 </div>
  <div class="section section3"> 3 </div>
  <div class="section section4"> 4 </div>
  <div class="section section5"> 5 </div>
</div>


2。使section2从第2列开始,跨4行

.main {
   display:grid;
   grid-template-columns: 1fr 1fr; // defining a two column grid
}


3。移动视图:将网格更改为列,并使section2从第1列和第3行开始

.section2{
  grid-column: 2;
  grid-row: span 4;
}

答案 1 :(得分:1)

也许这很有用。

为了满足您对智能手机布局的要求,我使用了flexbox(带有Bootstrap 4网格):

.grid .stacked {
  width: 48%
}

.grid .section {
  background: #069;
  margin: 5px auto;
  text-align: center;
  color: #fff;
  WIDTH: 100%;
}

@media (max-width: 575px) {
  .grid .stacked {
    width: 100%
  }
  .stacked .section:nth-child(1) {
    order: 1;
  }
  .stacked .section:nth-child(2) {
    background: #c00;
    order: 3;
  }
  .stacked .section:nth-child(3) {
    order: 2;
  }
  .stacked .section:nth-child(4) {
    order: 4;
  }
  .stacked .section:nth-child(5) {
    order: 5;
  }
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">

<div class="container">
  <div class="grid d-flex flex-wrap">
    <div class="stacked d-flex flex-column">
      <div class="section col-xs-12">1</div>
      <div class="section col-xs-12 d-block d-sm-none">2</div>
      <div class="section col-xs-12">3</div>
      <div class="section col-xs-12">4</div>
      <div class="section col-xs-12">5</div>
    </div>
    <div class="section col-xs-12 col-sm-6 d-none d-sm-block">2</div>
  </div>
</div>