在介绍居中方式之前,简单介绍一下行内元素块级元素

行内元素
  • 和其他元素都在同一行
  • 高,行高及外边距和内边距部分可以改变
  • 宽度只与内容有关
  • 行内元素只能容纳文本或者其他行内元素
  • 常用内联元素:a,img,input,lable,select,span,textarea,font
块级元素
  • 总是在新行上开始,占据一整行
  • 高度,行高以及外边距和内边距都可控制
  • 宽度始终与浏览器的宽度一样,与内容无关
  • 可以容纳行内元素和其他块级元素
  • 常用的块级元素:div,p,table,form,h1,h2,h3,h4,h5,h6,dl,ol,ul,li

居中方式分为三种:水平居中,垂直居中,水平垂直居中。

1、水平居中

1.1、行内元素水平居中

利用text-align: center可以实现在块级元素内部的行内元素水平居中。

此方法对行内元素(inline),行内块(inline-block),行内表(inline-table),inline-flex元素水平居中都有效。

<div class="center-text">
简单是稳定的前提。
</div>
div {
height:60px;
border: 2px dashed #f69c55;
}
.center-text {
text-align: center;
}

1.2、块级元素水平居中

通过把固定宽度的块级元素的margin-leftmargin-right设成auto,就可以使块级元素水平居中。

<div>
<p class="center-block">
简单不先于复杂,而是在复杂之后。
</p>
</div>
div {
height:100px;
border: 2px dashed #f69c55;
}
.center-block {
margin: 0 auto;
width: 8rem;
padding:1rem;
color:#fff;
background:#000;
}

1.3、多块级元素水平居中

利用inline-block

如果一行中有两个或两个以上的块级元素,通过设置块级元素的显示类型为inline-block和父容器的text-align属性从而使多块级元素水平居中。

<div class="container">
<div class="inline-block">
简单不先于复杂
</div>
<div class="inline-block">
而是在复杂之后
</div>
<div class="inline-block">
简单不先于复杂,而是在复杂之后。
</div>
</div>
.container {
height:100px;
padding: 8px;
text-align: center;
border: 2px dashed #f69c55;
}
.inline-block {
padding: 8px;
width: 4rem;
margin: 0 8px;
color: #fff;
background: #000;
display: inline-block;
}
.container >div:first-child {
height: 45px;
}
.container >div:nth-of-type(2) {
height: 45px;
}

利用displa: flex

利用弹性布局(flex),实现水平居中,其中justify-content用于设置弹性盒子元素在主轴(横向)方向上的对齐方式。

<div class="flex-center">
<div>
简单不先于复杂。
</div>
<div>
简单不先于复杂,而是在复杂之后。
</div>
<div>
而是在复杂之后。
</div>
</div>
.flex-center {
padding: 8px;
display: flex;
justify-content: center;
align-items:flex-end;
border: 2px dashed #f69c55;
}
.flex-center >div {
padding: 8px;
width: 4rem;
margin: 0 8px;
color: #fff;
background: #000;
}
.flex-center >div:first-child {
height: 45px;
}
.flex-center >div:last-child {
height: 45px;
}

  • flex-direction 决定主轴的方向(即项目的排列方向)
  • flex-wrap 如果一条轴线排列不下,如何换行
  • justify-content 定义了项目在主轴上的对齐方式
  • align-items 定义项目在交叉轴上如何对齐

Flex布局教程:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

2、垂直居中

2.1、单行行内元素垂直居中

通过设置内联元素的高度(height)和行高(line-height)相等,从而使元素垂直居中。

<div id="box">
软件在能够复用前必须先能用。
</div>
#box {
height: 120px;
line-height: 120px;
border: 2px dashed #f69c55;
}

2.2、多行元素垂直居中

利用表格布局

利用表布局的vertical-align: middle可以实现子元素的垂直居中 。

<div class="center-table">
<p class="v-cell">
The more technology you learn,<br>
the more you realize how little you know.
</p>
</div>
.center-table {
display: table;
height: 140px;
border: 2px dashed #f69c55;
}
.v-cell {
display: table-cell;
vertical-align: middle;
}

利用flex布局

利用flex布局实现垂直居中,其中flex-direction: column定义主轴方向为纵向。flex是在CSS3中定义,在较老的浏览器中存在兼容问题。

<div class="center-flex">
<p>
Dance like nobody is watching,<br>
code like everybody is.
</p>
</div>
.center-flex {
height: 140px;
display: flex;
flex-direction: column;
justify-content: center;
border: 2px dashed #f69c55;
}

2.3、块级元素垂直居中

固定高度的块级元素

已知居中元素的高度和宽度,垂直居中问题就很简单。通过绝对定位元素距离顶部50%,并设置margin-top向上偏移元素高度的一半,就可实现垂直居中。

<div class="parent">
<div class="child">控制复杂性是计算机编程的本质。</div>
</div>
.parent {
height: 140px;
position: relative;
border: 2px dashed #f69c55;
}
.child {
position: absolute;
top: 50%;
height: 100px;
margin-top: -50px;
color:#fff;
background: #000;
}

未知高度的块级元素

当垂直居中的块级元素高度未知时,可以借助CSS3中的transform属性向Y轴反向偏移50%的方法实现垂直居中,部分浏览器可能存在兼容性问题。

<div class="parent">
<div class="child">世界上有 10 种人,懂二进制的和不懂二进制的。</div>
</div>
.parent {
height: 140px;
position: relative;
border: 2px dashed #f69c55;
}
.child {
position: absolute;
top: 50%;
transform: translateY(-50%);
background: black;
color: #fff;
padding: 1rem;
width: 12rem;
}

3、水平垂直居中

3.1、固定宽高元素水平垂直居中

通过margin平移元素整体宽度的一半,使元素水平垂直居中。

<div class="parent">
<div class="child">控制复杂性是计算机编程的本质。</div>
</div>
.parent {
height: 140px;
position: relative;
border: 2px dashed #f69c55;
}
.child {
width: 200px;
height: 80px;
padding: 10px;
position: absolute;
top: 50%;
left: 50%;
margin: -50px 0 0 -110px;
background: black;
color: #fff;
}

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

利用2D变换,在水平和垂直方向都反向平移宽高的一半,从而使元素水平垂直居中。

<div class="parent">
<div class="child">当你试图解决一个你不理解的问题时,复杂化就产成了。当你试图解决一个你不理解的问题时,复杂化就产成了。</div>
</div>
.parent {
height: 140px;
position: relative;
border: 2px dashed #f69c55;
}
.child {
padding: 10px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
color: #fff;
background: black;
}

3.3、利用flex布局

利用flex布局,其中justify-content用于设置或检索弹性盒子元素在主轴上方向上的对齐方式;而align-items属性定义flex子项在flex容器的当前行的侧轴方向上的对齐方式。

<div class="parent">
<div class="child">Facebook wasn't built in a day.</div>
</div>
.parent {
height: 140px;
display: flex;
justify-content: center;
align-items: center;
border: 2px dashed #f69c55;
}
.child {
padding: 20px;
background: black;
color: #fff;
}

最后附赠一张居中总结的图

CSS实现居中的方式的更多相关文章

  1. 用CSS实现居中的方式

    直接放链接吧,最近大量时间放在看书上了,不想玩游戏,不想看电影,只想看书,早日做出自己的网站卖广告. CSS居中

  2. CSS中居中的完全指南(中英对照翻译)

    翻译自:https://css-tricks.com/centering-css-complete-guide/ Centering things in CSS is the poster child ...

  3. js、css引用文件的下载方式

    js.css引用文件的下载方式 一.测试(chrome):1.直接使用<script...>.<link...>标签来混合引入脚本文件和css文件, <script as ...

  4. 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码

    原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...

  5. 使用Flexbox实现CSS竖向居中

    竖向居中需要一个父元素和一个子元素合作完成. <div class="flexbox-container"> <div>Blah blah</div& ...

  6. CSS标签居中

    CSS标签居中是相对于父标签说的,即在父标签的中居中.通常是在子标签中使用margin:0 auto,来使子标签居中.此外子标签需要有固定的宽度才行,比如 子标签为div时,div的宽度默认占父标签的 ...

  7. css常用居中

    对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...

  8. CSS样式之连接方式

    前言:上一篇博客是HTML基本结构和标签,是笔者学习HTML的笔记,本篇博客开始记录CSS,废话不多说,直接进入主题. 首先,我们要知道CSS是什么.简单地说,CSS层叠样式表是用来表现HTML或XM ...

  9. 前端基础----CSS语法、CSS四种引入方式、CSS选择器、CSS属性操作

    一.CSS语法 CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明. 例如: h1 {color:red; font-size:14px;} 二.CSS四种引入方式 1,行内式 行内式是在标 ...

随机推荐

  1. BNUOJ 7178 病毒侵袭持续中

    病毒侵袭持续中 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 30 ...

  2. 全文搜索(A)-相关性

    文章:搜索相关性 文章:推荐系统中相似度算法介绍及效果测试 文章:常用的相似度计算方法原理及实现 文章:推荐系统用户相似度计算方法研究

  3. chrome webstore

    chrome webstore https://chrome.google.com/webstore/detail/set-character-encoding/bpojelgakakmcfmjfil ...

  4. Android 网络连接状态的监控

    有些应用需要连接网络,例如更新后台服务,刷新数据等,最通常的做法是定期联网,直接使用网上资源.缓存数据或执行一个下载任务来更新数据. 但是如果终端设备没有连接网络,或者网速较慢,就没必要执行这些任务. ...

  5. Hive之侧视图(Lateral View)

    Lateral View和UDTF类功能函数一起使用,表中的每一行和UDTF函数输出的每一行进行连接,生成一张新的虚拟表,可以对UDTF产生的记录设置字段名称,新加的字段可以使用在sort by,gr ...

  6. Kettle循环删除数据

    1.问题描述: 某个系统原库的数据同步到备份库.但是由于原库的的数据会物理删除,此时需要删除备份库的数据. 2.不理想的解决1: 1)首先从备份库获取该表的所有ID: 2)循环备份库的ID,去原库检测 ...

  7. anaconda是个啥?

    Anaconda具有跨平台(win/mac/linux).包管理(类似于pip).环境管理(类似于virtualenv)的特点,因此很适合快速在新的机器上部署Python环境 Anaconda利用工具 ...

  8. win7笔记本如何设置共享网络供手机WIFI上网?

    第一步 按WIN+R调出“运行”栏,在“运行”菜单栏输入“cmd”,出现命令提示符,输入命令“netsh wlan set hostednetwork mode=allow ssid=xiaoming ...

  9. 百度语音识别API初探

    近期想做个东西把大段对话转成文字.用语音输入法太慢,所以想到看有没有现成的API,网上一搜,基本就是百度和讯飞. 这里先看百度的 笔者使用的是Java版本号的 下载地址:http://bos.nj.b ...

  10. C#如何开发多语言支持的Winform程序

    C# Winform项目多语言实现(支持简/繁/英三种语言)有很多种方案实现多语言,我在这里介绍一种最简单最容易理解的,作为教学材题应该从通俗易懂入手.在写这篇文章之前,本来想用枚举窗体对象成员的方式 ...