内容导读
当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。所以需要对父div的 line-height 进行调整。利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置 margin:auto 会使它居中显示。
例如 一个父div(w:100%;h:400px)中有一个子div(w:100px;100px;)。让其上下左右居中。
方法一(varticle-align)
理念
利用表格单元格的居中属性。
步骤
父div外层配置一个div,同时设置为表格元素 (display: table),宽度为100%
父div配置为表格单元格元素 (display: table-cell)
父div配置居中属性(vertical-align: middle),使子div上下居中
1、可以用text-align属性设置。首先打开hbuilder编辑器,新建一个html文件,先切换到边改边看模式:2、写一个div出来,同时在内部加上一些文字,使用css增加属性,使得div的区域看得清楚:3、接下来就使用text-align的center。
子div通过margin配置左右居中(margin-left:auto; margin-right:auto)
例子
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .table {display: table; width: 100%;} .father {display: table-cell; vertical-align: middle;} .son {margin: auto;}</style><body> <div class="table" > <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div> </div></body>
注:
表格单元格比较特殊,如果只有一个单元格时,它的宽度默认会占父级(table|tr)宽度的100%;
table默认宽度不会撑开,需要手动配置width:100%;
方法二(line-height)
理念
当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。通过文本居中属性text-align:center
步骤
子div设定为行内块元素(display:inline-block);
父div设置行高(line-height)使子div上下居中
父div设置文本居中(text-align:center)使子div左右居中。
如何让div在整个页面中居中,<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {line-height: 500px; text-align: center; font-size: 0;} .son { display: inline-block; /* display: inline-flex; display: inline-grid; display: inline-table; */ }</style><body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div></body>
font-size:0
1、新建一个html文件,命名为test.html,用于讲解CSS怎样让一个div居中。2、在test.html文件中,使用div标签创建一个模块,用于测试居中效果。3、在test.html文件中,设置div的class属性为test,下面将通过该class属性设置其c。
方法三(定位)
理念
步骤
子div上下居中:top:50%;margin-top:-h/2;
bottom:50%;margin-bottom:-h/2;
随便写上文字 先打开visual studio软件,然后在div中随意写上文字,如下图所示:水平居中代码 然后在style中写上水平居中代码body{text-align: center;},如下图所示:div内容居中对齐的代码,text-align:center;如下。
子div左右居中: left:50%;margin-left:-w/2
right:50%;margin-right:-w/2
例子
第一种方式,知道自己高度,知道容器高度,用margin-top,margin-left设置居中。第二种方式,知道自己高度,知道容器高度,设置成position:relative;用margin:0 auto;实现左右居中,用margin-top实现上下居中。第三种方式;position。
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute;bottom:50%;margin-bottom: -50px;left: 50%;margin-left: -50px; }</style><body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div></body>
方法四(定位)
div居中可以用外边距margin属性来实现。 1、新建html文档,在body标签中添加div标签,标签的id为“header”,这时因为div标签中没有内容且没有设置样式,所以网页中显示空白: 2、这时为div标签设置原题中的css样式,此时虽然。
理念:
步骤:
父div标记下定位(position:relative|absolute|fixed|sticky);子div绝对定位(position:absolute)
子div上下居中:top:0;bottom:0;margin-top:auto;margin-bottom:auto
子div左右居中:left:0;right:0;margin-left:auto;margin-right:auto
例子
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {position: relative;} .son {position: absolute; top: 0; bottom:0; left: 0; right: 0; margin: auto}</style><body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div></body>
方法五(flex)
理念
弹性盒子,自带的一个居中功能
例子
<style> * {margin: 0; padding: 0; box-sizing: border-box;} .father {display: flex; align-items: center} .son {margin: auto}</style><body> <div class="father" style="width: 100%; height: 400px; border: 1px solid rebeccapurple;"> <div class="son" style="width: 100px; height: 100px;background: palegreen;"></div> </div></body>
结尾
方法二和方法三兼容性要比其它好些