CSS center any image in div

后端 未结 2 740
被撕碎了的回忆
被撕碎了的回忆 2021-01-20 19:16

Lets say I have div with width: 300px and height: 200px
I want any image inside this div to be centered both horizont

相关标签:
2条回答
  • 2021-01-20 19:42

    Try this

    div{
      display: flex;
      align-items: center;
      justify-content: center
    }
    
    0 讨论(0)
  • 2021-01-20 19:48

    Add position: absolute to img and position: relative to div

    .el {
      position: relative;
      width: 100px;
      height: 100px;
      border: 1px solid black;
      margin: 50px 150px;
    }
    img {
      opacity: 0.4;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      position: absolute;
    }
    <div class="el">
      <img src="http://placehold.it/350x150">
      <img src="http://placehold.it/50x50/000000/ffffff">
    </div>

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