CSS垂直水平居中方法总结
在布局的时候经常能用到居中,在此总结一下
html结构:
<div class="wrap">
<div class="content"></div>
</div>
1.水平居中:margin:auto;
.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
/*margin:auto只能水平居中*/
margin: auto;
}
此方法简单只需一行代码,但是只能水平居中,至于为什么垂直居中不起作用呢:默认宽度继承父级宽度,而高度不能继承可以由内容撑开,居中计算时是相对父级计算因此垂直居中不起作用
效果如下:

2.垂直居中:table-cell
width: 400px;
height: 400px;
background-color: lightblue;
/* 将父元素的display设置为table-cell*/
display: table-cell;
vertical-align: middle;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
}
这里要强调一点就是display:table-cell是对父元素设置的,在表格单元中,vertical-align属性会设置单元格框中的单元格内容的对齐方式。这里设置为middle居中对齐,有兴趣的小伙伴也可以试试其他值如:top,bottom等。
效果如下:

3.一个水平居中,一个垂直居中放在一起是不是就可以垂直水平居中了
.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
display: table-cell;
vertical-align: middle;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
margin: auto;
}
效果图:
bingo就是这么完美
4.绝对定位(高度未知)
.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
/*父元素相对定位*/
position: relative;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
/*子元素绝对定位*/
position: absolute;
/*子元素分别相对top,left偏移50%*/
top:50%;
left: 50%;
/*子元素相对自身top,left分别移动-50%*/
transform: translate(-50%,-50%);
}
效果图:

上面两种方法优点在于可以在高度未知的情况下使用,下面这种方法就要知道子元素高度
5.绝对定位(已知子元素宽高的情况下) 调整负margin值
.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
/*父元素相对定位*/
position: relative;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
/*子元素绝对定位*/
position: absolute;
/*子元素分别相对top,left偏移50%*/
top:50%;
left: 50%;
/*子元素相对自身top,left分别移动负100像素*/
margin-top:-100px;
margin-left:-100px;
}
效果图与上面一种方法一样就不在放了。
6.弹性盒模型:display:flex;
.wrap{
width: 400px;
height: 400px;
background-color: lightblue;
display:flex;
}
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
margin:auto;
}
flex兼容性问题可参考下图:

7.单行文本居中:line-height
html结构:
<div class="content">一切都是最好的安排</div>
css样式:
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
line-height: 200px;
}
效果图:

8.多行文本就不能用line-height了,还可以用上面table-cell方法来使多行文本居中
.content{
width: 200px;
height: 200px;
background-color: lightcoral;
display:table-cell;
vertical-align:middle;
}
可以直接在div(.content)中输入文本或者文本输入在一个标签里
更新与2016-12-30
9.text-align:center+inline-block文本居中
text-align属性用于控制文字的对其与显示,从其渲染与解析上来看,其主要是用来控制inline水平元素或inline-block元素的对齐与显示的,例如嵌套行内标签的文字、图片、input表单控件等;而对block水平的元素是没有作用的。
*{
margin:;
padding:;
}
div{
width:400px;
height:400px;
background-color: #00AA88;
}
ul{
font-size:;
/*因为li标签inline-block后li之间默认有3px间隙,font-size设为0去除间隙*/
text-align: center;
}
li{
font-size: 16px;
display: inline-block;
border:1px solid #000;
}
效果如下:

稍加修改可以作为分页标签来使用。
CSS垂直水平居中方法总结的更多相关文章
- CSS垂直水平居中方法整理
CSS定位中常常用到垂直居中,比如覆盖层上的弹框. 兼容性比较好的方法: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transition ...
- 关于css垂直水平居中的几种方式
css中元素的垂直水平居中是比较常见及较常使用的,在这里向大家介绍一下几种方式. 1.水平居中 margin: 0 auto; 效果图: 而文字的垂直水平居中也比较简单,加上line-height: ...
- css 垂直+水平居中
垂直+水平居中是一个老生常谈的问题了,现在就固定高度和不固定高度两种情况去讨论 1.父盒子固定高度[定位] 实现1: father-box: position:relative child-box:p ...
- CSS之垂直水平居中方法
//居中方法1 position: absolute; top: 0; left: 0; right: 0; bottom: 0; margin: auto; width: 346px; height ...
- css 垂直水平居中总结
一.前言: 垂直居中有很多方式,我们要做的不是写出完美代码,而是在合适的情况下根据需求选择合适方式. 主要方式: line-height 绝对定位 表格 display:table-cell 主要需求 ...
- css垂直水平居中方案
1. 水平居中 如果是inline元素:在父元素上面设置text-align:center; 如果是block元素:设置宽度和margin:0 auto; 如果是多块级元素:在父元素上面设置text- ...
- (转载)css垂直水平居中的整理
方法一 .demo1 { width:180px; height:180px; line-height:180px; *font-size:160px; border:1px solid #ddd; ...
- CSS垂直水平居中
小小的总结一下:行内元素水平居中用text-align: center;块级元素水平居中用margin-left: auto; margin-right: auto; 首先讨论一下单行时的情况. 毫无 ...
- div块元素垂直水平居中方法总结
1.已知块级元素的宽和高,使用绝对定位+外边距设定水平垂直居中. 父元素position:relative,子元素position:absolute;top:50%;left:50%;margin-t ...
随机推荐
- go:channel(未完)
注:1)以下的所有讨论建立在包含整形元素的通道类型之上,即 chan int 2)对于“<-”我的理解是,它可能是一个操作符(接收操作符),也 可能是类型的一部分(如“chan<- in ...
- javaEE基础08
javaEE基础08 一.继承 特点:继承父类的属性和方法,单继承(多继承) 特性:方法的复写(重写) 比如:人可以养狗 人------>狗:整体和部分(拥有)关系 关键字:extends 结构 ...
- APM程序分析-AC_WPNav.cpp
APM程序分析 主程序在ArduCopter.cpp的loop()函数. /// advance_wp_target_along_track - move target location along ...
- 阿里云centos7基于搭建VPN
本文参考自:http://www.xxkwz.cn/1495.html 前段时间使用pptp搭建了一个VPN,速度很快,但是用了大概一个月挂了,估计是被墙了吧,于是,用shadowsocks重新搭建了 ...
- js 字符串操作函数
concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf() – 返回字符串中一个子串第一处出现的索引.如果没有匹配项,返回 -1 . charAt() – 返回指定 ...
- IO边读边写
using (FileStream fs = new FileStream(@"C:\Users\Desktop\lijia1.txt",FileMode.Open)) ...
- python爬虫成长之路(二):抓取代理IP并多线程验证
上回说到,突破反爬虫限制的方法之一就是多用几个代理IP,但前提是我们得拥有有效的代理IP,下面我们来介绍抓取代理IP并多线程快速验证其有效性的过程. 一.抓取代理IP 提供免费代理IP的网站还挺多的, ...
- JS基础学习(二)
昨天把网站上的基础知识看完了,下面是剩下的部分 第六节 JS Window浏览器对象模型 JavaScript全局对象,函数,变量均自动成为window对象的成员. 1.Window对象 1.获取浏览 ...
- C#中的隐式类型var——详细示例解析
从 Visual C# 3.0 开始,在方法范围中声明的变量可以具有隐式类型var.隐式类型可以替代任何类型,它的具体类型由编译器根据上下文推断而出. 下面就让我来总结下隐式类型的一些特点: 1.va ...
- NetMQ(一):zeromq简介
ZeroMQ系列 之NetMQ 一:zeromq简介 二:NetMQ 请求响应模式 Request-Reply 三:NetMQ 发布订阅模式 Publisher-Subscriber 四:NetMQ ...