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资讯、技术干货~
通常首选方法是使用flexbox居中内容。只需三行代码即可:display:flex,然后使用 align-items:center 和 justify-content:center 将子元素垂直和水平居中。
如下代码:
html:
<div class="flexbox-centering">
<div>Centered content.</div>
</div>
css:
.flexbox-centering {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
}
使用grid(网格)与flexbox非常相似,也是一种常见的技术,尤其是布局中已经使用网格的情况下。与前一种flexbox技术的唯一区别是它显示为栅格。
如下代码:
html:
<div class="grid-centering">
<div class="child">Centered content.</div>
</div>
css:
自己是一名从事了多年开发的web前端老程序员,目前辞职在做自己的web前端私人定制课程,今年年初我花了一个月整理了一份最适合2019年学习的web前端学习干货,各种框架都有整理,送给每一位前端小伙伴,想要获取的可以关注我的头条号并在后台私信我:前端,即可免费获取
Html5-CSS之五大居中方式
你是不是也对元素居中的知识点很是模糊?是不是苦于找不到一个总结的通俗易懂的说明?是不是自己懒得去总结?恭喜你,搜到这篇博客! 这是鄙人在前端的学习与实践中总结出的元素的五大居中方式,黏贴了代码并对代码做了解释,希望对迷茫的有所帮助!
下面的居中示例中,统一使用了同一个div作为父元素和p作为子元素
设置一个div,并且设置了div的宽高边框,div里面设置一个块元素p,设置了它的宽高和背景色
css居中方式1
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中1</title> <style> *{margin:0;} div{width:200px;height:300px;border:2px solid #000;margin:200px auto; text-align:center;font-size:0; } div p{width:100px;height:100px;background:#666; display:inline-block;vertical-align:middle; } div:after{content:"";display:inline-block;height:100%;vertical-align:middle;} </style> </head> <body> <div> <p></p> </div> </body> </html>
这里利用了伪元素让子元素p在div盒子里左右水平居中只需要在它的父元素div里加text-align:center;垂直方向居中需要在父元素后面加了一个伪元素,并使得样式为inline-block;height:100%;就是和父元素一样高,vertical-align:middle;垂直居中,也就是p元素相对与伪元素居中,由于伪元素和div一样高,所以相当于p元素在div里垂直居中。
css居中方式2
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中2</title> <style> *{margin:0;} div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{position:absolute;left:0;bottom:0;right:0;top:0;margin:auto;width:100px;height:100px;background:#f99;} </style> </head> <body> <div> <p></p> </div> </body> </html>
这里利用了定位居中
子元素p设置position:absolute脱离文档流,默认以html作为父元素,所以我们给父元素div设置position:relative;使得p以div为父元素做位置的变动,left:0;tight:0;top:0;bottom:0;(只有设置了定位的元素才可以使用这种方式来移动),最后margin:auto;就会水平和垂直都居中。
css居中方式3
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中3</title> <style> *{margin:0;} div{display:flex;justify-content:center;align-items:center;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{width:100px;height:100px;background:#f99;} </style> </head> <body> <div> <p></p> </div> </body> </html>
这里利用了弹性盒居中
父元素div设置成弹性盒样式,justify-content:center;主轴居中
align-items:center;垂直居中(而且这两个只能设置在父元素上,弹性盒知识)
css居中方式4
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中4</title> <style> *{margin:0;} div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{width:100px;height:100px;background:#f99;position:absolute; left:50%;top:50%;margin:-50px 0 0 -50px;} </style> </head> <body> <div> <p></p> </div> </body> </html>
利用定位线左上角居中,然后左移子元素宽度的一半,再上移子元素高度的一半。
css居中方式5
<!doctype html> <html> <head> <meta charset="utf-8"> <title>五大居中5</title> <style> *{margin:0;} div{position:relative;width:300px;height:400px;border:1px solid #000;margin:100px auto;} p{position:absolute;width:100px;height:100px;background:#f99;left:50%;top:50%; transform:translate(-50%,-50%);} </style> </head> <body> <div> <p></p> </div> </body> </html>
利用动画移动属性transform
结语
相信看了上面的有关Html5、css的元素五大居中方式,你们就可以解决自己的小问题了,但是也要养成一个总结的好习惯。好记性不如烂笔头!以前留下来的话语总是有他的道理。Come on!
原文链接:https://blog.csdn.net/qq_38110274/article/details/102756968
*请认真填写需求信息,我们会在24小时内与您取得联系。