How to Customize Bootstrap Column Widths?
Asked Answered
A

6

52

I have this, but I feel 4 is too big for my sidebar width and 3 is too small (it has to add up to 12).

  <div class="col-md-8">
  <div class="col-md-4">

I tried this but it doesn't work:

  <div class="col-md-8.5">
  <div class="col-md-3.5">

Is there another way to get a similar outcome?

Thanks for your help!

Artefact answered 26/2, 2015 at 19:16 Comment(2)
Changing to a 16- or 24-column layout for more flexibility: #11591685Nauseous
Is your site responsive? It's best to configure your content to flow more easily. Few sidebars are always the same width in today's web.Nauseous
D
33

To expand on @isherwood's answer, here is the complete code for creating custom -sm- widths in Bootstrap 3.3

In general you want to search for an existing column width (say col-sm-3) and copy over all the styles that apply to it, including generic ones, over to your custom stylesheet where you define new column widths.

.col-sm-3half, .col-sm-8half {
    position: relative;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

@media (min-width: 768px) {
    .col-sm-3half, .col-sm-8half {
        float: left;
    }
    .col-sm-3half {
        width: 29.16666667%;
    }
    .col-sm-8half {
        width: 70.83333333%;
    }
}
Denny answered 10/7, 2015 at 10:11 Comment(0)
G
25

For a 12 columns grid, if you want to add half of a column (4,16667%) to each column width. This is what you do.

For example, for col-md-X, define .col-md-X-5 with the following values.

.col-md-1-5 { width: 12,5%; } // = 8,3333 + 4,16667
.col-md-2-5 { width: 20,83333%; } // = 16,6666 + 4,16667
.col-md-3-5 { width: 29,16667%; } // = 25 + 4,16667
.col-md-4-5 { width: 37,5%; } // = 33,3333 + 4,16667
.col-md-5-5 { width: 45,83333%; } // = 41,6667 + 4,16667
.col-md-6-5 { width: 54,16667%; } // = 50 + 4,16667
.col-md-7-5 { width: 62,5%; } // = 58,3333 + 4,16667
.col-md-8-5 { width: 70,83333%; } // = 66,6666 + 4,16667
.col-md-9-5 { width: 79,16667%; } // = 75 + 4,16667
.col-md-10-5 { width: 87,5%; } // = 83,3333 + 4,16667
.col-md-11-5 { width: 95,8333%; } // = 91,6666 + 4,16667

I rounded certain values.

Secondly, to avoid copying css code from the original col-md-X, use them in the class declaration. Be careful that they should be added before your modified ones. That way, only the width gets override.

<div class="col-md-2 col-md-2-5">...</div>
<div class="col-md-5">...</div>
<div class="col-md-4 col-md-4-5">...</div>

Finally, don't forget that the total should not exceed 12 columns, which total 100%.

I hope it helps!

Gangland answered 19/9, 2016 at 13:20 Comment(0)
N
8

You could certainly create your own classes:

.col-md-3point5 {width: 28.75%}
.col-md-8point5 {width: 81.25%;}

I'd do this before I'd mess with the default columns. You may want to use those inside these.

You'd probably also want to put those inside a media query statement so that they only apply for larger-than-mobile screen sizes.

Nauseous answered 26/2, 2015 at 19:29 Comment(0)
V
7

You can use Bootstrap's own column mixins make-xx-column():

.col-md-8half {
    .make-md-column(8.5);
}

.col-md-3half {
    .make-md-column(3.5);
}
Vireo answered 25/10, 2017 at 22:3 Comment(0)
P
7

Bootstrap 4.1+ version of Antoni's answer:

The Bootstrap mixin is now @include make-col($size, $columns: $grid-columns)

.col-md-8half {
  @include make-col-ready();

  @include media-breakpoint-up(md) {
    @include make-col(8.5);
  }
}

.col-md-3half {
  @include make-col-ready();

  @include media-breakpoint-up(md) {
    @include make-col(3.5);
  }
}

Source:

Prevenient answered 20/3, 2019 at 14:42 Comment(0)
C
4

you can customize bootstrap stylesheet, as in:

.col-md-8{
  width: /*as you wish*/;
}

Then, set the media query for that too, as in:

@media screen and (max-width:768px){
  .col-md-8{
     width:99%;
   }
}
Colp answered 26/2, 2015 at 19:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.