SS 是前端里面的基础之一,也是非常重要的一部分,它往往决定了你所做出来的网页页面是否美观。在设计网页页面的过程中,总会有将元素或者文字进行水平垂直居中的要求。下面w3cschool编程狮就为大家介绍 CSS 中几种常用到的水平垂直居中的方法。
当元素有给定的高度以及宽度的时候,使用 margin: auto; 元素仅会水平居中,并不会进行垂直居中。此时就需要设置元素的 position 为 absolute,父级元素的 position 为 relative,同时元素的上下左右都需要设置为 0。
HTML 代码
<div class="box">
<div class="center1"></div>
</div>
CSS 代码
.box{
width: 200px;
height: 200px;
background-color: #eee;
position: relative;
margin-top: 20px;
}
.center1{
width: 50px;
height: 50px;
background-color: #00ACED;
margin: auto;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
效果展示:
当已经知道了要进行水平垂直居中的元素的宽高时,就可以通过设置 position: absolute 来实现。但是,使用的同时还需要结合其他属性才完整实现。因为,单是设置 absolute,上左距离均为一半,就会出现下面这种情况。很显然可以看到,元素并不是完全居中,仅只有左上角的位置在中心点
概念图:
因此想要实现元素完全水平垂直居中,在设置了 absolute 定位后,可以设置 margin 值为负,或者使用 calc 来计算,上左距离在 50% 的基础上还要减去元素本身一半的宽高。
margin 值为负或者 calc 计算均是在已知元素宽高的情况下,假设不知道元素的宽高,那么怎么实现水平垂直居中呢?这里就可以使用 transform 属性,通过坐标位移来实现居中。
CSS 代码
/* 结合 margin */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
margin-left: -25px;
margin-top: -25px;
}
/* 结合 calc 计算*/
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: calc(50% - 25px)
top: calc(50% - 25px);
}
/* 结合 transform */
.center2{
width: 50px;
height: 50px;
background-color: #7FFFD4;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
效果展示
03
PART
可以通过弹性布局来设置水平垂直居中,这里需要设置父级元素 display:flex; 还需要设置两个属性,水平布局 justify-content 以及垂直布局 align-items。
HTML代码
<div class="box2">
<div class="center4"></div>
</div>
CSS代码:
.box2{
background-color: #eee;
width: 200px;
height: 200px;
position: relative;
margin-top: 20px ;
display: flex;
justify-content: center;
align-items: center;
}
.center4{
width: 50px;
height: 50px;
background-color: #B39873;
}
效果展示:
前面介绍的是元素如何实现水平垂直居中,下面介绍的是如何将文字进行水平垂直居中。这第一个方法也是最经常用的,使用文本水平对齐 text-align 和行高 line-height 来实现的。
HTML 代码
<div class="box3">
<div class="center5">文字居中</div>
</div>
CSS 代码
.box3{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
}
.center5{
text-align: center;
line-height: 200px;
}
效果展示
05
PART
第二个方法可以通过网格布局 grid 来实现。而这里通过 grid 有两种方式实现,一种对元素本身属性进行设置,另一种在元素的父级元素中设置。两者看上去内容似乎差不多,不同的是在元素中设置的是 align-self 还要多了一个 margin,父级元素中是 align-items。
相关代码:
/* grid 元素中设置 */
.box4{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
}
.center6{
align-self: center;
justify-content: center;
margin: auto;
}
/* grid 父级元素中设置 */
.box5{
background-color: #eee;
width: 200px;
height: 200px;
margin-top: 20px;
display: grid;
align-items: center;
justify-content: center;
}
效果展示:
以上就是关于 CSS 如何将元素或者文字进行水平垂直居中的几种常用方法,大家还其他关于 CSS 实现水平垂直居中的方法吗?请在评论区留下你的想法。
关注w3cschool编程狮订阅更多IT资讯、技术干货~
在网页中将 HTML 元素水平居中,可以使用 CSS 中的 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>Horizontal Centering</title>
<style>
.center {
width: 300px; /* 设置元素宽度 */
margin: 0 auto; /* 水平居中 */
background-color: lightblue;
padding: 20px;
}
</style>
</head>
<body>
<div class="center">
<p>This element is horizontally centered.</p>
</div>
</body>
</html>
在上面的示例中, center 类的元素使用了 width: 300px; 来设置宽度,然后通过 margin: 0 auto; 来实现水平居中。这样,无论屏幕宽度如何变化,元素都会始终水平居中显示。
您也可以将此样式应用到任何 HTML 元素(例如 div 、 span 、 p 等),以实现水平居中效果。
内容首发于工粽号:程序员大澈,每日分享一段优质代码片段,欢迎关注和投稿!
大家好,我是大澈!
本文约 500+ 字,整篇阅读约需 1 分钟。
今天分享一段优质 CSS 代码片段,可以轻松实现元素垂直水平居中对齐。
老规矩,先阅读代码片段并思考,再看代码解析再思考,最后评论区留下你的见解!
div {
display: grid;
place-content: center;
}
分享原因
这段代码展示了如何使用 CSS Grid 布局以及 place-content 属性来居中对齐元素。
CSS Grid 是一种强大的布局工具,提供了简单且灵活的方式来实现复杂的布局,而place-content 属性进一步简化了居中对齐的实现。
相较于 Flex 布局,Grid 布局仅仅只需一行代码即可搞定居中对齐!
代码解析
1. display: grid;
设置元素为网格容器。
网格容器可以通过定义网格行和列来布置其子元素。
2. place-content: center;
place-content 是 align-content 和 justify-content 的组合属性。
center 值表示内容在容器的中央对齐。
3. 兼容性
place-content: center; 的兼容性在现代浏览器中较好,某一些旧版浏览器的不兼容完全可以忽略了。
以下是一些常见浏览器的具体支持情况:
*请认真填写需求信息,我们会在24小时内与您取得联系。