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

行内元素
  • 和其他元素都在同一行
  • 高,行高及外边距和内边距部分可以改变
  • 宽度只与内容有关
  • 行内元素只能容纳文本或者其他行内元素
  • 常用内联元素: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. 算法(1):查找&排序

    算法(Algorithm):一个计算过程,解决问题的方法 程序 = 数据结构+算法 时间复杂度: 当算法过程中出现循环折半的时候,复杂度式子中会出现 O(logn) 时间复杂度小结: 1. 时间复杂度 ...

  2. TCP/IP学习笔记(4)------ICMP,ping,traceroute

    IMCP协议介绍 当传送IP数据包发生错误--比如主机不可达,路由不可达等等,ICMP协议将会把错误信息封包,然后传送回给主机.给主机一个处理错误的机会,这 也就是为什么说建立在IP层以上的协议是可能 ...

  3. Validate Binary Search Tree(DFS)

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  4. IDUtil 永不重复的ID

    package com.xxx.common.util; import java.util.Random; /** * 各种id生成策略 * * @version 1.0 */ public clas ...

  5. Oracle SqlPlus导出查询结果

    Oracle SqlPlus导出查询结果 在sqlplus下导出查询的结果保存到本地sql文件中,可以采用如下方式:1.连接数据库: sqlplus xmq/xmqpwd@192.168.1.57:1 ...

  6. Android GIS开发系列-- 入门季(13)Gdal简单写个shp文件

    Gdal是用来读写栅格与矢量数据的,在Gdal官网,可以下载相关的资源进行平台的编译.其实Arcgis底层也是用Gdal来读取shp文件的,那在Android中可以直接读写shp文件吗,是可以的.这里 ...

  7. mysql 排序order by可以根据权重,进行表达式计算。再排序

    1.select * from tbl_actor order by (follower_count+Recommend_weight)*weight_ratio desc limit 3; 2.or ...

  8. java.util.Scanner

    java.util.Scanner是Java5的新特征,主要功能是简化文本扫描.这个类最实用的地方表现在获取控制台输入,其他的功能都很鸡肋,尽管Java API文档中列举了大量的API方法,但是都不怎 ...

  9. pycharm支持react

    安装nodejs插件 使能node 出现下面的变化,在scope里可以定义使用的范围 创建react项目 使能eslint规则检查功能 配置前端启动脚本: https://www.jetbrains. ...

  10. laravel5.4新特性

    http://www.cnblogs.com/webskill/category/1067140.html laravel 5.4 新特性 component and slot 使用: 1.compo ...