Bootstrap 3 responsive with multiple break points

12,916

Solution 1

It doesn't appear that you are using Bootstrap 3 RC1. The link in your fiddle is broken. Bootstrap RC1 doesn't have .row-fluid anymore.

You could just simplify everything by letting the responsive features in 3 do the work for you. BS3 now has 3 grid sizes -- tiny, small and large that are used to manipulate display on different devices / widths. You could do something like this..

<div class="row">
    <div class="col-lg-3 col-sm-4 col-6">
        <a href="#" class="thumbnail">
             <img src="http://placehold.it/350x150" class="img-responsive">
        </a>
    </div>
    <div class="col-lg-3 col-sm-4 col-6">
        <a href="#" class="thumbnail">
             <img src="http://placehold.it/350x150" class="img-responsive">
        </a>
    </div>

     ...
</div>

That will get you..

  • 4 per row on large screens (col-lg-3)
  • 3 per row on tablets (col-sm-4)
  • 2 per row on "tiny" phones (col-6)

Demo: http://bootply.com/70929

Solution 2

Just changed some css and it works. See this fiddle: http://jsfiddle.net/J3F99/

Well, the major changes being:

  1. box-sizing: border-box to .group so that the % width is inclusive of the padding(10px).
  2. Added display: inline-block to and removed the float: left in .group, and text-align: center to .row-fluid so that the .group elements are centre aligned.
  3. Added max-width: 350px and max-height: 150px to .group
  4. Adjusted % widths for different screen sizes to take up the full width and still maintaining the max-width
  5. Cleaned up css in media queries by removing duplicate properties that is already inherited from common style for .group for all screens

Thats it. It works well in chrome. I din't test in other browsers. Use full bootstrap while testing.

The main problem here is the padding, given in pixels. To ensure clean fluid layout you should use % instead if pixels. And for that you have to use margin rather than padding for .group. Because, margin in % depends on the container width.

For example, 4 .group elements with width 20% and margin-right and margin-left 2.5% will fit exactly whithin the container(.row-fluid here) without leaving out a single pixel.

4*20 + 4*2.5*2 = 100%

I recommend you to go with that method. See @Bharat Parmar's answer for that.

Share:
12,916
charliexx
Author by

charliexx

Updated on June 04, 2022

Comments

  • charliexx
    charliexx almost 2 years

    I'm using latest Bootstrap 3.0 RC1 and trying to build an image overview with responsiveness and multiple break points when the images becomes too small (like seen on Dribbble).

    Issues:

    1. The image scaling only appears when there's two or less on an line (it should work with 3-4 images as well)
    2. My break lines OR the responsiveness cause different image sizes. I need to make sure that the max img size after breaks is the same as max size when there's 4 on a row. Example when breaking into two or one the images will initially appear much larger than the largest size with 4 in a row.

    I hope someone can help me out, I'm a beginner to building responsive stuff..

    Best regards

    Link to jsfiddle:

    http://jsfiddle.net/6dckB/ (your browser must be wide to see the effects)

    HTML:

    <div class="container">
        <ul class="row-fluid">
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
            <li class="group">
                <div class="img-thumbnail">
                    <a href="#">
                        <img src="http://placehold.it/350x150" style="width:100%;">
                    </a>
                </div>
            </li>
        </ul>
    </div>
    

    CSS:

    .row-fluid {
      padding-left: 0;
      list-style: none;
    }
    .row-fluid:before,
    .row-fluid:after {
      content: " ";
      /* 1 */
    
      display: table;
      /* 2 */
    
    }
    .row-fluid:after {
      clear: both;
    }
    .row-fluid:before,
    .row-fluid:after {
      content: " ";
      /* 1 */
    
      display: table;
      /* 2 */
    
    }
    .row-fluid:after {
      clear: both;
    }
    @media (min-width: 768px) {
      .row-fluid {
        margin-left: -10px;
        margin-right: -10px;
      }
    }
    .row-fluid .row {
      margin-left: -10px;
      margin-right: -10px;
    }
    .row-fluid .group {
      position: relative;
      min-height: 1px;
      padding-left: 10px;
      padding-right: 10px;
      float: left;
      width: 50%;
      margin-bottom: 20px;
    }
    @media (max-width: 400px) {
      .row-fluid .group {
        position: relative;
        min-height: 1px;
        padding-left: 10px;
        padding-right: 10px;
        float: left;
        width: 100%;
      }
    }
    @media (min-width: 768px) and (max-width: 991px) {
      .row-fluid .group {
        position: relative;
        min-height: 1px;
        padding-left: 10px;
        padding-right: 10px;
        float: left;
        width: 33.33333333333333%;
      }
    }
    @media (min-width: 992px) {
      .row-fluid .group {
        position: relative;
        min-height: 1px;
        padding-left: 10px;
        padding-right: 10px;
        float: left;
        width: 25%;
      }
    }