css水平垂直居中
margin法(水平居中)
需要满足三个条件:
- 元素定宽
- 元素为块级元素或行内元素设置
display:block - 元素的
margin-left和margin-right都必须设置为auto
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background: orange;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
定位法(水平垂直居中)
首先绝对定位,设置top:50%; left:50%; 然后再利用负margin把它向左、向上移动(移动距离相当于它自身宽高的一半)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background: orange;
position: absolute;
left: 50%;
top: 50%;
margin-left: -50px; /*宽度的一半*/
margin-top: -50px; /*高度的一半*/
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
利用calc()函数简化上面的代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background: orange;
position: absolute;
left: calc(50% - 50px);
top: calc(50% - 50px);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
这个方法的最大的局限性在于它要求元素的宽高是固定的,在通常情况下,对那些需要居中的元素来说,其尺寸往往是由其内容来决定的。如果能找到一个属性的百分比是以元素自身的宽高作为解析基准,那我们的难题就迎刃而解!遗憾的是,对于绝大多数css属性(包括margin)来说,百分比都是以父元素的尺寸为基准进行解析的。
而translate()变形函数中使用百分比值时,是以这个元素自身的宽度和高度为基准进行换算和移动的,这正是我们所需要的,接下来,只要换用基于百分比的css变形来对元素进行偏移,就不需要在偏移量中把元素的尺寸写死了。这样我们就可以彻底解决对固定尺寸的依赖。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.box {
padding: 20px;
background: orange;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="box">this is a test.</div>
</body>
</html>
文字水平垂直居中
对于单行文字来说,直接使用text-align: center即可实现水平居中。使用line-height实现垂直居中。
多行文字可以看作一个块级元素参考margin法和定位法。
绝对水平垂直居中
利用绝对定位与margin:auto; box宽度与高度要固定,常用在弹出层的定位中。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background: orange;
position: absolute;
left: 0;
right: 0;
top: 0;
bottom: 0;
margin: auto;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
writing-mode与垂直居中
我们知道块状元素的水平居中可用margin:0 auto来实现,即margin-left:auto;margin-right:auto;
writing-mode是改变文档流的显示方向的,所以水平居中也可以变为垂直居中。writing-mode结合margin-top:auto,margin-bottom:auto就可以实现。
但水平居中会失效。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.father {
width: 500px;
height: 500px;
background: orange;
writing-mode: vertical-lr;
}
.son {
width: 100px;
height: 100px;
background: red;
margin: auto;
}
</style>
</head>
<body>
<div class="father">
<div class="son"></div>
</div>
</body>
</html>
基于视口单位(水平垂直居中)
vw是相对于视窗的宽度。与你预期刚好相反,1vw相当于视窗宽度的1%,而不是100%
与vw相似的是,1vh相当于视窗高度的1%
如果视窗的宽度小于高度,1vmin等于1vw,反之,如果视窗宽度大于高度,1vmin等于1vh
如果视窗的宽度大于高度,1vmax等于1vw,反之,如果视窗宽度小于高度,1vmax等于1vh
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.box {
width: 100px;
height: 100px;
background: orange;
margin: 50vh auto 0;
transform: translateY(-50%);
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
注意:这种方法最大的局限是只能适用于元素在视窗中垂直居中,如果是在局部的某个地方就无能为力了。
Flexbox的解决方案
如果不考虑浏览器的兼容性,Flexbox无疑是最好的解决方案,因为它的出现就是为了解决这样的问题。
完成这项工作只需要两个样式,在需要水平垂直居中的父元素上设置display:flex(在这个例子中的body元素)和在水平垂直居中的元素上设置margin:auto(在这个例子中的类名为box的元素)
当我们使用Flexbox时,margin:auto;不仅在水平方向上将元素居中,垂直方向上也是如此。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
body {
display: flex;
margin: 0;
min-height: 100vh;
}
.box {
width: 100px;
height: 100px;
background: orange;
margin: auto;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
Flexbox的另一个好处是,它可以将匿名容器(即没有被标签包裹的文本节点)垂直居中。
代码如下:我们先给box指定一个固定的尺寸,然后借助flexbox的align-items和justify-content属性,这样让它内部的文本也实现居中。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title></title>
<style>
.box {
width: 400px;
height: 400px;
background: orange;
display: flex;
align-items: center;
justify-content: center;
}
</style>
</head>
<body>
<div class="box">this is a test.</div>
</body>
</html>
参考:https://xdlrt.github.io/2016/12/15/2016-12-15/
css水平垂直居中的更多相关文章
- CSS水平垂直居中总结
行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...
- css水平垂直居中对齐方式
1.文字或者内联元素的垂直水平居中对齐 css属性 -- 水平居中:text-aligin:center; 垂直居中: line-height:height; 例子:. html: <div c ...
- 把简单做好也不简单-css水平垂直居中
44年前我们把人送上月球,但在CSS中我们仍然不能很好实现水平垂直居中. 作者:Icarus 原文链接:http://xdlrt.github.io/2016/12/15/2016-12-15 水平垂 ...
- CSS水平垂直居中的方法
原文链接:http://caibaojian.com/370.html 水平垂直居中,特别是使用在列表的时候经常会用到的,以前有需求的时候我也做过类似的代码,是使用display:table-cell ...
- css 水平垂直居中总结
空闲总结了下水平垂直居中方案,欢迎补充: 水平居中 水平居中有两种情况: 子元素是内联元素 这种那个情况下只需要在父元素定义: text-align:center; 例子: html: //省略了bo ...
- CSS水平垂直居中的几种方法2
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
- CSS水平垂直居中的几种方法
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
- 常见的几种 CSS 水平垂直居中解决办法
用CSS实现元素的水平居中,比较简单,可以设置text-align center,或者设置 margin-left:auto; margin-right:auto 之类的即可. 主要麻烦的地方还是在垂 ...
- 介绍一种css水平垂直居中的方法(非常好用!)
这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...
随机推荐
- postgresql 数据库的备份和恢复 (pg_dump 和 pg_restore)
pg_dump 用来备份数据库: pg_restore 用来恢复数据库: 备份出来的文件可以用 XZ (linux 自带的压缩工具压缩). XZ压缩最新压缩率之王 xz这个压缩可能很多都很陌生,不过您 ...
- Blog`s CSS
#div_digg { position: fixed; bottom: 10px; width: 50px; right: 50px; filter: alpha(opacity=20); opac ...
- python--字符串操作(删除,替换)
示例: 替换字符串开头和结尾处的空格 1. [代码][Python]代码 跳至 [1] [全屏预览] view source print? 01 # -*- coding: utf-8 -*- ...
- linux分区-df
转自:http://baike.baidu.com/link?url=tyonI3NCB3F-ytIQz72PY-8uAaUQgfFFXbyKAea1e2NiB_t5AsE0MLOLc2LcqOiS ...
- 【转】2015年薪酬大涨的15个IT岗位
近日,国外科技 IT 招聘公司 Robert Half 分析了 70 个科技职位后发现 2015 年从事 IT 从业人员的平均起薪将攀升至 5.7%,其中 15 个职位的提升潜力最大. 当企业在招聘过 ...
- 步进控件——UIStepper
步进控件,可用于替换传统用于输入值的文本框.步进控件提供了“+”和“-”两个按钮,用来改变stepper内部value的增加或减少,调用的事件是UIControlEventValueChanged.由 ...
- Maven的安装和使用
http://repo.spring.io/release/org/springframework/spring/ 安装配置:https://segmentfault.com/a/1190000003 ...
- win8.1远程连接Redis数据库
环境:redis安装在虚拟机Centos6.5系统上 通过java远程连接 问题一:报错 connected refused redis.conf 注释掉 #bind 127.0.0.1 问题二:还是 ...
- yield return 和yield break
这个还是有点意思,两个都是有返回的意思,但是区别在哪里呢? 1.return 会销毁函数的局部变量,下次调用的时候又会产生新的值 2.yield 当退出函数的时候,变量人然存在,函数下次调用的时候变量 ...
- Python的zip函数
zip函数接受任意多个(包括0个和1个)序列作为参数,返回一个tuple列表.具体意思不好用文字来表述,直接看示例: 1.示例1: x = [1, 2, 3] y = [4, 5, 6] z = [7 ...