CSS一个盒子在另一个盒子水平垂直居中

html-css08

CSS一个盒子在另一个盒子水平垂直居中,第1张

链接:https://zhuanlan.zhihu.com/p/39437057

第一种:利用负的margin来进行居中,需要知道固定宽高,限制比较大。

body>div:nth-of-type(1){ width:400pxheight:400pxbackground:#ff0position:relativemargin-bottom:10px}

body>div:nth-of-type(1)div{ width:100pxheight:100pxbackground:#0f0position:absolutetop:50%left:50%margin-left:-50pxmargin-top:-50px}

第二种:利用绝对定位居中,非常常用的一种方法。body>div:nth-of-type(2){ width:400pxheight:400pxbackground:#ff0position:relativemargin-bottom:10px}

body>div:nth-of-type(2) div{ width:100pxheight:100pxbackground:#0f0position:absolutetop:0left:0right:0bottom:0margin:auto}

第三种:使用flex布局(.min宽高可不固定)

body>div:nth-of-type(3){ width:400pxheight:400pxbackground:#ff0margin-bottom:10pxdisplay:flex}

body>div:nth-of-type(3) div{ width:100pxheight:100pxbackground:#0f0margin:auto }

第四种:flex居中(演示)。CSS3中引入的新布局方式,比较好用。缺点:IE9以及IE9一下不兼容。

body>div:nth-of-type(4){ width:400pxheight:400pxbackground:#ff0margin-bottom:10pxdisplay:flexjustify-content:centeralign-items:center}

body>div:nth-of-type(4) div{ width:100pxheight:100pxbackground:#0f0}

第五种:利用table-cell来控制垂直居中。

body>div:nth-of-type(5){ width:400pxheight:400pxbackground:#ff0margin-bottom:10pxvertical-align:middledisplay:table-celltext-align:center}

body>div:nth-of-type(5) div{ width:100pxheight:100pxbackground:#0f0display:inline-block }

第六种:利用空盒子做外容器,里边的块元素会自动垂直居中,只需要控制一下水平居中就可以达到效果。

body>div:nth-of-type(6){ width:400pxheight:400pxbackground:#ff0margin-bottom:10pxtext-align:center vertical-align:middle}

body>div:nth-of-type(6) div{ width:100pxheight:100pxbackground:#0f0display:inline-blockvertical-align:middle}

body>div:nth-of-type(6) span{ width:0height:100%display:inline-blockvertical-align:middle}

第七种:这种方法灵活运用CSS中transform属性,较为新奇。缺点是IE9下不兼容。

body>div:nth-of-type(7){ width:400pxheight:400pxbackground:#ff0position:relativemargin-bottom:10px}

body>div:nth-of-type(7) div{ width:100pxheight:100pxbackground:#0f0position:absolutetop:50%left:50%transform:translate(-50%,-50%)}

css垂直居中,如果兼容各个浏览器的话,我还没有见过。

在百度里面搜了很多,代码都不健壮,最终都是用js来实现的。

首先,我们来了解水平居中,它有很多种方法,我们暂时先来了解其中的几种:

1.在实现方案中,我们最熟悉的莫过于给元素定义一个宽度,然后使用margin:

body{

width:960px

margin:0 auto

}

这个是当我们的定义元素的宽度时显现的,如果我们不能定义宽度时,该怎么办呢?

2.我们对于定位也是常用的,在这里当然也可以采用定位的方法来实现:

body{

position:absolute

left:50%

}

3. 既然定位可以,那浮动也是可以的:

body{

float:left

right:50%

}

4.对于几个元素同时居中在一条线上

body{

vertical-align:middle

}

5.利用table:

ul{

display:table

}

ul li{

display:table-cell

}

6.还可以使用inline-block来实现,但要使用这个就得在其父元素上设置text-align.如下:

body{

text-align:center

}

.content{

display:inline-block

}

实现垂直居中的四种方法:

1.只能是单行文本居中(可适用于所有浏览器):

.content{

height:100px

line-height:100px

}

2.跟水平居中一样,垂直也可以用定位的方法:

.content{

position:absolute

top:0

bottom:0

left:0

right:0

margin:auto

}

或者

.content{

position:absolute

top:50%

}

定位的方法,它的缺点是当没有足够的空间时,元素会消失。

3.对此,浮动也是可以的:

.content{

float:left

height:50%

margin-bottom:-120px

}

.footer{

clear:both

height:240px

position:relative

}

对于浮动,我们需要在之后清除,并显示在中间。

4.也可以使用vertical-align属性:

.content{

display:table-cell

vertical-align:middle

}

这种方法可以随意改变元素高度,但在IE8中无效。

现在来看个div模块在屏幕中居中的例子:

position: absolute top: 50% left: 50% //上下移动屏幕的50%

margin: auto

-webkit-transform: translate(-50%,-50%)//减去自身的50%(多移动的)

-ms-transform: translate(-50%,-50%)

transform: translate(-50%,-50%)

这个基本可以不确定宽高的模块居中,但是在低版本的浏览器中可能出现问题,现在还没测试过,但是主流的都是可以的!