小div在大div中垂直居中

作者:admin来源:编辑 日期:2021/3/31 7:58:00浏览次数:

这里讲述小div在大div中垂直居中的问题。

方法一:

1、代码:

<div style="width:200px;height:200px;border:2px solid blue;position:relative;">

 <div style="width:100px;height:100px;margin: auto;  position: absolute;  top: 0; left: 0; bottom: 0; right: 0; background: red;"></div>

 </div>

效果图:

方法二:

2、代码:

 <div style="width:200px;height:200px;border:2px solid #00f;display:table-cell;vertical-align:middle;text-align: center;">

 <div style="width:100px;height:100px;display:inline-block;"></div>

 </div>

方法三:

3、代码:

 <div style="width:200px;height:200px; border:2px solid #00f;display:flex;justify-content:center;align-items:center;">

  <div style="width:100px;height:100px;"></div>

 </div>

方法四:

4、代码:

 <div style="width:200px;height:200px; border:2px solid #00f;position:relative;">

 <div style="width:100px;height:100px;margin:auto;position:absolute;left:50%;top:50%;margin-left: -50px;margin-top:-50px;"></div>

 </div>

 

欢迎阅读、分享,转载和使用请注明出处,文章来自三里河之光-风、物、情

 

中华人民共和国