行内元素水平居中

把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center;

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>css水平垂直居中</title>
<style>
body{background:#000;}
#container{
background: #ccc;
text-align: center;
}
</style>
</head>
<body>
<div id="container">行内元素水平居中</div>
</body>
</html>

块状元素水平居中

将块状元素的左右外边距设置为auto即可。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>块状元素水平居中</title>
<style>
body{background:#000;}
#container{
background: #ccc; }
#box1{
width:300px;
height:300px;
background:#fff;
margin: 0 auto;
text-align: center;
}
</style>
</head>
<body>
<div id="container">
<div id="box1">块状元素水平居中</div>
</div>
</body>
</html>

多个块状元素水平居中的情况:

1、传统方法:将水平排列的块状元素设置为:display:inline-block;然后父元素设置为text-align:center;即可。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>多个块状元素水平居中</title>
<style>
body{background:#000;}
#container{
background: #ccc;
text-align: center;
}
.box1{
width:300px;
height:300px;
background:#fff;
display: inline-block;
}
</style>
</head>
<body>
<div id="container">
<div class="box1">块状元素水平居中</div>
<div class="box1">块状元素水平居中</div>
<div class="box1">块状元素水平居中</div>
</div>
</body>
</html>

2、使用弹性盒子:将父元素设置为display:-webkit-box;-webkit-box-pack:justify(或center);注意兼容性

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>多个块状元素水平居中</title>
<style>
body{background:#000;}
#container{
background: #ccc;
display: -webkit-box;
-webkit-box-pack:justify;/*box-pack决定了父元素水平空间的使用*/
}
.box1{
width:300px;
height:300px;
background:#fff;
}
</style>
</head>
<body>
<div id="container">
<div class="box1">块状元素水平居中</div>
<div class="box1">块状元素水平居中</div>
<div class="box1">块状元素水平居中</div>
</div>
</body>
</html>

已知元素宽高水平垂直居中

1、绝对定位与margin

这个方法使用了position:absolute;有固定宽高的div,被设置为top:0;bottom:0;因为它有固定高度,不能与上下的间距都为0,margin:auto;会使它居中。

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>多个块状元素水平居中</title>
<style>
body{background:#000;}
#container{
background: #ccc;
position: relative;
width:1000px;
height:500px;
}
.box1{
position: absolute;
background:#fff;
width: 300px;
height: 300px;
top: 0;
left: 0;
bottom: 0;
right: 0;
margin: auto; }
</style>
</head>
<body>
<div id="container">
<div class="box1">已知元素宽高水平垂直居中</div>
</div>
</body>
</html>

2、绝对定位与margin负值

利用绝对定位,将元素的top、left值设为50%,然后设置外边距margin-left:width/2;margin-top:height/2;实现垂直居中效果

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>多个块状元素水平居中</title>
<style>
body{background:#000;}
#container{
background: #ccc;
position: relative;
width:1000px;
height:500px;
}
.box1{
position: absolute;
background:#fff;
width: 300px;
height: 300px;
top: 50%;
left: 50%;
margin-left:-150px;
margin-top:-150px;
}
</style>
</head>
<body>
<div id="container">
<div class="box1">已知元素宽高水平垂直居中</div>
</div>
</body>
</html>

未知元素宽高水平垂直居中

1、把div的显示方式设置为表格,使用表格的vertical-align属性

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>未知宽高的元素水平垂直居中</title>
<style>
body{background: #000;font-size: 24px;}
#wrapper{
display: table;
background: #ccc;
width: 1000px;
height:300px; }
#cell{
display: table-cell;
text-align: center;
vertical-align: middle;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="cell">
<div class="content">未知宽高的元素水平垂直居中</div>
</div>
</div>
</body>
</html>

2、跟上面提到的多个块状元素水平居中的用法差不多,这里用到弹性盒子是要注意浏览器的兼容性;(这里被居中元素的宽度由其里面的内容决定)

<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>未知宽高的元素水平垂直居中</title>
<style>
body{background: #000;font-size: 24px;}
#container{
margin:30px auto;
background: #ccc;
width: 1000px;
height:300px;
display: -webkit-box;
-webkit-box-pack:center;
}
#container .box1{
background: red;
}
#container .box2{
background: green;
}
#container .box3{
background: yellow;
}
</style>
</head>
<body>
<div id="container">
<div class="box1">未知宽高的元素水平垂直居中</div>
<div class="box2">未知宽高的元素水平垂直居中</div>
<div class="box3">未知宽高的元素水平垂直居中</div>
</div>
</body>
</html>

CSS水平垂直居中总结的更多相关文章

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

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

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

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

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

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

  4. css 水平垂直居中总结

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

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

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

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

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

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

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

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

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

  9. css水平垂直居中

    margin法(水平居中) 需要满足三个条件: 元素定宽 元素为块级元素或行内元素设置display:block 元素的margin-left和margin-right都必须设置为auto 三个条件缺 ...

随机推荐

  1. VedioCaptureHelper

    void testFun() { chStringA strDevName; chStringA strDevID; chStringA useDevName = "WIN2 USB2.0 ...

  2. 重置mysql数据库密码相关方法

    方法一: 在my.ini的[mysqld]字段加入:skip-grant-tables重启mysql服务,这时的mysql不需要密码即可登录数据库 然后进入mysqlmysql>use mysq ...

  3. ABAP BDC

    REPORT程序中用BDC录入 DATA: GS_BDC TYPE BDCDATA, GT_BDC TYPE TABLE OF BDCDATA, GS_MSG TYPE BDCMSGCOLL, GT_ ...

  4. ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) 背景: mys ...

  5. Appium客户端

    Appium版本:1.5.3 Xcode有两个版本:Xcode8.1   Xcode7.2.1 iOS10以下只能用Xcode7.2.1 iOS10及以上可以用Xcode8.1   1.Appium客 ...

  6. 复利test

    因为上节课老师有给我们讲了单元测试的概念及其基本操作,我就对我之前的复利计算器进行了改进和测试.因为从控制台的简单输入输出对单元测试来说不够完善,便参考了其他同学的优秀编程又重新写了一个复利计算器的框 ...

  7. Win8 x64环境下VS2010 C#工程运行报错:没有注册类 (异常来自 HRESULT:0x80040154

    来源:http://blog.sina.com.cn/s/blog_7095482001019c2v.html 问题描述: 在Win8 x64环境下,VS2010的C#工程中引用了COM组件(Acti ...

  8. php openssl 增加密钥

      生成私钥:openssl genrsa 1024 > private.key (注意,1024是密钥的长度,如果密钥较长,相应加密后的密文也会较长) 生成公钥:openssl rsa -in ...

  9. hdoj 2544最短路

    Problem Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要 ...

  10. 文件过滤器 filter

    OpenFileDialog对话框的Filter属性说明: 首先说明一个示例,分析一下Filter属性的构成:“ Excel文件|*.xls ”,前面的“Excel文件”成为标签,是一个可读的字符串, ...