A Simple Bootstrap Check Box for MVC

A Simple Bootstrap Check Box for MVC

I really enjoy working with Bootstrap and extending the styles to match what my customers want. Recently my customer wanted a simple push button style of check box and radio buttons. This turned out to be very easy to accomplish using the built-in Bootstrap styles and just a little extra CSS.

 

Simple Check Box

As a way to get a better looking check box, you can wrap a check box into a button as shown in Figure 1. What is nice about this style of check box is it really puts the label together with the check box. In addition, on a mobile device it is much easier to hit as the whole button area is clickable.

Figure 1: Check boxes wrapped into a button

To create these check boxes you simply have to wrap the check box within the normal Bootstrap class “checkbox”. You then add a label element with the classes “btn btn-default” around the check box as shown in the following code:

@using (Html.BeginForm())

{

  <div class="well well-sm">

    <div class="form-group">

      <div class="checkbox">

        <label class="btn btn-default">

          @Html.CheckBox("IsJazz")

          Jazz

        </label>

      </div>

    </div>

    <div class="form-group">

      <div class="checkbox">

        <label class="btn btn-default">

          @Html.CheckBox("IsCountry")

          Country

        </label>

      </div>

    </div>

    <div class="form-group">

      <div class="checkbox">

        <label class="btn btn-default">

          @Html.CheckBox("IsRock")

          Rock

        </label>

      </div>

    </div>

  </div>

}

To make it appear correctly, you have to add the following CSS to align the check box and the text correctly within the button. That is all there is to it!

<style>

  .checkbox .btn,

  .checkbox-inline .btn {

    padding-left: 2em;

    min-width: 7em;

  }

 

  .checkbox label,

  .checkbox-inline label {

    text-align: left;

  }

</style>

Summary

Creating better looking controls for your web page is sometimes very simple and just involves combing a couple of elements together along with a tiny bit of CSS.

Past Blog Content

Blog Archive

No Comments