使用bootstrap的网格“居中”图像

时间:2017-03-08 02:16:18

标签: html css twitter-bootstrap

说明

我尝试使用Bootstrap的网格类col-lg-3(> = 1200px)“居中”3个图像,但出于某种原因,当使用273x303图像时,它不会居中;但是,它可以使用670x670,如下面的代码所示。

我做错了吗?我有点困惑,因为670x670效果很好。

代码

273x303

您还可以看到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

您还可以看到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>

提前致谢,
路易斯。

4 个答案:

答案 0 :(得分:3)

来自Bootstrap的

.img-responsive已设置display: block,您可以使用margin: 0 auto来居中。

添加以下行:

.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>

答案 1 :(得分:3)

在图像的bootstrap中使用.center-block类来居中它们。这将应用auto的左/右边距,这将使使用.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>

答案 2 :(得分:2)

.img-responsive {
  margin: 0 auto;
}

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

答案 3 :(得分:2)

尝试将display: initial;添加到.img-responsive课程。下面的代码段已更新。

注意:不需要使用!important

.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>