How I can make nice looking matrix of buttons with Bootstrap 3?
Asked Answered
M

5

9

I have something like that:

<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>

And I would like to have a 3x3 matrix of buttons. One more thing, left and right side must look like this example (straight lines):

<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>

How I can make it? Maybe I need to add some Bootstrap class or edit the CSS file?

Maraschino answered 8/7, 2016 at 7:38 Comment(0)
M
23

One group of buttons + few pseudo-classes

  1. Use only one block with the .btn-group class.

  2. Apply a set of CSS properties by using of pseudo-classes:

  1. The clear: left; property forces the button to start a new row of the matrix. It's because the .btn class has the float: left; property.

  2. Set up border-radius and margin properties in a similar way as the btn-group class is described in the bootstrap.css file.

Three column button matrix with 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>

X×Y matrix with Bootstrap 3

The code depends only on 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;
}

Three column button matrix with Bootstrap 4 or 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>

X×Y matrix with Bootstrap 4 or 5

The code depends only on 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;
}
Merrileemerrili answered 8/7, 2016 at 13:27 Comment(2)
Thanks a lot. It is exactly I have asked aboutMaraschino
This mostly still works in bootstrap 4, but you need to add .btn-matrix { flex-wrap: wrap; } or the buttons will not wrap around.Tigress
D
1

Modified @Pranjal answer a bit because of following reason:

  • the bootstrap grid (row, col-... etc) creates rows which are by definition 12 columns wide

Because of this we create a div with class row for each row of 3 buttons. Also, we want the buttons the be 1/3 of the width of the row (12 columns) so we give it the class "col-md-4" (12 divided by 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>
Deutschland answered 8/7, 2016 at 7:52 Comment(4)
Thanks for your answer. Can I make left and right sides of matrix more straight? (like .btn-group-vertical class makes it)Maraschino
Then you would have to make one row, which contains 3 columns (col-md-4 each). In each column you would place a vertical button group. However this will give the middle group the same layout so you have to play around a bit to find the desired result.Deutschland
I would like to have straight lines at the top and bottom of the matrix, so it doesn't solve the problem. I think about merging .btn-group and .btn-group-vertical in some way, maybe, by edite css files or overwrite them. But I'm not good at css.Maraschino
Yes custom CSS will be needed then. Give it a try and make a new question when you are stuck :) This question can be closed I assume.Deutschland
C
0

You can try the following:

    <div class="row">
        <div class="col-md-1 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="col-md-1 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="col-md-1 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>
Colic answered 8/7, 2016 at 7:41 Comment(1)
thanks. what about straight lines of the sides of matrix?Maraschino
S
0

I Think this will be helpful to you.Try this one.

button{
  margin-right:14px!important;
  }
<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-3">Button 1</button>
      <button type="button" class="btn btn-default col-md-3">Button 2</button>
      <button type="button" class="btn btn-default col-md-3">Button 3</button>
    </div>
  </div>
  <div class="row">
    <div class=" btn-group">
      <button type="button" class="btn btn-default col-md-3">Button 4</button>
      <button type="button" class="btn btn-default col-md-3">Button 5</button>
      <button type="button" class="btn btn-default col-md-3">Button 6</button>
    </div>
  </div>
  <div class="row">
    <div class=" btn-group">
      <button type="button" class="btn btn-default col-md-3">Button 7</button>
      <button type="button" class="btn btn-default col-md-3">Button 8</button>
      <button type="button" class="btn btn-default col-md-3">Button 9</button>
    </div>
  </div>
</div>
Splutter answered 8/7, 2016 at 14:11 Comment(1)
Is this the thing you want?Splutter
G
0

For Bootstrap 4 and if you have unequal text that needs to wrap.

.btn-matrix > .btn:nth-child(3n+4) {
  clear: left;
  margin-left: 0;
}
.btn-matrix > .btn:nth-child(n+4) {
  margin-top: -1px;
}
.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 { flex-wrap: wrap; }
/* force wrap text */
 .btn-matrix .btn{
white-space: normal !important;
}


<div class="btn-group btn-matrix">
  <a class="btn btn-sm btn-default col-sm-4">Button 1 Extra Content</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 2</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 3</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 4</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 5</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 6</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 7</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 8</a>
  <a  class="btn btn-sm btn-default col-sm-4">Button 9</a>
</div>
Gathering answered 10/10, 2018 at 20:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.