Solution 1:

给absolute元素的left设为50%, margin-left设为absolute元素宽度一半的负数

.con{
  width:200px;
  height:200px;
  background:#ccc;
  position:relative;
}
.abs{
  width:40px;
  height:20px;
  background:steelblue;
  position:absolute;
  bottom:0;

  left:50%;
  margin-left:-20px;
}

Solution 2:

原理和1相似,设left:50%,但使用css3的transform:translate(x,y);

.con{
  width:200px;
  height:200px;
  background:#ccc;
  position:relative;
}
.abs{
  width:40px;
  height:20px;
  background:steelblue;
  position:absolute;
  bottom:0;

  left:50%;
  transform:translate(-50%);
}


Solution 3:

margin:auto;实现居中,但是absolute元素一定要有宽度,并且如果宽度不合适(常见于ul li)也是不会居中的

.con{
  width:200px;
  height:200px;
  background:#ccc;
  position:relative;
}
.abs{
  width:40px;
  height:20px;
  background:steelblue;
  position:absolute;
  bottom:0;
  left:0;
  right:0;
  margin:0 auto;
}

 

相关文章:

  • 2022-12-23
  • 2021-08-04
  • 2022-02-13
  • 2022-12-23
猜你喜欢
  • 2021-11-21
  • 2021-11-22
  • 2022-12-23
  • 2021-12-11
  • 2021-11-27
  • 2021-11-01
  • 2021-10-19
相关资源
相似解决方案