css - Bootstrap - 为 4 列表定义 Css

标签 css twitter-bootstrap

我有一个 4 列表,CSS 如下。它使用第一个 child + td 语法在 css 中定义列的大小。例如第三列是:

.tbl4Cols td:first-child + td + td

在 Bootstrap md 和更大尺寸的屏幕中,它应该在单行上显示所有 4 列,而在 sm 屏幕中,它应该在每行上显示 2 列。因此,内联 CSS 和 HTML 将与下面的 HTML 片段一样。但是我的 HTML 是动态的,我只想用 class="bootStrapRow4Cols" 标记外部 divtable 并将 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/

相关文章:

javascript - material-ui CSS 的正确使用方法是什么?

javascript - 来自 JSON 的 D3.JS 分组圆环图

javascript - 如何更改点击或提交表单时的网址

javascript - 如果表单字段的输入值为空,则尝试使用 jQuery 显示一个 div

twitter-bootstrap - react-router-dom 与 react-bootstrap 导航

html - div 相同的周围边距

jquery - 如何使用 jquery 和 css 对齐动画菜单

html - 蓝色按钮在 iPhone 上显示为银色

html - slick.js - 为什么在使用 Bootstrap 行时克隆第一张幻灯片?

android - Cordova Android 相机插件在捕获后不显示图像