简体   繁体   English

如何在 1 行的小屏幕上正确堆叠 col 元素?

[英]How do I properly stack col elements on small screens in 1 row?

When using the built in bootstrap col classes, when I shrink my grid to mobile, the columns are taking 100% of the width of the row thus on mobile this results in a very long vertical list of items even though there should be space on the horizontal axis to accommodate for more items.当使用内置的引导 col 类时,当我将网格缩小到移动设备时,列占用了行宽的 100%,因此在移动设备上这会导致非常长的垂直项目列表,即使应该有空间水平轴以容纳更多物品。

This is for a personal website I'm doing, it's a course section where I'm laying out a grid of the courses we would like to offer.这是针对我正在做的个人网站的,这是一个课程部分,我在其中布置了我们想要提供的课程的网格。 The problem and what I've already tried is explained below.下面解释了问题和我已经尝试过的问题。

This is the HTML I have:这是我拥有的 HTML:

  <div class="container courses-container">
        <div class="row justify-content-center courses-header no-gutters">
          <div class="col-1">
            <h2>Courses</h2>
          </div>
        </div>
        <div class="row courses">
          <div class="col-xs-1 col-md-2 course-item">
            <div >
              <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid"  >
            </div>
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
        </div>
        <div class="row courses">
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
          <div class="col-xs-1 col-md-2 course-item">
            <img src="https://cdn4.iconfinder.com/data/icons/musical-instruments-22/50/22-512.png" alt="..." class="img-fluid" height="210px" width="200px" >
          </div>
        </div>
      </div>

This is the CSS I'm using:这是我正在使用的 CSS:

.courses-header {
    margin: 2em 0;
}

.carousel-container, .courses-container {
  padding: 0;
}

@media (min-width:  720px) {
  .courses {
  margin: 3em 0;
  }
}

@media only screen and (max-width: 767px) {
  .course-item {
    width: 33%;
  }
}

The last media query at the 767px breakpoint 'fixes' the issue by targeting the custom class i've set to each column so that I make them take up only 33% of the width on smaller screens, however if you comment out that section and preview this example on a smaller screen you'll see that each column will essentially sit on one line .在 767px 断点处的最后一个媒体查询通过定位自定义 class我为每一列设置的自定义 class 来“修复”问题,以便我让它们在较小的屏幕上仅占用 33% 的宽度,但是如果您注释掉该部分并在较小的屏幕上预览此示例,您会看到每一列基本上都位于一行上 This is why I've also set col-xs-1 on each item but I think I don't need it if I use another approach.这就是为什么我还在每个项目上设置 col-xs-1 但我认为如果我使用另一种方法我不需要它。

Is this the right way to do it because to me it seems like that there should be an easier way in which I can control the grid to be displayed properly on smaller screens.这是正确的方法吗,因为对我来说,似乎应该有一种更简单的方法来控制网格在较小的屏幕上正确显示。

The full layout on larger screens is taking 2 rows and each row has 6 columns in it.大屏幕上的完整布局占用 2 行,每行有 6 列。 On smaller screens this layout is not possible since it's too wide.在较小的屏幕上,这种布局是不可能的,因为它太宽了。

I expect once I resize the browser window to a smaller one that the cols will break in to more rows vertically and they do but without adjusting their width with a media query each column breaks in to its own line taking 100% of it thus it creates alot of veritical space and it doesn't use the horizontal space properly as there's a lot of it left empty.我希望一旦我将浏览器 window 调整为更小的浏览器,cols 将垂直分成更多行并且它们会这样做,但没有通过媒体查询调整它们的宽度,每列会占用它自己的行,占它的 100%,因此它创建很多垂直空间,它没有正确使用水平空间,因为它有很多是空的。

Ideally it should smoothly break the items in to more rows but keeping several columns on each line, the smaller the screen the less items on each line but at all times it should keep at least 2, if not 3 items side by side.理想情况下,它应该平滑地将项目分成更多行,但每行保留几列,屏幕越小,每行上的项目越少,但始终应保持至少 2 个,如果不是 3 个项目并排。

Use col-xs-2 on all <div class="col-xs-1 col-md-2 course-item">在所有<div class="col-xs-1 col-md-2 course-item">上使用col-xs-2

Remove col-xs-1 and add col-xs-2删除col-xs-1并添加col-xs-2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么内联元素会包裹在小屏幕上? - Why do inline elements wrap on small screens? 如何修复css在小屏幕上有两列? - How do I fix the css to have two columns on small screens? 当使用flexbox在其包含的行中垂直居中列时,如何将引导网格列堆叠在移动屏幕上? - How do I get bootstrap grid columns to stack on mobile screens when using flexbox to vertically center columns within their containing row? 如果更改 col 填充,如何影响行边距? - How do I affect row margin if I change col padding? 小屏幕上的 Bootstrap col-12 和大屏幕上的固定长度 - Bootstrap col-12 on small screens and fixed length on larger screens 如何修复引导程序中的行、列布局 - How do I fix the row, col layout in bootstrap 标头元素在小屏幕上无法对齐 - header elements not aligning on small screens 如何使用 javascript 连续制作 3 列和 2 列(引导程序)? - How can i make 3 col in a row an 2 col in a row (bootstrap) with javascript? 我如何使用调整为带有桌子的小屏幕的引导网格系统 - How do i use bootstrap grid system adjusted to small screens with a table Bootstrap:如何在带有或不带有媒体查询的小屏幕上创建响应div列? - Bootstrap : How do i create responsive div columns in the small screens with or without media queries?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM