目录
行盒水平居中
/* 清除默认样式 */
body {
margin: 0;
padding: 0;
}
h1 {
width: 100vw;
height: 300px;
background-color: lightseagreen;
/* 水平居中 */
text-align: center;
}
<body>
<h1>标题</h1>
</body>
行盒垂直居中
/* 清除默认样式 */
body {
margin: 0;
padding: 0;
}
h1 {
width: 100vw;
height: 300px;
background-color: lightseagreen;
/* 垂直居中(这里的行高与高度的px一致) */
line-height: 300px;
}
<body>
<h1>标题</h1>
</body>
行盒水平垂直居中
/* 清除默认样式 */
body {
margin: 0;
padding: 0;
}
h1 {
width: 100vw;
height: 300px;
background-color: lightseagreen;
/* 水平居中 */
text-align: center;
/* 垂直居中(这里的行高与高度的px一致) */
line-height: 300px;
}
<body>
<h1>标题</h1>
</body>
块盒水平居中
方法一:
.outer {
height: 200px;
background-color: lightsalmon;
position: relative;
}
.inner {
width: 500px;
height: 100px;
background-color: lightgreen;
position: absolute;
left: 0;
right: 0;
margin: 0 auto;
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
方法二:
.outer {
height: 200px;
background-color: lightsalmon;
}
.inner {
width: 500px;
height: 100px;
background-color: lightgreen;
margin: 0 auto;
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
方法三:
.outer {
height: 500px;
background-color: lightgreen;
position: relative;
}
.inner {
width: 500px;
height: 250px;
background-color: lightcoral;
position: absolute;
left: 50%;
transform: translateX(-50%);
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
块盒垂直居中
方法一:
.outer {
height: 500px;
background-color: lightgreen;
position: relative;
}
.inner {
width: 500px;
height: 250px;
background-color: lightcoral;
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
方法二:
.outer {
height: 500px;
background-color: lightgreen;
position: relative;
}
.inner {
width: 500px;
height: 250px;
background-color: lightcoral;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
块盒水平垂直居中(重点)
方法一: flex布局1
.outer {
width: 500px;
height: 500px;
background-color: lightseagreen;
display: flex;
justify-content: center;
align-items: center;
}
.inner {
width: 200px;
height: 200px;
background-color: lightcoral;
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
方法二: flex布局2
.outer {
width: 500px;
height: 500px;
background-color: lightseagreen;
display: flex;
}
.inner {
width: 200px;
height: 200px;
background-color: lightgreen;
margin: auto;
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
方法三: 定位
.outer {
width: 500px;
height: 500px;
background-color: lightskyblue;
position: relative;
}
.inner {
width: 200px;
height: 200px;
background-color: lightpink;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
方法四: 定位+位移(transform)
.outer {
width: 500px;
height: 500px;
background-color: lightcoral;
position: relative;
}
.inner {
width: 200px;
height: 200px;
background-color: lightgoldenrodyellow;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<body>
<div class="outer">
<div class="inner"></div>
</div>
</body>
方法五:有头部或者底部的水平垂直居中
body {
margin: 0;
padding: 0;
}
header {
width: 100vw;
height: 70px;
background-color: lightskyblue;
}
.main {
display: flex;
/* 70px是基于 header 的高度 */
height: calc(100vh - 70px);
}
.content {
width: 1000px;
height: 500px;
background-color: lightseagreen;
display: flex;
align-items: center;
/* 基于内容 */
justify-content: space-evenly;
/* 基于content */
margin: auto;
}
<body>
<header>我是头部</header>
<div class="main">
<div class="content">我是内容</div>
</div>
</body>
方法六:给视口水平垂直居中
body {
margin: 0;
padding: 0;
width: 100vw;
/* 注意加 */
height: 100vh;
background-color: lightcyan;
position: relative;
}
.main {
width: 400px;
height: 400px;
background-color: lightseagreen;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
.item {
width: 200px;
height: 200px;
background-color: lightskyblue;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
<body>
<div class="main">
<div class="item"></div>
</div>
</body>