Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap Buttons

Bootstrap Buttons

Goals

Buttons are widely used in websites or applications. In this tutorial, we will discuss how to use Bootstrap 3 buttons through examples and explanations, and how to customize the default buttons.

Using default buttons

The following example demonstrates how to quickly create buttons with Bootstrap 3.

Example

     Bootstrap 3 default buttons

    

    

    

    

    

    

    

The above code outputs as follows. You canclick here to view the example online。

CSS

We will now explore the CSS rules used to create these buttons. The main class used to create buttons is btn, as shown below.



.btn {

  display: inline-block;

  padding: 6px 12px;

  margin-bottom: 0;

  font-size: 14px;

  font-weight: normal;

  line-height: 1.428571429;

  text-align: center;

  white-space: nowrap;

  vertical-align: middle;

  cursor: pointer;

  background-image: none;

  border: 1px solid transparent;

  border-radius: 4px;

  -webkit-user-select: none;

     -moz-user-select: none;

      -ms-user-select: none;

       -o-user-select: none;

          user-select: none;

}

Thanks to the CSS above, buttons (with the btn class) render as inline-block, allowing them to appear inline with adjacent elements sharing a common baseline, but you can add width and height properties to them. Their top padding and bottom padding are 6 and 12 pixels, respectively. The bottom margin is set to 0.

The font size is set to 14 pixels, and the font weight is set to normal. Buttons with the btn class have a line height of 1.428571429, slightly higher than the default of 1. Text is center-aligned.

The white-space property is set to nowrap, so new lines, spaces, and tabs will collapse. The relevant vertically centered elements will be positioned in the middle of the height. When you hover the mouse over the relevant elements, the cursor appears as a pointer. No background image is displayed here.

A transparent, 1-pixel-thick solid border will be present here. Due to border-radius: 4px;, the corners of the relevant buttons will be rounded. By increasing or decreasing the value of 4px, you can make the rounding more or less pronounced. Due to user-select: none, the text of the relevant buttons and their child elements will not be selectable. For cross-browser consistency, the same properties are applied with multiple vendor prefixes.

Each button shown in the example above, besides btn , also has another CSS class, such as btn-default, btn-primary, and so on. These classes are used to set the button's color、background-color and border properties. They are CSS rules for regular buttons and buttons with link states (i.e., hover, active, focus, etc.).

btn-primary The code for the class is as follows



.btn-primary {

  color: #ffffff;

  background-color: #428bca;

  border-color: #357ebd;

}

Buttons of different sizes

There are three sizes to choose from. To set each size, you need to add an additional class.

btn-lg for setting large button.





The CSS code for the btn-lg class is as follows



.btn-lg {

  padding: 10px 16px;

  font-size: 18px;

  line-height: 1.33;

  border-radius: 6px;

}

Use btn-sm and btn-xs Set buttons with different sizes.







The CSS code for the btn-sm and btn-xs classes is as follows



.btn-sm,

.btn-xs {

  padding: 5px 10px;

  font-size: 12px;

  line-height: 1.5;

  border-radius: 3px;

}



.btn-xs {

  padding: 1px 5px;

}

The code below demonstrates different types of buttons with different sizes. You canclick here to view the demo online. The screenshot of the effect and the code are shown below.

Example

     Bootstrap 3 buttons of different sizes

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

    

Block-level buttons

To use the full width of the button's container, Bootstrap 3 provides a block-level button option. You need to add the btn-block class to the classes discussed above to create a block-level button.





  

The CSS definition for creating a block-level button is as follows



.btn-block {

  display: block;

  width: 100%;

  padding-right: 0;

  padding-left: 0;

}



.btn-block + .btn-block {

  margin-top: 5px;

}

The screenshot and code of the block-level button are shown below. You can click here to view the example online。

Example

     Bootstrap 3 block-level buttons

    

    

    

    

    

    

    

    

        

            

            

                

                

                

            

            

                

                

                

                

            

            

                

                

                

                

            

            

                

                

                

                

            

            

                

                

                

                

            

            

                

                

                

                

            

            

                

                

                

            

        

    

Active buttons

You can use the active The CSS class to give a button or link a different appearance to show that it is active. Here is an Example Demonstration, screenshot, and code.

Example

     Bootstrap 3 active buttons

    

    

    

    

    

    

    

Using Chrome Developer Tools, we find that when a button has the active class added, the CSS rule takes effect.

Default active button



.btn-default:hover, .btn-default:focus, .btn-default:active, .btn-default.active, .open .dropdown-toggle.btn-default {

color: #333;

background-color: #ebebeb;

border-color: #adadad;

}

Primary active button



.btn-primary:hover, .btn-primary:focus, .btn-primary:active, .btn-primary.active, .open .dropdown-toggle.btn-primary {

color: #fff;

background-color: #3276b1;

border-color: #285e8e;

}

Success active button



.btn-success:hover, .btn-success:focus, .btn-success:active, .btn-success.active, .open .dropdown-toggle.btn-success {

color: #fff;

background-color: #47a447;

border-color: #398439;

}

Info active button



.btn-info:hover, .btn-info:focus, .btn-info:active, .btn-info.active, .open .dropdown-toggle.btn-info {

color: #fff;

background-color: #39b3d7;

border-color: #269abc;

}

Warning active button



.btn-warning:hover, .btn-warning:focus, .btn-warning:active, .btn-warning.active, .open .dropdown-toggle.btn-warning {

color: #fff;

background-color: #ed9c28;

border-color: #d58512;

}

Danger active button



.btn-danger:hover, .btn-danger:focus, .btn-danger:active, .btn-danger.active, .open .dropdown-toggle.btn-danger {

color: #fff;

background-color: #d2322d;

border-color: #ac2925;

}

Link active button



.btn-link, .btn-link:hover, .btn-link:focus, .btn-link:active {

border-color: transparent;

}

So you can see these are achieved by changing the color, background-color, and border-color properties.

Disabled buttons

You can use the disabled The CSS class to give a button or link a different appearance to show that it is disabled. Here is an Example Demonstration, screenshot, and code.

To make the button slightly blurred, use the CSS rule below (found via Chrome Developer Tools)



.btn[disabled], fieldset[disabled] .btn {

pointer-events: none;

cursor: not-allowed;

opacity: .65;

filter: alpha(opacity=65);

-webkit-box-shadow: none;

box-shadow: none;

}

Example

     Bootstrap 3 disabled buttons

    

    

    

    

    

    

    

Disabled anchor elements

When an anchor element with the btn and other related classes appears disabled, it is slightly different from usual. First, the anchor's link still exists; only its appearance and feel have changed. In addition, you cannot use them in navigation bars.

click here to view the demo online. The code is as follows:

Example

     Bootstrap 3 disabled anchor buttons

    

    

    

    

    

    

    

Default

Original

Success

Information

Warning

Danger

Please note,role="button" is used for convenience purposes.

Button tags

You can use the button class on button, a, and input elements to use a button as a label. However, it is recommended that you normally use it with the button element, otherwise it will cause cross-browser inconsistency issues.

The following example demonstrates how to use the button tag

Example

     Bootstrap 3 button tag example

    

    

    

    

    

    

    

    link

    

    

    

    link

    

    

    

    link

    

    

    

    link

    

    

    

    link

    

    

    

    link

    

    

    

Click here to view the demo online.

Customizing buttons

Now we will start customizing from the default button to give it a completely different look and feel.

We will start with the button code as follows





Now we want to create a CSS class btn-w3r, and use it to replace btn-default. So we will write the following code to replace the code above





First we will change the background color by adding the following CSS



.btn-w3r {

    background: #cb60b3; /* Old browsers */

    background: -moz-linear-gradient(top,  #cb60b3 0%, #ad1283 50%, #de47ac 100%); /* FF3.6+ */

    background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#cb60b3), color-stop(50%,#ad1283), color-stop(100%,#de47ac)); /   *   Chrome,Safari4+ */

    background: -webkit-linear-gradient(top,  #cb60b3 0%,#ad1283 50%,#de47ac 100%); /* Chrome10+,Safari5.1+ */

    background: -o-linear-gradient(top,  #cb60b3 0%,#ad1283 50%,#de47ac 100%); /* Opera 11.10+ */

    background: -ms-linear-gradient(top,  #cb60b3 0%,#ad1283 50%,#de47ac 100%); /* IE10+ */

    background: linear-gradient(to bottom,  #cb60b3 0%,#ad1283 50%,#de47ac 100%); /* W3C */

    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#cb60b3', endColorstr='#de47ac',GradientType=0 ); /* IE6-9 */

    }

Now the button looks as follows

To change the font color, we will append the following CSS after the existing btn-w3r class's CSS code



color:#fff;

Now, to make the button look round in shape, we will add the following CSS before the existing btn-w3r class's CSS code



width: 200px;

    height: 200px;

    -moz-border-radius: 50%;

    -webkit-border-radius: 50%;

    border-radius: 50%;

To add a hover effect, we will add the following CSS code



.btn-w3r:hover {

    background: #333; 

    color:#e75616;

    }

The button hover effect after adding the above CSS code is shown below

You can Click here to view the online demo of the custom button we created。