css 垂直水平居中总结
一、前言:
垂直居中有很多方式,我们要做的不是写出完美代码,而是在合适的情况下根据需求选择合适方式。
主要方式:
- line-height
- 绝对定位
- 表格 display:table-cell
主要需求:
- 固定宽高
- 不固定宽高
主要兼容:
- ie8+ 主流浏览器
- ie6,7
二、行高
1. 利用行高与高度相同,实现单行文本居中
缺点:只能是单行文本
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.d1{width: 200px;height: 200px;background-color: blue;line-height: 200px;}
</style>
</head>
<body>
<div class="d1">
fdsfdsfdsfdfsfds
</div>
</body>
</html>
2.利用inline-block改进(推荐)
改变display属性,就可以是块元素了,vertical-align: middle;设置垂直属性
优点:自适应内容
兼容:>=ie8 + 主流
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
text-align: center;
line-height: 500px;
}
.div2{
display: inline-block;
vertical-align: middle;
width: 200px;
height: 200px;
background-color: red;
/*通过 line-hight 来垂直居中 此法>= ie8*/
}
</style>
<body>
<div class="div1">
<div class="div2"> </div>
</div>
</body>
</html>
三、绝对定位
1.负margin
top,left 50%;margin -50%;
缺点:需固定宽高
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
position: relative;
}
.div2{
position: absolute;
width: 200px;
height: 200px;
background-color: red;
top: 50%;
left: 50%;
margin-left: -100px; /*此处为width的一半,所以应用此法,元素必须固定宽、高*/
margin-top: -100px; }
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>
2.css3 translate
我们希望实现不固定宽高,在上法上改进。可以用js,动态添加宽高,但不推荐。其实可以用css3 translate属性,因为translate是唯一一个相对于自身宽度计算的属性
兼容:>=ie9 + 主流
优点:自适应内容
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
position: relative;
}
.div2{
position: absolute;
background-color: red;
top: 50%;
left: 50%;
transform:translate(-50%,-50%);
/*应用css3 translate属性是相对于自身的,此法>=ie9,且宽高自适应*/
}
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>
3.绝对定位 + 相对定位(推荐)
思想与上面的方式是一样,只是这是基于ie6,7的bug,相对定位位移父元素的50%
缺点:多添加一个容器标签
优点:自适应内容
兼容:ie6,7
<!DOCTYPE html>
<html>
<head>
<title></title>
<style type="text/css">
.middle-demo-4{
background-color: blue;
height: 300px;
width: 300px;
position: relative;
}
.middle-demo-4 div{
position: absolute;
top: 50%;
left: 50%;
}
.middle-demo-4 div div{
height: 200px;
background-color: red;
position: relative;
top: -50%;
left: -50%;
}/*ie6 ie7*/
</style>
</head>
<body>
<div class="middle-demo-4">
<div>
<div>dsfdsfdsfdsfdsfdsfdsf</div>
</div>
</div> </body>
</html>
4.margin:auto
绝对定位下,固定宽高,top:0,bottom:0,会自适应内容,多余部分会用margin填充
缺点:固定宽高
兼容:>=ie8
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
position: relative;
}
.div2{
margin: auto;
position: absolute;
background-color: red;
width: 200px;
height: 200px;
left: 0;
right: 0;
top: 0;
bottom: 0; }
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>
四、表格
1.table-cell(推荐)
单元格可以设置垂直属性 vertical-align: middle;
优点:自适应内容
兼容:>=ie8 +主流
缺点:子元素为浮动、绝对定位无效(注意)
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<style type="text/css">
.div1{
width: 500px;height: 500px;
background-color: blue;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.div2{
/*float: left;position: absolute; 子元素为浮动、绝对定位无效
此法>=ie8
*/}
</style>
<body>
<div class="div1">
<div class="div2">
fdsfsdffdf
fdsfdsfsdff
</div>
</div>
</body>
</html>
五、总结
根据兼容性和自适应内容来考虑 表格/行高法 + 相对定位法
如果固定宽高 负margin法
css 垂直水平居中总结的更多相关文章
- CSS垂直水平居中方法总结
在布局的时候经常能用到居中,在此总结一下 html结构: <div class="wrap"> <div class="content"> ...
- 关于css垂直水平居中的几种方式
css中元素的垂直水平居中是比较常见及较常使用的,在这里向大家介绍一下几种方式. 1.水平居中 margin: 0 auto; 效果图: 而文字的垂直水平居中也比较简单,加上line-height: ...
- css 垂直+水平居中
垂直+水平居中是一个老生常谈的问题了,现在就固定高度和不固定高度两种情况去讨论 1.父盒子固定高度[定位] 实现1: father-box: position:relative child-box:p ...
- CSS垂直水平居中方法整理
CSS定位中常常用到垂直居中,比如覆盖层上的弹框. 兼容性比较好的方法: <!DOCTYPE html PUBliC "-//W3C//DTD XHTML 1.0 Transition ...
- 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; 首先讨论一下单行时的情况. 毫无 ...
- 使用Sass优雅并高效的实现CSS中的垂直水平居中(附带Flex布局,CSS3+SASS完美版)
实现css水平垂直居中的方法有很多,在这里我简单的说下四种比较常用的方法: 1.使用CSS3中的Flex布局 对于flex,我们要了解的是它是一个display的属性,而且必须要给他的父元素设置fle ...
- div+css实现未知宽高元素垂直水平居中
div+css实现未知宽高元素垂直水平居中.很多同学在面试的时候都会遇到这样的问题:怎么用div+css的方法实现一个未知宽高的弹出框(或者图片)垂直水平居中??如果用JS的话就好办了,但是JS的使用 ...
随机推荐
- Matlab 高斯_拉普拉斯滤波器处理医学图像
前言:本程序是我去年实现论文算法时所做.主要功能为标记切割肝脏区域.时间有点久,很多细节已经模糊加上代码做了很多注释,因此在博客中不再详述. NOTE: 程序分几大段功能模块,仔细阅读,对解决医学图像 ...
- 【接口开发】浅谈 SOAP Webserver 与 Restful Webserver 区别
接口,强大,简单,交互,跨越平台 下面简单阐述这两大接口思想 一 REST: REST是一种架构风格,其核心是面向资源,REST专门针对网络应用设计和开发方式,以降低开发的复杂性,提高系统的可伸缩性. ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- 看图理解JWT如何用于单点登录
单点登录是我比较喜欢的一个技术解决方案,一方面他能够提高产品使用的便利性,另一方面他分离了各个应用都需要的登录服务,对性能以及工作量都有好处.自从上次研究过JWT如何应用于会话管理,加之以前的项目中也 ...
- ABP项目中使用Swagger生成动态WebAPI
本文是根据角落的白板报的<使用ABP实现SwaggerUI,生成动态webapi>一文的学习总结,感谢原文作者角落的白板报. 1 安装Swashbuckle.core 1.1 选择WebA ...
- “老坛泡新菜”:SOD MVVM框架,让WinForms焕发新春
火热的MVVM框架 最近几年最热门的技术之一就是前端技术了,各种前端框架,前端标准和前端设计风格层出不穷,而在众多前端框架中具有MVC,MVVM功能的框架成为耀眼新星,比如GitHub关注度很高的Vu ...
- 玩转ajax
1.什么是ajax? Ajax 是 Asynchronous JavaScript and XML(以及 DHTML 等)的缩写. 2.ajax需要什么基础? HTML 用于建立 Web 表单并确定应 ...
- Android Studio 编译单个module
前期自己要把gradle环境变量配置好 在Terminal中gradle命令行编译apk 输入gradle assembleRelease 会编译全部module编译单个modulecd ./xiru ...
- Git使用详细教程(二)
分支 其实在项目clone下来后就有一个分支,叫做master分支.新建分支的步骤:右键项目→Git→Repository...→Branches... master分支应该是最稳定的,开发的时候,建 ...
- Oracle常用SQL函数整理
--返回ASCII码select ASCII('A') "A的ASCII码" ,ASCII('a') "a的ASSCII码" from dual ; --反向 ...