简介

总结记录一下经常需要用到垂直居中布局,欢迎补充(空手套。。。O(∩_∩)O)

以下栗子如果未特别标注同一使用这样的html结构

<div class="container">
<div class="content"></div>
</div>

垂直居中布局

利用绝对定位和负margin

绝对定位可以很容易做到top:50%,现在只要再让目标元素上移自身高度的一半就垂直居中了

.container {
background: #777777;
height: 400px;
position: relative;
} .container .content {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
margin-top: -50px;
left: 50%;
margin-left: -50px;
background: #ee5f28;
}

优点:兼容性好
缺点:需要知道居中元素的高度

利用绝对定位和transform

 .container {
background: #777777;
height: 400px;
position: relative;
} .container .content {
width: 100px;
height: 100px;
position: absolute;
top: 50%;
left: 50%;
transform: translate3d(-50%, -50%, 0);
background: #ee5f28;
}
  • 优点:不需要考虑content元素的高度
  • 缺点:兼容性

利用绝对定位和calc

.container {
background: #777777;
height: 400px;
position: relative;
} .container .content {
width: 100px;
height: 100px;
position: absolute;
top: calc(50% - 50px);
left: calc(50% - 50px);
background: #ee5f28;
}
  • 优点:相比于前面少了两条样式
  • 缺点:兼容性

利用flex

.container {
background: #777777;
height: 400px;
display: flex;
justify-content: center;
align-items: center;
} .container .content {
width: 100px;
height: 100px;
background: #ee5f28;
}
.container {
background: #777777;
height: 400px;
display: flex;
} .container .content {
width: 100px;
height: 100px;
background: #ee5f28;
margin: auto;
}
  • 优点:垂直居中特别容易搞定
  • 缺点:兼容性

震惊absoulute(绝对定位)还可以这样用

.container {
background: #777777;
height: 400px;
position: relative;
} .container .content {
width: 100px;
height: 100px;
background: #ee5f28;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}

优点:

  • 1.跨浏览器,兼容性好(无需hack,可兼顾IE8~IE10);
  • 2.无特殊标记,样式更精简;
  • 3.自适应布局,可以使用百分比和最大最小高宽等样式;
  • 4.居中时不考虑元素的padding值(也不需要使用box-sizing样式);
  • 5.布局块可以自由调节大小;6.img的图像也可以使用
  • 6.浏览器支持:Chrome、Firefox、Safari、Mobile Safari、IE8-10。 “完全居中”经测试可以完美地应用在最新版本的Chrome、Firefox、Safari、Mobile Safari中,甚至也可以运行在IE8~IE10上

使用inline-block

.container {
background: #777777;
height: 400px;
text-align: center;
font-size: 0;
overflow: auto;
} .container::after {
content: '';
display: inline-block;
height: 100%;
vertical-align: middle;
} .container .content {
display: inline-block;
vertical-align: middle;
width: 100px;
height: 100px;
background: #ee5f28;
}

这里注意:容器‘container’里要设置font-size:0;避免inline-block之间产生间隔

优点:

  • 内容高度可变
  • 内容溢出则能自动撑开父元素高度
  • 浏览器兼容性好,甚至可以调整支持IE7

使用table与table-cell

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
.container-table {
background: #777777;
height: 400px;
width: 100%;
display: table;
} .container-table .container-cell {
display: table-cell;
vertical-align: middle;/* 这里达到了垂直居中的效果 */
} .container-table .container-cell .content {
width: 100px;
height: 100px;
margin: 0 auto;/* 利用margin值 水平居中*/
background: #ee5f28;
}
</style>
</head> <body>
<div class="container-table">
<div class="container-cell">
<div class="content"></div>
</div>
</div>
</body> </html>
  • 优点:

    • 内容高度可变
    • 内容溢出则能自动撑开父元素高度
    • 浏览器兼容性好
  • 缺点:额外标签

参考资料

css垂直居中布局总结的更多相关文章

  1. css布局 - 垂直居中布局的一百种实现方式(更新中...)

    首先将垂直居中的现象和实现方式两大方向细分类如下: 接下来逐条累加不同情况下的垂直居中实现. 目录: 一.父元素高度固定时,单行文本 | 图片的垂直居中 1. line-height行高简单粗暴实现法 ...

  2. CSS里总算是有了一种简单的垂直居中布局的方法了

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  3. CSS 实现:父元素包含子元素,子元素垂直居中布局

    ☊[实现要求]:父元素包含子元素,子元素垂直居中布局 <div class="demo5"> <div class="child">A& ...

  4. CSS垂直居中总结

    工作中遇到垂直居中问题,特此总结了一下几种方式与大家分享.本文讨论的垂直居中仅支持IE8+ 1.使用绝对定位垂直居中 <div class="container"> & ...

  5. 原生 CSS 网格布局学习笔记

    下是来自Oliver Williams的帖子. Oliver已经学习了相当长时间的原生CSS网格,可以说是在CSS网格方面有一定的发言权.在这篇文章中,他将以非同寻常的思路分析自己的CSS网格布局学习 ...

  6. CSS垂直居中精华总结

    1  table-cell方式 将center元素的包含框display设置为table,center元素的display设置为table-cell,vertical-align设置为middle. ...

  7. CSS垂直居中指南

    大概整理下css中的垂直居中问题 主要分两大类 1 知道块元素尺寸,一般就是指知道元素高度. 方法一:最简单的,类比水平居中. 思路:子元素设置为absolute;  top bottom都设置为0 ...

  8. 总结与学习DIV+CSS网页布局技巧

    以前用表格布局时设置网页居中非常方便,把表格对齐方式设置为居中就行了,就这么简单,现在呢,用DIV+CSS样式表控制,好像不是那么容易了,其实也很简单,只不过方式不同而已. <style> ...

  9. 《CSS网站布局实录》读书笔记

    从Web标准.HTML标记.CSS语法基础介绍到实用技巧,事无巨细.实体书已不印刷,只能下载电子版 书的背景: 国内第一本web标准的CSS布局书,2006年9月第一版,作者李超. 环境背景: 当时主 ...

随机推荐

  1. 谁是嫌疑犯问题Python枚举法

    原文:https://blog.csdn.net/yunzifengqing/article/details/81941592 问题描述:有6名犯罪嫌疑人A.B.C.D.E.F,已知如下事实: A.B ...

  2. 爬虫时安装的newspaper 新闻包

    Newspaper3k: Article scraping & curation 首先在命令行安装 newspaper pip install Newspaper3k 然后导入包进行写代码 f ...

  3. 这不是javascript:什么?

    javascript协议.<a href=“javascript:void(0):”>xxx</a>基于事件的事件,例如:<input onblur=“check():” ...

  4. npm install 常用的几个参数

    npm install moduleName # 安装模块到项目目录下 npm install -g moduleName # -g 的意思是将模块安装到全局,具体安装到磁盘哪个位置,要看 npm c ...

  5. shell与其他语言不同点

    1.定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="w3cschool.cn" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语 ...

  6. centos7.3安装mysql5.7

    一.写随笔的原因:最近在阿里云上买了个centos7.3服务器,想将一些demo运行在上面,所以需要做一些环境的安装,通过此篇文章MAKR一下.下面来记录下安装步骤(参考网上的一些教程,有坑的话会实时 ...

  7. MySQL8.x msi版安装教程

    一.下载MySQL 官网下载地址 https://dev.mysql.com/downloads/windows/installer/8.0.html  下载第二个即可(虽然只有32位的 但是会同时安 ...

  8. 开源Android 恶意软件Radio Balouch

    安全研究机构 ESET 首次发现了开源 Android 间谍软件在 Google Play  上的恶意信息窃取行为,并且在被删除后仍在Google Play 重复出现.据悉,第一个间谍软件是基于开源间 ...

  9. 实操 | 内存占用减少高达90%,还不用升级硬件?没错,这篇文章教你妙用Pandas轻松处理大规模数据

    注:Pandas(Python Data Analysis Library) 是基于 NumPy 的一种工具,该工具是为了解决数据分析任务而创建的.此外,Pandas 纳入了大量库和一些标准的数据模型 ...

  10. 今日理解之js

    JavaScript 是前端的一门编程语言(也是有逻辑) node.js 支持前端js代码 跑在后端服务器上 Js跟Java什么关系? Js跟Java半毛钱关系都没有!!! 原因是当初Java特别火 ...