div宽高不确定,在父元素div中居中。

Posted daidechong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了div宽高不确定,在父元素div中居中。相关的知识,希望对你有一定的参考价值。

第一种方法

 1 <div class="parent">
 2     <div class="child"></div>
 3 </div>
 4 
 5 <style>
 6     .parent 
 7         display: flex;
 8         width: 100%;
 9         height: 300px;
10         background: #cecece;
11         align-items: center; /* 垂直居中 */
12         justify-content: center; /* 水平居中 */
13         
14     .child 
15         background: #000;
16         width: 200px;
17         height: 100px;
18     
19 </style>

第二种方法

 1 <div class="x1">
 2     <div class="x2">
 3     </div>
 4 </div>
 5 
 6 <style>
 7     .x1 
 8         width: 1000px;
 9         height: 1000px;
10         position: relative;
11         background: #000;
12     
13     .x2 
14         width: 20px;
15         height: 20px;
16         margin: auto;
17         top: 0;
18         left: 0;
19         right: 0;
20         bottom: 0;
21         position: absolute;
22         background: #fff;
23     
24 </style>

 

以上是关于div宽高不确定,在父元素div中居中。的主要内容,如果未能解决你的问题,请参考以下文章

div盒子水平垂直居中的方法推荐

未知宽高元素如何在一个div中上下左右居中对齐

div在父元素中的居中问题

div css中 div里面的元素总是不能包含里面的内容,请问如何设置样式才能完全包含div元素里面的内容呢?

CSS:将嵌套元素定位在父元素边界之外

子div在父div下层,父div图片覆盖子div图片