• 技术文章 >web前端 >css教程

    CSS怎么设置垂直居中?

    青灯夜游青灯夜游2019-11-23 13:40:38原创29533
    在我们开发前端页面的时候,为了让页面效果美观,会出现需要垂直居中效果的地方。本章就让我们来了解一下用css如何设置垂直居中,详细介绍一下设置文字与div盒子的垂直居中的几种方法。有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    推荐手册CSS在线手册

    大前端零基础入门到就业:进入学习

    一:css如何设置文本文字垂直居中

    1、line-height 使文字垂直居中

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>css 垂直居中</title>
    		<style>
    			.box{
    				width: 300px;
    			    height: 300px;
    			    background: #ddd;
    			    line-height:300px;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="box">css 垂直居中了--文本文字</div>
    	</body>
    </html>

    效果图:

    9.8.2.jpg

    这样就能让div中的文字水平垂直居中了

    2、CSS3的flex布局 使文字垂直居中

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>css 垂直居中</title>
    		<style>
    			.box{
    				width: 300px;
    			    height: 300px;
    			    background: #ddd;
    			    line-height:300px;
    			     /*设置为伸缩容器*/
    			    display: -webkit-box;
    			    display: -moz-box;
    			    display: -ms-flexbox;
    			    display: -webkit-flex;
    			    display: flex;
    			    /*垂直居中*/
    			    -webkit-box-align: center;/*旧版本*/
    			    -moz-box-align: center;/*旧版本*/
    			    -ms-flex-align: center;/*混合版本*/
    			    -webkit-align-items: center;/*新版本*/
    			    align-items: center;/*新版本*/
    			}
    		</style>
    	</head>
    	<body>
    		<div class="box">css 垂直居中--文本文字(弹性布局)</div>
    	</body>
    </html>

    效果图:

    9.8.2.jpg

    相关文章推荐:
    1.Div垂直居中效果怎么实现
    2.div标签:水平居中和垂直居中的实现(附代码)
    相关视频推荐:
    1.CSS视频教程-玉女心经版

    二:css如何设置div盒子容器(块状元素)垂直居中

    1.使用绝对定位和负外边距对块级元素进行垂直居中 (已知元素的高度)

    如果我们知道元素的高度,可以这样来实现垂直居中:

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>css 垂直居中</title>
    		<style>
    			.box{
    				width: 300px;
    			    height: 300px;
    			    background: #ddd;
    			    position: relative;
    			}
    			.child{
    				width: 150px;
    			    height: 100px;
    			    background: orange;
    			    position: absolute;
    			    top: 50%;
    			    margin: -50px 0 0 0;
    			    line-height: 100px;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="box">
    		    <div class="child">css 垂直居中</div>
    		</div>
    	</body>
    </html>

    效果图:

    9.8.2.jpg

    这个方法兼容性不错,但是有一个小缺点:必须提前知道被居中块级元素的尺寸,否则无法准确实现垂直居中。

    2. 使用绝对定位和transform(未知元素高度)

    如果我们不知道元素的高度,那么就需要先将元素定位到容器的中心位置,然后使用 transform 的 translate 属性,将元素的中心和父容器的中心重合,从而实现垂直居中:

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>css 垂直居中</title>
    		<style>
    			.box{
    				width: 300px;
    			    height: 300px;
    			    background: #ddd;
    			    position: relative;
    			}
    			.child{
    				background: #93BC49;
    			    position: absolute;
    			    top: 50%;
    			    transform: translate(0, -50%);
    			}
    		</style>
    	</head>
    	<body>
    		<div class="box">
    		    <div class="child">css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中,css 垂直居中</div>
    		</div>
    	</body>
    </html>

    效果图:

    9.8.2.jpg

    这种方法有一个非常明显的好处就是不必提前知道被居中元素的尺寸了,因为transform中translate偏移的百分比就是相对于元素自身的尺寸而言的。

    3. 绝对定位结合margin: auto

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>css 垂直居中</title>
    		<style>
    			.box{
    				width: 300px;
    			    height: 300px;
    			    background: #ddd;
    			    position: relative;
    			}
    			.child{
    				width: 200px;
                    height: 100px;
    				background: orange;
    			    position: absolute;
    			    top: 0;
    			    bottom: 0;
    			    margin: auto;
    			    line-height: 100px;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="box">
    		    <div class="child">css 垂直居中...</div>
    		</div>
    	</body>
    </html>

    效果图:

    9.8.2.jpg

    这种方法需要先 把要垂直居中的元素相对于父元素绝对定位,top和bottom设为相等的值,不管设置成多少值,只要两者相等就行;然后再将要居中元素的margin设为auto,这样便可以实现垂直居中了。被居中元素的宽高也可以不设置,但不设置的话就必须是图片这种自身就包含尺寸的元素,否则无法实现。

    4. 使用padding实现子元素的垂直居中

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>css 垂直居中</title>
    		<style>
    			.box{
    				width: 300px;
    			    background: #ddd;
    			    padding: 100px 0;
    			}
    			.child{
    				width: 200px;
                    height: 100px;
    				background: orange;
    			    line-height: 100px;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="box">
    		    <div class="child">css 垂直居中了</div>
    		</div>
    	</body>
    </html>

    效果图:

    9.8.2.jpg

    这种实现方式非常简单,就是给父元素设置相等的上下内边距,则子元素自然是垂直居中的,当然这时候父元素是不能设置高度的,要让它自动被填充起来,除非设置了一个正好等于上内边距+子元素高度+下内边距的值,否则无法精确的垂直居中。这种方式看似没有什么技术含量,但其实在某些场景下也是非常好用的。

    5. 使用flex布局

    <!DOCTYPE html>
    <html>
    	<head>
    		<meta charset="UTF-8">
    		<title>css 垂直居中</title>
    		<style>
    			.box{
    				width: 300px;
    			    height: 300px;
    			    background: #ddd;
    			    display: flex;
    			    flex-direction: column;
    			    justify-content: center;
    			}
    			.child{
    				width: 300px;
    			    height: 100px;
    			    background: #08BC67;
    			    line-height: 100px;
    			}
    		</style>
    	</head>
    	<body>
    		<div class="box">
    		    <div class="child">css 垂直居中了--弹性布局</div>
    		</div>
    	</body>
    </html>

    效果图:

    9.8.2.jpg

    关于flex布局(弹性布局/伸缩布局)里门道颇多,这里先针对用到的东西简单说一下:
    flex也就是flexible,意为灵活的、柔韧的、易弯曲的。
    元素可以通过设置display:flex;将其指定为flex布局的容器,指定好了容器之后再为其添加align-items属性,该属性定义项目在交叉轴(这里是纵向轴)上的对齐方式,可能的取值有五个,分别如下:
      flex-start::交叉轴的起点对齐;
      flex-end:交叉轴的终点对齐;
      center:交叉轴的中点对齐;
      baseline:项目第一行文字的基线对齐;
      stretch(该值是默认值):如果项目没有设置高度或者设为了auto,那么将占满整个容器的高度。

    以上就是CSS怎么设置垂直居中?的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

    前端(VUE)零基础到就业课程:点击学习

    清晰的学习路线+老师随时辅导答疑

    自己动手写 PHP MVC 框架:点击学习

    快速了解MVC架构、了解框架底层运行原理

    上一篇:CSS用@media媒体查询进行响应式设计,什么是媒体查询 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • ❤️‍🔥共22门课程,总价3725元,会员免费学• ❤️‍🔥接口自动化测试不想写代码?• 关于td的height和line-height兼容问题_经验交流• font和line-height之CSS代码书写顺序不同,导致显示效果不一样_经验交流• 说一说line-height - 北风吹雪• 说一说line-height_html/css_WEB-ITnose• 一些常用css技巧的为什么(二)我所理解的line-height_html/css_WEB-ITnose
    1/1

    PHP中文网