Cssh5面试

绝对定位元素的三种居中方式

2020-12-02  本文已影响0人  小小鱼yyds

绝对定位元素的三种居中方式:(横向居中)

<div class="father">
    <img class="pro-img" src="../../static/img/xxx@2x.png" alt="">
</div>
<style scoped lang="scss">
  .father{
     width: 100%;
     position: relative;  /*注意:父级的position要设置为relative*/
     display: flex;
     justify-content: center;
  }
  .pro-img{
      width: 257px;
      height: 214px;
      position: absolute;
      bottom: 64px;
   }
         
</style>
<style scoped lang="scss">
  .father{
     width: 100%;
     position: relative;  /*注意:父级的position要设置为relative*/
  }
  .pro-img{
      width: 257px;
      height: 214px;
      position: absolute;
      bottom: 64px;
      left: 0;
      right:0;
      margin-left: auto;
      margin-right: auto;
   }
         
</style>
<style scoped lang="scss">
  .father{
     width: 100%;
     position: relative;  /*注意:父级的position要设置为relative*/
  }
  .pro-img{
      width: 257px;
      height: 214px;
      position: absolute;
      bottom: 64px;
      left: 50%;
      transform: translateX(-50%);
   }
         
</style>
上一篇下一篇

猜你喜欢

热点阅读