前端开发中经常会遇到各种各样元素居中的设计,今天特意搜集归纳了一下各种情况下元素居中显示的解决方案。
行内元素水平居中:text-align
我最熟悉的一个属性,只能实现行内元素(display为inline或inline-block等)的水平居中。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<p>我要居中显示!</p>
</div>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
background-color: aquamarine;
/* 只能确保子元素中的行内元素居中 */
text-align: center;
}
p {
background-color: yellowgreen;
}
</style>
</body>
</html>

块级元素水平居中:margin: 0 auto
块级元素的水平居中使用text-align一般无法实现,这时候可以使用margin:0 auto;来实现。
确定块级元素自身的width属性后,设置margin-left和margin-right的值auto。
这时候不可以设置元素浮动,设置浮动后元素失去居中效果
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<p>我要居中显示!</p>
</div>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
background-color: aquamarine;
text-align: center;
}
p {
background-color: yellowgreen;
/* 确定元素自身宽度后,使用margin: 0 auto 确保元素自身水平居中 */
width: 400px;
margin: 0 auto;
}
</style>
</body>
</html>

单行文本垂直居中显示:line-height
只有一行文字的情况下,设置文字的line-height高度等于父级容器的高度
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<p>我要居中显示!</p>
</div>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
background-color: aquamarine;
text-align: center;
}
p {
background-color: yellowgreen;
width: 400px;
margin: 0 auto;
/* 给文字的父级容器设置高度为64px */
height: 64px;
/* 设置文字行高等于父级容器高度,实现单行文字垂直居中 */
line-height: 64px;
}
</style>
</body>
</html>

使用绝对定位实现居中显示(知道元素自身的宽度和高度)
绝对定位实现元素居中显示有一个前提条件,需要知道元素自身的宽度和高度。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<p>我要居中显示!</p>
</div>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
background-color: aquamarine;
text-align: center;
}
p {
background-color: yellowgreen;
/* 确定元素自身的width 和 height */
width: 400px;
height: 64px;
line-height: 64px;
/* 设置元素绝对定位,top、left的值设为50% */
position: absolute;
top: 50%;
left: 50%;
/* 再通过设置top、和left方向的外边距为一半宽度、一半高度的的负值 */
margin-top: -32px;
margin-left: -200px;
}
</style>
</body>
</html>
除了上面一种方法外,使用绝对定位实现元素居中显示还有一种方法,这种方法同样需要知道元素自身的宽度和高度。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<p>我要居中显示!</p>
</div>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
background-color: aquamarine;
text-align: center;
}
p {
background-color: yellowgreen;
/* 确定元素自身的width 和 height */
width: 400px;
height: 64px;
line-height: 64px;
/* 设置元素绝对定位,top、left、right、bottom的值设为0 */
position: absolute;
top:0;
left: 0;
right: 0;
bottom: 0;
/* margin: auto 这个属性必须有 */
margin: auto;
}
</style>
</body>
</html>

浮动元素居中显示(浮动+相对定位)
优点:不需要知道居中元素的宽度和高度
缺点:通过两次相对定位实现
原理:把浮动元素相对定位到父元素宽度50%的地方,但这个时候元素还不是居中的,而是比居中的那个位置多出了自身一半的宽度,这时就需要他里面的子元素再用一个相对定位,把那多出的自身一半的宽度拉回来,而因为相对定位是相对于自身来定位的,所以自身一半的宽度只要把left 或 right 设为50%就可以得到。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<div class="wraper">
<p>我要居中显示!</p>
</div>
</div>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
background-color: aquamarine;
text-align: center;
}
.wraper {
/* 设置元素浮动,让元素自适应内容宽度 */
float: left;
position: relative;
top: 50%;
left: 50%;
}
p {
background-color: yellowgreen;
position: relative;
top: -50%;
left: -50%;
}
</style>
</body>
</html>

使用flex布局实现水平垂直居中
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div class="box">
<p>我要居中显示!</p>
</div>
<style>
* {
margin: 0;
padding: 0;
}
.box {
width: 100%;
height: 100vh;
background-color: aquamarine;
text-align: center;
display: flex;
/* 垂直居中弹性盒子的各项div元素 */
align-items: center;
/* 水平居中 */
justify-content: center;
}
p {
background-color: yellowgreen;
}
</style>
</body>
</html>


本文详细介绍了前端开发中元素居中显示的各种解决方案,包括行内元素的text-align属性,块级元素的margin:0 auto,单行文本的line-height,绝对定位以及使用flex布局实现的水平垂直居中。每个方法都有相应的代码示例,适用于不同的场景需求。
3019

被折叠的 条评论
为什么被折叠?



