Luiz
Luiz

Reputation: 1315

"Centering" images with bootstrap's grid

Explanation

I trying to "center" 3 images with Bootstrap's grid class col-lg-3 (>=1200px), but for some reason, when using a 273x303 image, it doesn't center; however, it does with a 670x670 one, as you can see in the code below.

Am I doing anything wrong? I got kinda confused since 670x670 works great.

Code

273x303

You can also see it's fiddle.

.main {
    background-color: blue;
    position: relative;
}

h2 {
  color: black !important;
}

h4, span {
  color: red !important;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>273x303</title>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    
    <body>

        <section class="main">
            <div class="container">
                <div class="row">

                    <div class="col-md-12 title text-center">
                        <h2>273x303</h2>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 1</h4>
                        <span>testing subtitle 1</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 2</h4>
                        <span>testing subtitle 2</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 3</h4>
                        <span>testing subtitle 3</span>
                    </div>

                </div>
            </div>
        </section>

        <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </body>
</html>

670x670

You can also see it's fiddle.

.main {
    background-color: blue;
    position: relative;
}

h2 {
  color: black !important;
}

h4, span {
  color: red !important;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>670x670</title>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    
    <body>

        <section class="main">
            <div class="container">
                <div class="row">

                    <div class="col-md-12 title text-center">
                        <h2>670x670</h2>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/LBFo66H.png" alt="Blog Post">
                        <h4>testing title 1</h4>
                        <span>testing subtitle 1</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/LBFo66H.png" alt="Blog Post">
                        <h4>testing title 2</h4>
                        <span>testing subtitle 2</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/LBFo66H.png" alt="Blog Post">
                        <h4>testing title 3</h4>
                        <span>testing subtitle 3</span>
                    </div>

                </div>
            </div>
        </section>

        <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </body>
</html>

Thanks in advance,
Luiz.

Upvotes: 2

Views: 218

Answers (4)

Jesse Johnson
Jesse Johnson

Reputation: 1718

Try adding display: initial; to the .img-responsive class. The snippet below has been updated.

NOTE: The use of !important is not required.

.main {
    background-color: blue;
    position: relative;
}

h2 {
  color: black !important;
}

h4, span {
  color: red !important;
}

.img-responsive {
   display: initial !important;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>273x303</title>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    
    <body>

        <section class="main">
            <div class="container">
                <div class="row">

                    <div class="col-md-12 title text-center">
                        <h2>273x303</h2>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 1</h4>
                        <span>testing subtitle 1</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 2</h4>
                        <span>testing subtitle 2</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 3</h4>
                        <span>testing subtitle 3</span>
                    </div>

                </div>
            </div>
        </section>

        <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </body>
</html>

Upvotes: 2

Brad
Brad

Reputation: 8668

.img-responsive {
  margin: 0 auto;
}

http://jsfiddle.net/6sstpz5q/2/

Upvotes: 2

Michael Coker
Michael Coker

Reputation: 53674

Utilize the .center-block class in bootstrap on the images to center them. That will apply a left/right margin of auto, which will center the block element that results from using .img-responsive

.main {
  background-color: blue;
  position: relative;
}

h2 {
  color: black;
}

h4,
span {
  color: red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<section class="main">
  <div class="container">
    <div class="row">

      <div class="col-md-12 title text-center">
        <h2>273x303</h2>
      </div>

      <div class="col-lg-4 master text-center">
        <img class="img-responsive center-block" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
        <h4>testing title 1</h4>
        <span>testing subtitle 1</span>
      </div>

      <div class="col-lg-4 master text-center">
        <img class="center-block img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
        <h4>testing title 2</h4>
        <span>testing subtitle 2</span>
      </div>

      <div class="col-lg-4 master text-center">
        <img class="center-block img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
        <h4>testing title 3</h4>
        <span>testing subtitle 3</span>
      </div>

    </div>
  </div>
</section>

Upvotes: 3

Ave
Ave

Reputation: 4430

Class .img-responsive from Bootstrap already sets display: block, you can use margin: 0 auto to center.

Add this line:

.master .img-responsive {
   margin: 0 auto;
}

.main {
    background-color: blue;
    position: relative;
}

h2 {
  color: black !important;
}

h4, span {
  color: red !important;
}

.master .img-responsive {
   margin: 0 auto;
}
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>273x303</title>

        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    </head>
    
    <body>

        <section class="main">
            <div class="container">
                <div class="row">

                    <div class="col-md-12 title text-center">
                        <h2>273x303</h2>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 1</h4>
                        <span>testing subtitle 1</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 2</h4>
                        <span>testing subtitle 2</span>
                    </div>

                    <div class="col-lg-4 master text-center">
                        <img class="img-responsive" src="http://i.imgur.com/3Eqr1Gf.png" alt="Blog Post">
                        <h4>testing title 3</h4>
                        <span>testing subtitle 3</span>
                    </div>

                </div>
            </div>
        </section>

        <script src="https://code.jquery.com/jquery-3.1.1.js" integrity="sha256-16cdPddA6VdVInumRGo6IbivbERE8p7CQR3HzTBuELA=" crossorigin="anonymous"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

    </body>
</html>

Upvotes: 3

Related Questions