CSS center any image in div

后端 未结 2 741
被撕碎了的回忆
被撕碎了的回忆 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: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;
    }

提交回复
热议问题