table of Contents
Elements in the line:
(1) Set text-align:center in the parent element
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
height: 100px;
background-color: deepskyblue;
text-align: center;
}
</style>
</head>
<body>
<div class="box1">行内元素水平居中</div>
</body>
</html>
Block-level elements:
(1) Set margin: 0 auto; (need to have a fixed width)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: deepskyblue;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box1">设置 margin:0 auto;(需有固定宽度)</div>
</body>
</html>
(2) Use display:inline-block to convert to inline elements, and set text-align:center on the parent element.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
height: 300px;
background-color: deepskyblue;
text-align: center;
}
.box2 {
width: 300px;
height: 200px;
background-color: deeppink;
display: inline-block;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">
用 display:inline-block 转成行内元素,在父元素设置 text-align:center
</div>
</div>
</body>
</html>
(3) Absolute positioning
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 200px;
height: 200px;
background-color: deepskyblue;
position: absolute;
left: 50%;
transform: translateX(-50%); /* 移动元素本身50% */
}
</style>
</head>
<body>
<div class="box1">绝对定位+translateX</div>
</body>
</html>
(3) Realize with flex layout
Method 1: display: flex; justify-content: center
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 500px;
height: 200px;
background-color: deepskyblue;
display: flex;
justify-content: center
}
</style>
</head>
<body>
<div class="box1">内容水平居中</div>
</body>
</html>
Method 2: Parent element display: flex; flex-direction: column; Child element: align-self: center;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 800px;
height: 300px;
background-color: deepskyblue;
display: flex;
flex-direction: column;
}
.box2 {
width: 300px;
height: 200px;
background-color: deeppink;
align-self: center;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">块元素水平居中</div>
</div>
</body>
</html>
Method 3: Parent element display: flex; Child element: margin: 0 auto;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.box1 {
width: 800px;
height: 300px;
background-color: deepskyblue;
display: flex;
}
.box2 {
width: 300px;
height: 200px;
background-color: deeppink;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box1">
<div class="box2">块元素水平居中</div>
</div>
</body>
</html>