Center image using text-align center?

后端 未结 24 1598
终归单人心
终归单人心 2020-11-21 17:53

Is the property text-align: center; a good way to center an image using CSS?

img {
    text-align: center;
}
相关标签:
24条回答
  • 2020-11-21 18:18

    That doesn't always work... if it doesn't, try:

    img {
        display: block;
        margin: 0 auto;
    }
    
    0 讨论(0)
  • 2020-11-21 18:18
    img{
        display: block;
        margin-right: auto;
        margin-left: auto;      
     }
    
    0 讨论(0)
  • 2020-11-21 18:20

    Another way of doing it would be centering an enclosing paragraph:

    <p style="text-align:center; border:1px solid black"><img src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-icon.png?v=c78bd457575a"></p>

    0 讨论(0)
  • 2020-11-21 18:21
    .img-container {
      display: flex;
    }
    
    img {
      margin: auto;
    }
    

    this will make the image center in both vertically and horizontally

    0 讨论(0)
  • 2020-11-21 18:22

    You can do:

    <center><img src="..." /></center>

    0 讨论(0)
  • 2020-11-21 18:22

    I discovered that if I have an image and some text inside a div, then I can use text-align:center to align the text and the image in one swoop.

    HTML:

        <div class="picture-group">
            <h2 class="picture-title">Picture #1</h2>
            <img src="http://lorempixel.com/99/100/" alt="" class="picture-img" />
            <p class="picture-caption">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Temporibus sapiente fuga, quia?</p>
        </div>
    

    CSS:

    .picture-group {
      border: 1px solid black;
      width: 25%;
      float: left;
      height: 300px;
      #overflow:scroll;
      padding: 5px;
      text-align:center;
    }
    

    CodePen: https://codepen.io/artforlife/pen/MoBzrL?editors=1100

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