[英]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.