Responsive image align center bootstrap 3

前端 未结 18 1013
半阙折子戏
半阙折子戏 2020-11-29 14:16

I do a catalog using Bootstrap 3. When displayed on tablets, the product images look ugly because of their small size (500x500) and a width of 767 pixels in the browser. I w

相关标签:
18条回答
  • 2020-11-29 14:53
    <div class="col-md-12 text-center">
        <img class="img-responsive tocenter" />
    </div>
    

    .

    <style>
       .tocenter {
        margin:0 auto;
        display: inline;
        }
    </style>
    
    0 讨论(0)
  • 2020-11-29 14:53

    You can use property of d-block here or you can use a parent div with property 'text-center' in bootstrap or 'text-align: center' in css.

    Image by default is displayed as inline-block, you need to display it as block in order to center it with .mx-auto. This can be done with built-in .d-block:

    <div>
        <img class="mx-auto d-block" src="...">  
    </div>
    

    Or leave it as inline-block and wrapped it in a div with .text-center:

    <div class="text-center">
        <img src="...">  
    </div>
    
    0 讨论(0)
  • 2020-11-29 14:54

    There is .center-block class in Twitter Bootstrap 3 (Since v3.0.1), so use:

    <img src="..." alt="..." class="img-responsive center-block" />
    
    0 讨论(0)
  • 2020-11-29 14:55

    I would suggest a more "abstract" classification. Add a new class "img-center" which can be used in combination with .img-responsive class:

    // Center responsive images
    .img-responsive.img-center {
      margin: 0 auto;
    }
    
    0 讨论(0)
  • 2020-11-29 14:56

    So far the best solution to accept seems to be <img class="center-block" ... />. But no one has mentioned how center-block works.

    Take Bootstrap v3.3.6 for example:

    .center-block {
      display: block;
      margin-right: auto;
      margin-left: auto;
    }
    

    The default value of dispaly for <img> is inline. Value block will display an element as a block element (like <p>). It starts on a new line, and takes up the whole width. In this way, the two margin settings let the image stay in the middle horizontally.

    0 讨论(0)
  • 2020-11-29 14:59

    To add to the answers already given, having the img-responsive in combination with img-thumbnail will set display: block to display: inline block.

    0 讨论(0)
提交回复
热议问题