HTML/CSS盒子居中详解
程序员文章站
2022-06-26 16:22:34
我们在做web练习或项目的时候,经常会碰到盒子居中问题,今天给大家总结了几种方法,希望能有所帮助!居中目录1. 内容水平居中2. 一行文字垂直居中3. 盒子水平居中4. 子元素在父元素中居中4.1 子元素在父元素中居中-弹性盒子4.2 子元素在父元素中居中-table-cell4.3 子元素在父元素中居中-绝对定位1. 内容水平居中text-align: center代码: ....
我们在做web练习或项目的时候,经常会碰到盒子居中问题,今天给大家总结了几种方法,希望能有所帮助!
居中目录
1. 内容水平居中
- text-align: center
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>内容水平居中</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
text-align: center;
}
</style>
</head>
<body>
<div class="box">
好好学习,天天向上!
</div>
</body>
</html>
显示:
2. 一行文字垂直居中
- line-height=height
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>一行文字垂直居中</title>
<style>
.box{
width: 300px;
height: 300px;
background-color: red;
/* text-align: center; */
line-height: 300px;
}
</style>
</head>
<body>
<div class="box">
好好学习,天天向上!
</div>
</body>
</html>
显示:
3. 盒子水平居中
- margin: 0 auto;
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>盒子水平居中</title>
<style>
div {
width: 200px;
height: 200px;
background: red;
margin: 0 auto;
}
</style>
</head>
<body>
<div></div>
</body>
</html>
显示:
4. 子元素在父元素中居中
4.1 子元素在父元素中居中-弹性盒子
-
方法一:弹性盒子
display: flex; /* 弹性盒子 */
align-items: center; /水平居中/
justify-content: center; /垂直居中/
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>子元素在父元素中居中-弹性盒子</title>
<style>
.parent {
width: 500px;
height: 500px;
background-color: red;
display: flex; /* 弹性盒子 */
align-items: center; /*水平居中*/
justify-content: center; /*垂直居中*/
}
.child {
width: 200px;
height: 200px;
background-color: blue;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>
显示:
4.2 子元素在父元素中居中-table-cell
方法二:table-cell
父元素:display: table-cell; /以列来排布/
子元素:margin: 0 auto
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>子元素在父元素中居中-table-cell</title>
<style>
.parent {
width: 500px;
height: 500px;
background-color: red;
display: table-cell; /*以列来排布*/
vertical-align: middle;
}
.child {
width: 200px;
height: 200px;
background-color: blue;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>
显示:
4.3 子元素在父元素中居中-绝对定位
-
方法三:绝对定位
父元素:position: relative;
子元素:position: absolute;
left: 50%;
top: 50%;
margin-left: 减去自身的的一半; /如200px, 就减去100px/
margin-top: 减去自身的一半;/如200px, 就减去100px/
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>子元素在父元素中居中-绝对定位</title>
<style>
.parent {
width: 500px;
height: 500px;
background-color: red;
position: relative;
}
.child {
width: 200px;
height: 200px;
background-color: blue;
position: absolute;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -100px;
}
</style>
</head>
<body>
<div class="parent">
<div class="child"></div>
</div>
</body>
</html>
显示:
—Ending—
本文地址:https://blog.csdn.net/weixin_47021982/article/details/110244922
上一篇: 记录最近一场面试(Srping、SQL)