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. cakephp 2.0 源码解读(一)

    lib - Cake -basic.php -bootstrap.php -Cache -Config -Configure -Controller -Core -Error -Event -I18n ...

  2. JAVA中浅复制与深复制 - coolmist - ITeye技术网站

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

  3. cocos2dx 3.2 的中国象棋游戏

    改编来源:http://cn.cocos2d-x.org/tutorial/lists?id=103 在cocos2dx官网看到了这么个教程,是cocos2dx 2.x版本的,于是用 cocos2dx ...

  4. Openlayers实现第一张地图

    <html><head><title>OpenLayers Hello World</title> <style type="text/ ...

  5. [Unity Shader]Shader前述

    什么是Shader   Shader,也就是着色器,它的工作就是读取你的网格并渲染在屏幕上.Shader可以定义一些属性,你会用它来影响渲染模型时所显示的效果.当存储了这些属性的设置时,就是一个Mat ...

  6. Java对文件的16进制读取和操作

    大家可以参考一下源代码的相关部分注释,然后写出自己的16进制处理程序.有几个重点地方: 16进制字符串->10进制数 int input = Integer.parseInt("Str ...

  7. IIS Default Web Site : The service did not response to the start or control request in a timely fashion

    IIS Default Web Site无法启动,提示错误:The service did not response to the start or control request in a time ...

  8. PHP 安装使用 gearman

    1.安装服务器端: 官方下载,请到https://launchpad.net/gearmand. yum install boost-devel* gperf* libevent-devel* lib ...

  9. UVa 124 - Following Orders

    题目大意:给一个变量列表和变量的大小关系,输出所有的满足约束的序列. 构建为有向图,然后就是拓扑排序,使用回溯输出所有的结果. #include <cstdio> #include < ...

  10. Jquery插件之ajaxForm ajaxSubmit的理解用法

      如今ajax满天飞,作为重点的form自然也受到照顾. 其实,我们在平常使用Jquery异步提交表单,一般是在submit()中,使用$.ajax进行.比如:   $(function(){ $( ...