篇文章主要给大家介绍一下如何使用html+css实现元素的水平与垂直居中效果,这也是我们网页在编码制作中会经常用到的问题。
主要实现css代码:
水平居中:text-align:center;
垂直居中:line-height:XXpx; /*line-height与元素的height的值一致*/
我们先来看这样一个例子,加入我们这里有一个div,宽度和高度为300px,背景颜色为黑色,然后在div中有一行简短文字,我们只需要使用line-height:200px;就可以实现文字的居中效果,具体的代码如下所示:
由上图可以看出我们实现了单行文字的垂直居中效果,但是很多时候我们的文字并不知道具体有多少,可能有一行,也可能有很多行,那么遇到多行文字的这种问题我们要如何处理呢。
对于多行文本的垂直居中我们有很多种实现方式,我们这里逐个的来看一下;
1、使用display:table来实现
主要实现代码:
display: table使块状元素成为一个块级表格;
display: table-cell;子元素设置成表格单元格;
vertical-align: middle;使表格内容居中显示,即可实现垂直居中的效果;
具体的html与css的代码就如下所示:
2、使用absolute与transform配合实现
主要实现代码:
position:absolute; 首先给文本绝对定位;
left:50%;top:50%;transform:translate(-50%,-50%); 让文本距离盒子左边和上边分别为50%,再用transform向左(上)平移它自己宽度(高度)的50%,也就达到居中效果了。
具体的html与css的代码就如下所示:
3、使用flex实现
主要实现代码:
display: flex;设置 display 属性的值为 flex 将其定义为弹性容器
align-items: center;定义项目在交叉轴(纵轴)上如何对齐,垂直对齐居中
justify-content: center; 定义了项目在主轴上的对齐方式,水平对齐居中
具体的html与css的代码就如下所示:
好了,本篇文章就给大家说到这里,大家自己动手写一下看能不能写出一样的页面效果出来,也可以找一些类似的页面自己练习一下,有需要源码的可以直接私信【网站源码】即可。
每日金句:了解别人心里想什么,你才能得到自己想要的。喜欢我的文章的小伙伴记得关注一下哦,每天将为你更新最新知识。
当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。所以需要对父div的 line-height 进行调整。利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置 margin:auto 会使它居中显示。
转载自喜欢JS的无名小站
例如 一个父div(w:100%;h:400px)中有一个子div(w:100px;100px;)。让其上下左右居中。
利用表格单元格的居中属性。
父div外层配置一个div,同时设置为表格元素 (display: table),宽度为100%
父div配置为表格单元格元素 (display: table-cell)
父div配置居中属性(vertical-align: middle),使子div上下居中
子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%;
当父div的行高等于自身高度时,内部的行内元素会上下居中显示。行内块没有固定高度时也会上下居中显示。通过文本居中属性text-align:center
,可以使内部行内元素或行内块元素左右居中显示。
子div设定为行内块元素(display:inline-block);
父div设置行高(line-height)使子div上下居中
父div设置文本居中(text-align:center)使子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>
注: 行高如果设置为当前父div的高度(400px)的话,有固定高度的子div并不会居中显示的,问题出在浏览器默认将其当做文本居中的,即把它当做了一段文本(chrome默认font-size:16px;hight:21px)进行居中,没把它当做高度100px进行居中。所以需要对父div的line-height
进行调整。以font-size:0
(对应的字体高度为0)为例子,则需要line-height增加一个子div的高度(400px + 100px;)。
利用定位属性(top、left、right、bottom)百分比的模式。若为100%,则代表偏移的长度为父div的高度(宽度)的100%。
父div标记下定位(position:relative|absolute|fixed);子div绝对定位(position:absolute)
子div上下居中:top:50%;margin-top:-h/2;
或是 bottom:50%;margin-bottom:-h/2;
;
子div左右居中: left:50%;margin-left:-w/2
或是 right:50%;margin-right:-w/2
;
<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>
定位属性top和bottom(或是left和right)值分别设置为0,但子div有固定高度(宽度),并不能达到上下(左右)间距为0,此时给子div设置margin:auto会使它居中显示。
父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>
弹性盒子,自带的一个居中功能
<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>
flex兼容性,以及存在的已知问题
方法二和方法三兼容性要比其它好些
Can I use
css-vertical-center-solution
CSS实现垂直居中的5种方法--前端观察
<div id="parent">
<!-- 定义子级元素 -->
<div id="child">居中布局</div>
</div>
过以下CSS样式代码实现水平方向居中布局效果
.parent{position:relative;}
.child{position:absolute;left:50%;transform: translateX(-50%)}
优点:
父级元素是否脱离文档流, 不影响子集元素水平居中效果
缺点:transform属性是CSS3中新增属性, 浏览器支持情况不好
*请认真填写需求信息,我们会在24小时内与您取得联系。