元素居中方法

2022/8/29 6:53:04

本文主要是介绍元素居中方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1、定位+外边距

.father{

  position:relative;

  top:50%;

  left:50%;
  width:40px;

  height:40px;

  .son{

  posithon:absolute;

  margin:-10px 0 0 -10px;

  width:20px;

  height:20px;

}

}

2、定位+位移(transform:translate(-50%,-50%))

.father{

  position:relative;

  top:50%;

  left:50%;
  width:40px;

  height:40px;

  .son{

  posithon:absolute;

  transform:translate(-50%,-50%);

  width:20px;

  height:20px;

}

}

3、弹性布局flex

.son{

  display:flex;

  justify-content:center;

  align-item:center;

}



这篇关于元素居中方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程