css 文字和子元素水平垂直居中
关于水平垂直居中,这是一个很简单的问题,但是很多时候,往往简单的东西,反而做不出来。这就是基础不扎实的缘故吧,我参照一些资料,总结了水平垂直居中的几种方法如下:
1 .文字水平垂直居中
这个比较简单,只要分别设置水平集中和垂直居中即可
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
div{
width:500px;
height:100px;
background: #ccc;
text-align:center;
line-height: 100px;
}
</style>
</head>
<body>
<div>
I am huanying2015!
</div>
</body>
</html>
2.水平垂直居中
2.1 把父元素设置成相对定位,子元素设置成绝对定位,margin为auto,left/right/top/bottom都分别设置为0;(备注:如果不是图片,是其他子元素的话,要设定子元素的宽和高,否则显示不出来,行内元素(inline)是没有宽高的,会聚集到最中央的一点去)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:500px;
height:500px;
background: #ccc;
margin:30px auto;
position:relative;
}
img{
position:absolute;
margin:auto;
left:0;
right:0;
top:0;
bottom: 0;
} </style>
</head>
<body>
<div class="box">
<img src="picture.jpg" alt="">
</div>
</body>
</html>
分析:子元素的上下左右(top/bottom/left/right)都设置为0,margin设置为auto;从四个方向进行定位,可以形象的看作是从四面拉扯,最终在中间进行平衡~~,形象一点,可以叫做四面拉扯法或者四点定位法
2.2 子元素居中,从原理上进行定位:
父元素设置position为relative;元素设置为absolute;要居中定位,实际上是要找子元素的起始点的位置,也就是左上角的点位置;一个元素,起始点定了,加上它本身的高和宽,这个元素就定型了;父元素的width/height,相当于子元素的left/top;子元素在父素中的定位,实际上就是本身margin的变化,这样,就可以找到子元素的起始点了:
子y = top*50% - margin-top*50%; 子x = left*50%- margin-left*50%;代码表示如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:500px;
height:500px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:200px;
height:200px;
top:50%;
left:50%;
margin-left:-100px;
margin-top:-100px;
background: #666;
}
</style>
</head>
<body>
<div class="box">
<span class="child"></span>
</div>
</body>
</html>
2.3 采用table的属性,将父元素设置display:table-cell;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:500px;
height:500px;
background: #ccc;
display:table-cell;
vertical-align:middle;
text-align:center;
margin:30px auto;
position:relative;
}
.child{
display:inline-block;
width:200px;
height:200px;
background: #666;
}
</style>
</head>
<body>
<div class="box">
<span class="child"></span>
</div>
</body>
</html>
2.4 采用盒子模型flex;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:500px;
height:500px;
background: #ccc;
margin:30px auto;
position:relative;
display:flex;
display:-webkit-flex;
align-items:center;
-webkit-align-items:center;
justify-content:center;
}
.child{
width:200px;
height:200px;
background: #666;
}
</style>
</head>
<body>
<div class="box">
<span class="child"></span>
</div>
</body>
</html>
2.5 采用Css3 的 transform 的translate属性进行水平垂直居中:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style lang="">
.box{
width:500px;
height:500px;
background: #ccc;
margin:30px auto;
position:relative;
}
.child{
position:absolute;
width:200px;
height:200px;
top:50%;
left:50%;
background: #666;
-ms-transform: translate(-50%,-50%);
-moz-transform: translate(-50%,-50%);
-o-transform: translate(-50%,-50%);
transform: translate(-50%,-50%);
}
</style>
</head>
<body>
<div class="box">
<span class="child"></span>
</div>
</body>
</html>
运行结果:

css 文字和子元素水平垂直居中的更多相关文章
- CSS未知宽高元素水平垂直居中
方法一 :table.cell-table 思路:显示设置父元素为:table,子元素为:cell-table,这样就可以使用vertical-align: center,实现水平居中优点:父元素(p ...
- CSS中怎么设置元素水平垂直居中?
记录怎么使用text-align与vertical-align属性设置元素在容器中垂直居中对齐.text-align与vertical-align虽然都是设置元素内部对齐方式的,但两者的用法还是有略微 ...
- CSS多种方式实现元素水平垂直居中
html结构: <div class="center">确定宽高水平垂直居中</div> <div class="center2" ...
- css实现块级元素水平垂直居中的方法?
父级给相对定位,子级给绝对定位,margin设置为auto,上下左右值设为0. 父级给相对定位,子级给绝对定位,设置left和top为50%,再向左和向上移动负的子级一半. 父级设置display:f ...
- css 常用的绝对定位元素水平垂直居中的方法
两种方法都能够实现: 1. div { height:80%; /*一定要设置高度*/ overflow:hidden;/*建议设置*/ margin: auto; position: absolut ...
- css 实现元素水平垂直居中总结5中方法
个人总结,如有错误请指出,有好的建议请留言.o(^▽^)o 一.margin:0 auto:text-align:center:line-height方法 <div id="divAu ...
- CSS元素水平垂直居中方法总结(主要对大漠以及张鑫旭博客所述方法进行了归纳)
本文主要是对主流居中方法进行了归纳,有些地方甚至就是把别人的代码直接复制过来的,没有什么自己的东西,除了大漠以及张鑫旭的方法外,还有来自司徒正美.怿飞博客的几个方法 以下方法,由于测试环境的原因,IE ...
- css进阶 04-如何让一个元素水平垂直居中?
04-如何让一个元素水平垂直居中? #前言 老板的手机收到一个红包,为什么红包没居中? 如何让一个子元素在父容器里水平垂直居中?这个问题必考,在实战开发中,也应用得非常多. 你也许能顺手写出好几种实现 ...
- CSS实现文字和图片的水平垂直居中
关于文字和图片的水平垂直居中,在前端界绝对算是一个老生常谈的问题了,尤其是垂直居中,什么千奇百怪的解法都能想的出来.下面我就总结一些比较常用的方法: 一.文本的水平垂直居中: 1.水平居中: 是不是很 ...
随机推荐
- Mysql免安装版配置【图文版和文字版】
图文版 配置环境变量 新建一个my.ini文件,添加下面内容 [mysqld] basedir=C:\\software\Mysql\mysql-5.7.14-winx64 datadir=C:\\s ...
- Python: 列表注意细节与元组的基本用法
列表注意细节: 1.list.clear():将列表中成员清空(与del list区别开) 2.list.copy():复制一份相同的列表(浅COPY,只复制列表第一层) 3.如果两个列表相等,如li ...
- Java学习笔记二---设置环境变量JAVA_HOME,CLASSPATH,PATH
1.环境变量包括: JAVA_HOME,CLASSPATH,PATH 2.设置环境变量的目的: 路径搜索,方便查找到jdk的安装路径.方便搜索用到的类文件.方便搜索用到的可执行文件如java,java ...
- webpack2进阶之多文件,DLL,以及webpack-merge
本需要对webpack已有一定的了解,如果你没接触过webpack或者刚刚接触webpack,可以考虑先看一下我的这篇教程. 入门教程 1.打包多文件 之前,当需要打包多个而文件时,我是这么写的: m ...
- Spring 级联属性
Spring 级联属性是当两个bean 关联时 从一个bean 给 另一个bean 赋值 Application xml 配置如下 <bean id="ZhangSan" ...
- CentOS7的一些初始化
默认最小化安装 [root@GVMCET001 ~]# nmtui 设置网络,主机名等 [root@GVMCET001 ~]# yum update 更新系统 SSH [root@GVMCET001 ...
- Struts2的核心运行流程,原理图解
感觉很有必要制定一个计划,这样盲目的想到哪里写到哪里,不符合我大程序员的思维逻辑呀~~~嗯...那就从基本的开始吧,循循渐进,今天想到的先写上,不能浪费了,哈哈哈................... ...
- asp.net core权限模块的快速构建
大部分系统都会有权限模块,别人家系统的权限怎么生成的我不知道,我只知道这样做是可以并且挺好的. 文章中只对asp.net core的部分代码进行说明 呃 记录~,mvc版本自行前往仓库查阅 代码中的一 ...
- vue-ajax小封装
1. js 文件: /** ajax封装:* 1. 引入文件* 2. new Vue().ajax.get(url,data,fn,ojson), 或 new Vue().ajax.post(url, ...
- POJ-1273-Drainage Ditches(网络流之最大流)
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...