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.水平居中: 是不是很 ...
随机推荐
- es6中对象的类与继承方法
对于对象,我一直搞不清楚到底是该如何去继承,如何去书写.在熟练es6之后,终于会尝试写出来了. 代码如下: //我们假定父类为person,子类为man class person{ construct ...
- Ansible系列(三):YAML语法和playbook写法
html { font-family: sans-serif } body { margin: 0 } article,aside,details,figcaption,figure,footer,h ...
- mybatis入门篇基——基本配置与参数说明
Mybatis 好吧这是我第一次写这种文章~如果有不足和错误之处欢迎评论,指点.今天想谈谈关于mybatis的一些基础入门知识. 进入正题~~: a.关于mybatis: 我个人觉得mybatis深得 ...
- CSDN博客新手使用方案
CSDN博客简易使用 在CSDN上写博客,总是遇到很多问题,虽然这些问题很简单,但是对于新手来说,缺经常遇到,因此写篇博客记载. 一.CSDN的博客如何上传图片 如果有现成 ...
- ACM学习之路___HDU 1385(带路径保存的 Floyd)
Description These are N cities in Spring country. Between each pair of cities there may be one trans ...
- Linux的硬盘使用情况、挂载、SSD挂载(查看df -h不能看到的卷)
linux上的盘和window的有区别,磁盘空间必须挂载在目录上,要不然没用 对与新增的硬盘.SSD固态硬盘.挂载到linux上的操作如下: df -h #显示目前在Linux系统上的文件系 ...
- const的用法,特别是用在函数前面与后面的区别!
const的用法,特别是用在函数后面 在普通的非 const成员函数中,this的类型是一个指向类类型的 const指针.可以改变this所指向的值,但不能改变 this所保存的地址. 在 const ...
- 记2017问鼎杯预赛的wp---来自一个小菜鸡的感想
这次准备写一下几个misc和密码题目..很坑. 打了一整天的比赛,越来越觉得自己很菜了. 有一道题目叫做"真真假假",这道题目只有一个提示--Xor.第一眼知道是异或,也就知道这一 ...
- FZU 1919 -- K-way Merging sort(记忆化搜索)
题目链接 Problem Description As we all known, merge sort is an O(nlogn) comparison-based sorting algorit ...
- Easyui后台管理角色权限控制
最近需要做一个粗略的后台管理的权限,根据用户的等级来加载相应的菜单,控制到子菜单.使用的是Easyui这个框架. 1.我使用的mysql数据库.在这里我就建立四张表,角色表(tb_users),菜单表 ...