center image in a div

前端 未结 3 1940
天涯浪人
天涯浪人 2020-12-07 05:36

I\'m trying to center horizontally an image in a div, and i don\'t understand why it isn\'t working... I\'ve centered images so many times, and this time i just don\'t under

相关标签:
3条回答
  • 2020-12-07 05:41

    Check the below code, I hope it will helps you.

    .wrap{
      position: relative;
      height: 100vh;
      width: 100vw;
    }
    .midImg{
      margin: auto;  
      position: absolute;
      left:0;
      right: 0;
      top: 0;
      bottom: 0;
      height: 150px;
      width: 150px;
    }  
    <div class="wrap">
      <img src="https://pbs.twimg.com/profile_images/473506797462896640/_M0JJ0v8.png" alt="av61" class="midImg" />
    </div>

    0 讨论(0)
  • 2020-12-07 05:46

    .image {
      text-align: center;
      background: #c0c0c0;
      border: #a0a0a0 solid 1px;
    
    }
    
    .image:before {
      content: '\200B';
    /*   content: '';
      margin-left: -0.25em; */
      display: inline-block;
      height: 100%; 
      vertical-align: middle;
     }
    
    .centered {
      display: inline-block;
      vertical-align: middle;
      width: 300px;
      padding: 10px 15px;
       }
    

    <div class="image" style="height: 600px;">
        <div class="centered">
            <img src="download.jpg"  alt="" />
        </div>
    </div>
    
    0 讨论(0)
  • 2020-12-07 05:49

    you can simply add this css to your image

    img{
       margin-left: auto;
       margin-right: auto;
    }
    
    0 讨论(0)
提交回复
热议问题