如何使用 Bootstrap 3 制作漂亮的按钮矩阵?

新手上路,请多包涵

我有这样的东西:

 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container">
  <div class="row">
    <div class="col-sm-6">

      <div class="btn-group">
        <button type="button" class="btn btn-default">Button 1</button>
        <button type="button" class="btn btn-default">Button 2</button>
        <button type="button" class="btn btn-default">Button 3</button>
      </div>

      <div class="btn-group">
        <button type="button" class="btn btn-default">Button 4</button>
        <button type="button" class="btn btn-default">Button 5</button>
        <button type="button" class="btn btn-default">Button 6</button>
      </div>

      <div class="btn-group">
        <button type="button" class="btn btn-default">Button 7</button>
        <button type="button" class="btn btn-default">Button 8</button>
        <button type="button" class="btn btn-default">Button 9</button>
      </div>

    </div>

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

我想要一个 3x3 的按钮矩阵。还有一件事,左侧和右侧必须看起来像这个例子(直线):

 <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<div class="container">
  <div class="btn-group-vertical">
    <button type="button" class="btn btn-default">button</button>
    <button type="button" class="btn btn-default">button</button>
    <button type="button" class="btn btn-default">button</button>
  </div>
</div>

我怎样才能做到?也许我需要添加一些 Bootstrap 类或编辑 CSS 文件?

原文由 Vadym Perepeliak 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 415
2 个回答

一组按钮+几个伪类

  1. 仅使用一个块与 .btn-group 类。

  2. 使用伪类应用一组 CSS 属性:

  1. clear: left; 属性强制按钮开始矩阵的新行。这是因为 .btn 类具有 float: left; 属性。

  2. 设置 border-radiusmargin 属性以类似于 btn-group 类的方式在 bootstrap.cs 文件中描述。

带 Bootstrap 3 的三列按钮矩阵

https://codepen.io/glebkema/pen/bGWWMRz

 @import "https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css";

/* Arrange buttons */
.btn-matrix>.btn {
  width: 33%; /* force buttons to have the same width regardless of content */
}
.btn-matrix>.btn:nth-child(3n + 4) {
  clear: left; /* force the button to start a new row of the matrix
                  (because .btn adds the `float: left;` property) */
  margin-left: 0; /* because .btn-group adds `margin-left: -1px;` to all buttons */
}
.btn-matrix>.btn:nth-child(n + 4) {
  margin-top: -1px; /* superimpose borders of the buttons from adjacent rows */
}

/* Fix border radius */
.btn-matrix>.btn:first-child {
  border-bottom-left-radius: 0;
}
.btn-matrix>.btn:nth-child(3) {
  border-top-right-radius: 4px !important;
}
.btn-matrix>.btn:nth-last-child(3) {
  border-bottom-left-radius: 4px !important;
}
.btn-matrix>.btn:last-child {
  border-top-right-radius: 0;
}

/* Decorations */
.btn-matrix {
  margin: 20px;
}
 <div class="btn-group btn-matrix">
  <button type="button" class="btn btn-default">Button 1</button>
  <button type="button" class="btn btn-default">Button 2</button>
  <button type="button" class="btn btn-default">Button 3</button>
  <button type="button" class="btn btn-default">Button 4</button>
  <button type="button" class="btn btn-default">Button 5</button>
  <button type="button" class="btn btn-default">Button 6</button>
  <button type="button" class="btn btn-default">Button 7</button>
  <button type="button" class="btn btn-default">Button 8</button>
  <button type="button" class="btn btn-default">Button 9</button>
  <button type="button" class="btn btn-default">Button 10</button>
  <button type="button" class="btn btn-default">Button 11</button>
  <button type="button" class="btn btn-default">Button 12</button>
  <button type="button" class="btn btn-default">Button 13</button>
  <button type="button" class="btn btn-default">Button 14</button>
  <button type="button" class="btn btn-default">Button 15</button>
</div>
使用 Bootstrap 3 的 X×Y 矩阵

代码仅依赖于 X:

 .btn-matrix > .btn {
  width: (100/X)%;
}
.btn-matrix > .btn:nth-child(Xn+X+1) {
  clear: left;
  margin-left: 0;
}
.btn-matrix > .btn:nth-child(n+X+1) {
  margin-top: -1px;
}

.btn-matrix > .btn:first-child {
  border-bottom-left-radius: 0;
}
.btn-matrix > .btn:nth-child(X) {
  border-top-right-radius: 4px !important;
}
.btn-matrix > .btn:nth-last-child(X) {
  border-bottom-left-radius: 4px !important;
}
.btn-matrix > .btn:last-child {
  border-top-right-radius: 0;
}

带有 Bootstrap 4 或 5 的三列按钮矩阵

https://codepen.io/glebkema/pen/ZEKKoJG

 @import "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css";

/* Arrange buttons */
.btn-matrix {
   flex-wrap: wrap; /* allow buttons to jump to another row */
}
.btn-matrix > .btn {
   width: 33%; /* force buttons to have the same width regardless of content */
}
.btn-matrix > .btn:nth-child(3n + 4) {
   margin-left: 0; /* because .btn-group adds `margin-left: -1px;` to all buttons */
}
.btn-matrix > .btn:nth-child(n + 4) {
   margin-top: -1px; /* superimpose borders of the buttons from adjacent rows */
}

/* Fix border radius */
.btn-matrix > .btn:first-child {
   border-bottom-left-radius: 0;
}
.btn-matrix > .btn:nth-child(3) {
   border-top-right-radius: 4px !important;
}
.btn-matrix > .btn:nth-last-child(3) {
   border-bottom-left-radius: 4px !important;
}
.btn-matrix > .btn:last-child {
   border-top-right-radius: 0;
}

/* Decorations */
.btn-matrix {
   margin: 20px;
   max-width: 500px;
}
 <div class="btn-group btn-matrix" role="group" aria-label="Three Column Button Matrix">
  <button type="button" class="btn btn-outline-primary">Button 1</button>
  <button type="button" class="btn btn-outline-primary">Button 2</button>
  <button type="button" class="btn btn-outline-primary">Button 3</button>
  <button type="button" class="btn btn-outline-primary">Button 4</button>
  <button type="button" class="btn btn-outline-primary">Button 5</button>
  <button type="button" class="btn btn-outline-primary">Button 6</button>
  <button type="button" class="btn btn-outline-primary">Button 7</button>
  <button type="button" class="btn btn-outline-primary">Button 8</button>
  <button type="button" class="btn btn-outline-primary">Button 9</button>
  <button type="button" class="btn btn-outline-primary">Button 10</button>
  <button type="button" class="btn btn-outline-primary">Button 11</button>
  <button type="button" class="btn btn-outline-primary">Button 12</button>
  <button type="button" class="btn btn-outline-primary">Button 13</button>
  <button type="button" class="btn btn-outline-primary">Button 14</button>
  <button type="button" class="btn btn-outline-primary">Button 15</button>
</div>
使用 Bootstrap 4 或 5 的 X×Y 矩阵

代码仅依赖于 X:

 .btn-matrix {
  flex-wrap: wrap;
}
.btn-matrix > .btn {
  width: (100/X)%;
}
.btn-matrix > .btn:nth-child(Xn+X+1) {
  margin-left: 0;
}
.btn-matrix > .btn:nth-child(n+X+1) {
  margin-top: -1px;
}

.btn-matrix > .btn:first-child {
  border-bottom-left-radius: 0;
}
.btn-matrix > .btn:nth-child(X) {
  border-top-right-radius: 4px !important;
}
.btn-matrix > .btn:nth-last-child(X) {
  border-bottom-left-radius: 4px !important;
}
.btn-matrix > .btn:last-child {
  border-top-right-radius: 0;
}

原文由 Gleb Kemarsky 发布,翻译遵循 CC BY-SA 4.0 许可协议

由于以下原因,修改了@Pranjal 的回答:

  • bootstrap 网格(row、col-…等)根据定义创建 12 列宽的行

因此,我们为每行 3 个按钮创建一个带有类 row 的 div。此外,我们希望按钮是行宽的 1/3(12 列),因此我们将其命名为“col-md-4”类(12 除以 3)。

 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/>

<div class="container-fluid">
  <div class="row">
    <div class="btn-group">
      <button type="button" class="btn btn-default col-md-4">Button 1</button>
      <button type="button" class="btn btn-default col-md-4">Button 2</button>
      <button type="button" class="btn btn-default col-md-4">Button 3</button>
    </div>
  </div>
  <div class="row">
    <div class=" btn-group">
      <button type="button" class="btn btn-default col-md-4">Button 4</button>
      <button type="button" class="btn btn-default col-md-4">Button 5</button>
      <button type="button" class="btn btn-default col-md-4">Button 6</button>
    </div>
  </div>
  <div class="row">
    <div class=" btn-group">
      <button type="button" class="btn btn-default col-md-4">Button 7</button>
      <button type="button" class="btn btn-default col-md-4">Button 8</button>
      <button type="button" class="btn btn-default col-md-4">Button 9</button>
    </div>
  </div>
</div>

原文由 Ruben Pirotte 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题