margin法(水平居中)

需要满足三个条件:

  • 元素定宽
  • 元素为块级元素或行内元素设置display:block
  • 元素的margin-leftmargin-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水平垂直居中的更多相关文章

  1. CSS水平垂直居中总结

    行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...

  2. css水平垂直居中对齐方式

    1.文字或者内联元素的垂直水平居中对齐 css属性 -- 水平居中:text-aligin:center; 垂直居中: line-height:height; 例子:. html: <div c ...

  3. 把简单做好也不简单-css水平垂直居中

    44年前我们把人送上月球,但在CSS中我们仍然不能很好实现水平垂直居中. 作者:Icarus 原文链接:http://xdlrt.github.io/2016/12/15/2016-12-15 水平垂 ...

  4. CSS水平垂直居中的方法

    原文链接:http://caibaojian.com/370.html 水平垂直居中,特别是使用在列表的时候经常会用到的,以前有需求的时候我也做过类似的代码,是使用display:table-cell ...

  5. css 水平垂直居中总结

    空闲总结了下水平垂直居中方案,欢迎补充: 水平居中 水平居中有两种情况: 子元素是内联元素 这种那个情况下只需要在父元素定义: text-align:center; 例子: html: //省略了bo ...

  6. CSS水平垂直居中的几种方法2

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

  7. CSS水平垂直居中的几种方法

    直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...

  8. 常见的几种 CSS 水平垂直居中解决办法

    用CSS实现元素的水平居中,比较简单,可以设置text-align center,或者设置 margin-left:auto; margin-right:auto 之类的即可. 主要麻烦的地方还是在垂 ...

  9. 介绍一种css水平垂直居中的方法(非常好用!)

    这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...

随机推荐

  1. ip识别运用

    tcp/ip协议中,专门保留了三个IP地址区域作为私有地址,其地址范围如下: 10.0.0.0/8:10.0.0.0-10.255.255.255 172.16.0.0/12:172.16.0.0-1 ...

  2. (简单) POJ 2352 Stars,Treap。

    Description Astronomers often examine star maps where stars are represented by points on a plane and ...

  3. Android相关修改教程

    ICS: 自己动手实现接通震动功能 原生来电归属地修改教程

  4. 对AD域进行定期自动备份设置图解

    今天为大家讲解一下,如何对域进行定期的备份,因为如果域出问题了,在公司里那可就不好玩了啊,对做定期备份,在域出问题的时候可以及时恢复,减少对域重建而浪费大量的时间,同样也耽误公司员工的工作,这样的事情 ...

  5. IOS 股票K线图、分时图

    IOS 股票K线图.分时图,网上开源项目很少,质量也是参差不齐:偶尔搜索到看似有希望的文章,点进去,还是个标题党:深受毒害.经过一段时间的探索,终于在开源基础上完成了自己的股票K线图.分时图: 先放出 ...

  6. Delphi 与 DirectX

    关于DirectX 在Delphi下的使用 源:Delphi 与 DirectX

  7. HTTP协议快速入门

    一.定义 The Hypertext Transfer Protocol (HTTP) is an application protocol for distributed, collaborativ ...

  8. winform总结5> winform程序开发注意事项

    1.全局异常捕获 Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //处理UI线程异常 Ap ...

  9. PHP新手之学习类与对象(3)

    四.访问控制 对属性或方法的访问控制,是通过在前面添加关键字 public.protected 或 private 来实现的.由 public 所定义的类成员可以在任何地方被访问:由 protecte ...

  10. Delphi中String类型原理介绍

    Delphi中字符串的操作很简单,但幕后情况却相当复杂.Pascal传统的字符串操作方法与Windows不同,Windows吸取了C语言的字符串操作方法.32位Delphi中增加了长字符串类型,该类型 ...