我有一个 4 列表,CSS 如下。它使用第一个 child + td
语法在 css 中定义列的大小。例如第三列是:
.tbl4Cols td:first-child + td + td
在 Bootstrap md 和更大尺寸的屏幕中,它应该在单行上显示所有 4 列,而在 sm 屏幕中,它应该在每行上显示 2 列。因此,内联 CSS 和 HTML 将与下面的 HTML 片段一样。但是我的 HTML 是动态的,我只想用 class="bootStrapRow4Cols"
标记外部 div
或 table
并将 CSS 应用于所有 < strong>cols 是动态的,所以我需要使用 CSS 和 Bootstrap 复制第一个子列、第二个子列等语法。有什么想法吗?
<div class="row">
<div class="col-md-2 col-sm-4"></div>
<div class="col-md-4 col-sm-8"></div>
<div class="col-md-2 col-sm-4"></div>
<div class="col-md-4 col-sm-8"></div>
</div>
我的 4 列表格的 CSS 如下:
/* tbl4Cols: Used to display entities in 4 columns:
Label1: | Control1 | Label2 | Control2
*/
.tbl4Cols {
width: 100%;
border-collapse: collapse;
border-spacing: 0;
margin-top: 0.75em;
border: 0 none;
}
.tbl4Cols tr {
border: none;
border-width: 1px;
padding-bottom: 2px;
padding-top: 2px;
}
.tbl4Cols td {
border: none;
border-width: 0px;
padding: 2px 1em 2px 1em;
}
/*lable1 column*/
.tbl4Cols td:first-child {
text-align: right;
width: 20%;
max-width: 20%;
min-width: 20%;
}
/*control1 column*/
.tbl4Cols td:first-child + td {
width: 30%;
max-width: 30%;
min-width: 30%;
}
/*lable2 column*/
.tbl4Cols td:first-child + td + td {
text-align: right;
width: 20%;
max-width: 20%;
min-width: 20%;
}
/*control2 column*/
.tbl4Cols td:first-child + td + td + td {
width: 30%;
max-width: 30%;
min-width: 30%;
}
最佳答案
从您的示例标记来看,仅使用 tr:nth-child(odd) { ... }
和 tr:nth-child(even) { .. . }
。
至于在较小的屏幕分辨率下堆叠,下面是您将在 Bootstrap.css 中找到的内容。
table col[class*="col-"] {
position: static;
display: table-column;
float: none;
}
table td[class*="col-"],
table th[class*="col-"] {
position: static;
display: table-cell;
float: none;
}
您可以使用@media 查询来使用 float: left;
将单元格堆叠在 col-sm-* 而不是 float: none;
。只需确保在 bootstrap.css 或 bootstrap.min.css 之后引用自定义样式表即可。
希望这能让您朝着正确的方向开始。
关于css - Bootstrap - 为 4 列表定义 Css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40378504/