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. ubuntu 开启 ftp 服务 | mingming-killer

    body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...

  2. Frequent Distribution sorted by frequency

    import nltk def freq_sorted(text,ranklimit): fd=nltk.FreqDist(text) cumulative = 0.0 for rank, (word ...

  3. (中等) POJ 2528 Mayor's posters , 离散+线段树。

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  4. ios上 更改 状态栏(UIStatusBar)的颜色,你值得一看、收藏

    IOS上 关于状态栏的相关设置(UIStatusBar) 知识普及 ios上状态栏 就是指的最上面的20像素高的部分 状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时 ...

  5. Listener

    通过Listner获得当前的用户个数 package listener; import javax.servlet.ServletContext; import javax.servlet.Servl ...

  6. WebService 使用wsdl.exe生成代理类

    利用wsdl.exe生成webservice代理类: 根据提供的wsdl生成webservice代理类 1.开始->程序->Visual Studio 2005 命令提示 2.输入如下红色 ...

  7. Memcached源码分析之内存管理

    先再说明一下,我本次分析的memcached版本是1.4.20,有些旧的版本关于内存管理的机制和数据结构与1.4.20有一定的差异(本文中会提到). 一)模型分析在开始解剖memcached关于内存管 ...

  8. bzoj2628: JZPSTR

    Description 问题描述 你要对一个字符串进行三种操作: 0. 在位置x_i处插入一个字符串y_i 1. 删除位置[x_i, y_i)的字符串 2. 查询位置[x_i, y_i)的字符串包含多 ...

  9. 递归添加 另一个ds 里的DataRow 时 报错:该行已经属于另一个表。

    public void create_tree(DataSet ds, int parentid)        { DataSet newds = new DataSet();            ...

  10. 数据可视化-OmniGraffle软件

    OmniGraffle Pro for mac破解版是一款运行在MAC OS平台上的思维导图流程图制作软件,通过思维导图软件(OmniGraffle Pro MAC)帮你组织头脑中思考的信息,组织头脑 ...