DIV水平垂直居中父级DIV方法

 时间:2026-02-13 11:20:43

1、效果如下:中间的div会水平垂直居中绿色的div盒子

DIV水平垂直居中父级DIV方法

2、html代码如下:

<div class="box"><div class="child"></div></div>

3、css代码如下:

.box{    position: relative;    width: 600px;    height: 600px;    background-color: green;    margin: 20px auto;}.box .child{    position: absolute;    left: 0;    right: 0;    bottom: 0;    top: 0;    margin: auto;    width: 300px;    height: 300px;    background-color: pink;}

DIV水平垂直居中父级DIV方法

4、父DIV必须加上相对定位position:relative;

需要居中的DIV使用绝对定位position:absolute;left:0;right:0;top:0;bottom:0;margin:auto;

当然还有其他方法可以实现,这只是其中的一种。

  • 怎样寻找网页代码的相关素材
  • 如何使用表单发红包?
  • prezi如何唤出CSS编辑器
  • 如何在静态网页中制作文字大小排版
  • 网页怎样设置单排多列
  • 热门搜索
    湖南岳阳旅游 福建泰宁旅游攻略 北京冬季旅游攻略 奉化旅游景点大全 温州海外旅游公司 多伦多旅游 大亚湾旅游景点大全 塞舌尔旅游报价 番禺旅游景点大全 高密旅游