使用媒体查询设置(引导程序)col宽度

时间:2018-03-15 20:17:59

标签: html css twitter-bootstrap media-queries responsive

我有一个横幅和3列的页面,我试图让它在屏幕宽度太小时,前2列从1/3宽度变为50%宽度,最后一列宽度变为100%,使其低于前两个。

当我这样做时,列的高度确实会改变(它们会变为50%,考虑到列现在可以在100%行内相互叠加),但宽度不会。我怎么能解决这个问题?提前谢谢!

Codepen

HTML

<section class="section">
  <div class="container-fluid">
    <div class="aboutBanner"> </div>

    <div class="row">
      <div class="col">

      </div>
      <div class="col">

      </div>
      <div class="col">

      </div>
    </div>
  </div>
</section>

相关CSS

.aboutBanner {
    height: 30%;
    border: 1px solid blue;
}

.row {
    height: 70%;
}

.row .col {
    height: 100%;
    border: 1px solid red;
}

@media screen and (max-width: 768px){
    .row .col:nth-of-type(1),
    .row .col:nth-of-type(2) {
        height: 50%;
        width: 50%;
    }
    .row .col:nth-of-type(3) {
        height: 50%;
        width: 100%;
        border-style: dashed;
    }
}

2 个答案:

答案 0 :(得分:1)

使用bootstrap col- [size]类

See documentation

引导网格基于12列大小,分为5种屏幕尺寸:

  • 超小
    • col-[1 to 12]
    • col-sm-[1 to 12]
  • 中等
    • col-md-[1 to 12]
    • col-lg-[1 to 12]
  • 特大号
    • col-xl-[1 to 12]

试试这个:

<section class="section">
  <div class="container-fluid">
    <div class="aboutBanner"> </div>

    <div class="row">
      <div class="col-md-4 col-sm-6">
          content of first
      </div>
      <div class="col-md-4 col-sm-6">
          content of second
      </div>
      <div class="col-md-4 col-sm-12">
          content of third
      </div>
    </div>
  </div>
</section>

请记住,12是100%,6是50%,4是33%,3是25%等等......

答案 1 :(得分:1)

使用Bootstrap responsive grid columns ...

https://codepen.io/anon/pen/eMzRyb

<section class="section">
  <div class="container-fluid">
    <div class="aboutBanner">

    </div>
    <div class="row">
      <div class="col-sm-4 col-6">

      </div>
      <div class="col-sm-4 col-6">

      </div>
      <div class="col-sm-4 col-12">

      </div>
    </div>
  </div>
</section>

要在codepen中对此进行可视化,我更改了CSS ...

.row {
    height: 70%;
}

.row > div {
    border: 1px solid red;
}

另见: What is the difference among col-lg-*, col-md-* and col-sm-* in Bootstrap?