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.水平居中: 是不是很 ...
随机推荐
- 【C# in depth 第三版】温故而知新(2)
声明 本文欢迎转载,原文地址:http://www.cnblogs.com/DjlNet/p/7522869.html 前言 我们接了上一篇的未完待续,接着我们的划重点之行.....哈哈 理解:LIN ...
- Thrift教程初级篇——thrift安装环境变量配置第一个实例
前言: 因为项目需要跨语言,c++客户端,web服务端,远程调用等需求,所以用到了RPC框架Thrift,刚开始有点虚,第一次接触RPC框架,后来没想到Thrift开发方便上手快,而且性能和稳定性也不 ...
- TypeMismatchException: Provided id of the wrong type for class zhongfucheng.user.entity.User.
今天在使用SSH框架做项目的时候出现了这个错误,找了我非常非常多的时间!!!!!!! Struts Problem Report Struts has detected an unhandled ex ...
- Appium (win7系统)环境搭建----完整版
首先感谢 http://www.cnblogs.com/puresoul/p/4696638.html 和 http://www.cnblogs.com/fnng/p/4540731.html ...
- STS安装
在eclipse中安装spring tool Suite插件需要根据eclipse版本找到对应的spring tool Suite安装包. spring tool Suite 官网地址:http:// ...
- C#ZIP根据路径读取压缩包内文件数量
/// <summary> /// 根据压缩包路径读取此压缩包内文件个数 /// </summary> /// <param name="strAimPath& ...
- 第5章 不要让线程成为脱缰的野马(Keeping your Threads on Leash) ---线程优先权(Thread priority)
有没有过这样的经验?你坐在你的车子里,目的地还在好几公里之遥,而时间已经很晚了.你拼命想告诉那些挡住你去路的人们,今天这个约会对你是多么多么重要,能不能请他们统统--呃--滚到马路外?很不幸,道路系统 ...
- 【Kafka】操作命令
生产者 ./kafka-console-producer.sh --broker-list --topic norm 消费者 ./kafka-console-consumer.sh --zookeep ...
- Django 1.10中文文档-执行查询
Django 1.10中文文档: https://github.com/jhao104/django-chinese-doc 只要创建好 数据模型, Django 会自动为生成一套数据库抽象的API, ...
- VB.NET 打开窗体后关闭自己
第一:要实例化打开的窗体 Dim bb As New frm_Main 第二:打开窗体 show 第三:释放自身 Finalize() '赋值另一窗体的控件值,先实例化,再进行操作 Dim bb ...