父元素:box1,子元素:box2;

持续补充中。。。。。

一、子div定宽高

1. flex布局

.box1{
  width: 300px;
  height: 300px;
  border: 1px solid red;
  display: flex;
  align-items: center;
}
.box2{
  width: 100px;
  height: 100px;
  border: 1px solid blue;
}

2.  定位+上下左右为0

.box1{
  width: 300px;
  height: 300px;
  border: 1px solid red;
  position: relative;
}
.box2{
  width: 100px;
  height: 100px;
  border: 1px solid blue;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  margin: auto;
}

3. 定位+平移translate

.box1{
  width: 300px;
  height: 300px;
  border: 1px solid red;
  position: relative;
}
.box2{
  width: 100px;
  height: 100px;
  border: 1px solid blue;
  position: absolute;
  top: 50%;
  transform: translate(0,-50%);
}

 

  

  

 

相关文章:

  • 2021-11-13
  • 2021-12-13
  • 2022-01-09
  • 2021-05-17
猜你喜欢
  • 2021-06-24
  • 2022-12-23
  • 2021-12-29
  • 2021-12-20
  • 2021-11-21
相关资源
相似解决方案