一.水平居中

(1)行内元素解决方案:父为块元素+text-align: center

只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可:

使用text-align: center;居中

对于行内元素或具有inline-block属性的元素居中,比如span、img等可以使用text-align: center;来实现。

<style type="text/css">
div.parent{
border: 1px solid red;
text-align: center;
} div span {
width: 100px;
background: #ddd;
}
</style>
----------------------------------- <div class="parent">
<span>行内元素居中</span>
<span>行内元素居中</span>
<span>行内元素居中</span>
</div>

(2)单个块状元素解决方案

父层元素使用text-align: center居中,子层元素使用 margin: 10px auto; 10px可改变,以设置顶端外边距;

<style type="text/css">
div.parent {
border: 1px solid red;
text-align: center;
} div p{
width: 100px;
background: #ddd;
/* 这里可以设置顶端外边距 */
margin: 10px auto;
}
</style> -----
<div class="parent">
<p>块元素居中</p>
</div>

(3)多个块状元素解决方案

将元素的display属性设置为inline-block,并且把父元素设置text-align:center即可:

<style type="text/css">
div.parent {
border: 1px solid red;
text-align: center;
} div p{
display: inline-block;
width: 100px;
background: #ddd;
}
</style> <div class="parent">
<p>块元素居中</p>
<p>块元素居中</p>
<p>块元素居中</p>
</div>

(4)多个块状元素解决方案 (使用flexbox布局实现)

使用flexbox布局,只需要把待处理的块状元素的父元素添加属性display:flex及justify-content:center即可:

   <style>
div {
border: 1px solid red;
width: 200px;
height: 200px; /*flex*/
display: flex;
justify-content: center;/*水平居中*/
align-items: center;/*垂直居中*/
}
div span{ background: #808080; }
</style> <div>
<span>我是span元素</span>
</div>

二、垂直居中

(1)单行的行内元素解决方案 :行内元素的height和line-height设置的和父元素一样高度即可实现垂直居中

div.parent {
background: #222;
height: 200px;
} /* 以下代码中,将a元素的height和line-height设置的和父元素一样高度即可实现垂直居中 */ a {
height: 200px;
line-height:200px;
color: #FFF;
} -------------------- <div class="parent">
<a>我是a元素</a>
</div>

(2)多行的行内元素解决方案

组合使用display:table-cell和vertical-align:middle属性来定义需要居中的元素的父容器元素生成效果,如下:

<style type="text/css">
div.center-aligned {
border:1px solid red;
width: 200px;
height: 200px;
display: table;/*父容器使用display:table*/
}
div.center-core{
display: table-cell;/*包裹行内元素容器使用display:table-cell*/
text-align: center;/*水平居中*/
vertical-align: middle;/*垂直居中*/
}
span{ background: #ddd;
}
</style> -----------
<div class="center-aligned">
<div class="center-core">
<span>我是span元素</span>
<span>我是span元素</span>
</div>
</div>

(3)已知高度的块状元素解决方案:负margin和定位配合

好处是行内元素和块级元素都适用,但是需要知道元素本身的宽高度。

  <style>
div {
border: 1px solid red;
width: 200px;
height: 200px;
text-align: center;
position: relative;
} div span {
width: 150px;
height: 50px;
background: #808080;
position: absolute;
margin: auto;
left: 50%;
top: 50%;
margin-left: -75px;/*-自身width/2*/
margin-top: -25px;/*-自身height/2*/
}
</style> ------------ <div>
<span>我是span元素</span>
</div>

三.水平垂直居中

(1)已知高度和宽度的元素解决方案1:margin-auto和定位的组合使用

这是一种不常见的居中方法,可自适应,比方案2更智能,如下:

<style>
div {
border: 1px solid red;
width: 200px;
height: 200px;
position: relative;
} div span {
width: 150px;
height: 50px;
background: #808080;
position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
</style> --------------- <div>
<span>我是span元素</span>
</div>

(2)已知高度和宽度的元素解决方案2:负margin和定位配合

position 元素已知宽度 父元素设置为:position: relative; 子元素设置为:position: absolute; 距上50%,据左50% ;margin-left: -自身width/2margin-top: -自身height/2

<style>
div {
border: 1px solid red;
width: 200px;
height: 200px;
text-align: center;
position: relative;
} div span {
width: 150px;
height: 50px;
background: #808080;
position: absolute;
margin: auto;
left: 50%;
top: 50%;
margin-left: -75px;/*-自身width/2*/
margin-top: -25px;/*-自身height/2*/
}
</style> ------------ <div>
<span>我是span元素</span>
</div>

(3)未知高度和宽度元素解决方案:使用translate居中

这种方法实现原理和负margin和定位的组合使用是一样的, 就是用css3的属性translate来达到和负margin一样的作用。translate是transform的一个值,在这里作用是定义2D转换。但是在IE9以下不支持

<style type="text/css">
div {
border: 1px solid red;
width: 200px;
height: 200px;
position: relative;
} div span {
width: 150px;
height: 50px;
background: #ddd;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
</style> ---------------
<div>
<span>行内元素居中</span>
</div>

(4)使用flex布局实现:使用flex居中不需要知道元素本身宽高以及元素的属性

<style>
div {
border: 1px solid red;
width: 200px;
/* 注意这里需要设置高度来查看垂直居中效果 */
height: 200px; /*flex*/
display: flex;
justify-content: center;/*水平居中*/
align-items: center;/*垂直居中*/
}
div span{ background: #808080; }
</style> -----
<div>
<span>我是span元素</span>
</div>

(5)calc和定位的组合使用

calc是英文单词calculate(计算)的缩写,是css3的一个新增的功能,可以使用calc()给元素的border、margin、pading、font-size和width等属性设置动态值。calc使元素居中的原理和负margin是一样的,calc 允许基于当前的页面布局计算尺寸。

计算公式为:

top: calc(50% - (自身高度height/ 2));

left: calc(50% - (自身高度宽度 / 2));

<style type="text/css">
div {
border: 1px solid red;
width: 200px;
height: 200px;
position: relative;
} div span {
width: 150px;
height: 50px;
background: #ddd;
position: absolute;
top: calc(50% - (50px / 2));
left: calc(50% - (150px / 2));
</style> -- <div>
<span>行内元素居中</span>
</div>

用CSS/CSS3 实现水平居中和垂直居中,水平垂直居中的方式的更多相关文章

  1. 用CSS/CSS3 实现 水平居中和垂直居中的完整攻略

    水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可:   .parent { text-align:center; } 水 ...

  2. 用CSS 实现 非浮动元素的 水平居中/垂直居中/水平垂直居中

    一.水平居中 (1)行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可:   .parent { text-align:center ...

  3. CSS3中flexbox如何实现水平垂直居中和三列等高布局

    最近这些天都在弥补css以及css3的基础知识,在打开网页的时候,发现了火狐默认首页上有这样一个东西.

  4. CSS实现文字和图片的水平垂直居中

    关于文字和图片的水平垂直居中,在前端界绝对算是一个老生常谈的问题了,尤其是垂直居中,什么千奇百怪的解法都能想的出来.下面我就总结一些比较常用的方法: 一.文本的水平垂直居中: 1.水平居中: 是不是很 ...

  5. css — 定位、背景图、水平垂直居中

    目录 1. 定位 2. 背景图 3. 水平垂直居中 1. 定位 position:static | relative | absolute | fixed; static 静态定位 relative ...

  6. 【css】常用的几种水平垂直居中方式与盒子模型,面试经常问到!

    div水平垂直居中 假设结构为此,2个div嵌套 <div class="box"> <div class="content">< ...

  7. CSS之常见布局|常用单位|水平垂直居中

    常见布局: 1. 流式布局:百分比布局,宽高.margin.pinding都是百分比 2. 固定布局:盒子的宽高固定,如:margin.padding等 3. 浮动布局:float 4. 弹性布局:f ...

  8. CSS 实现盒子水平居中、垂直居中和水平垂直居中的方法

     CSS 实现盒子模型水平居中 水平居中效果图如下: HTML: CSS 全局样式: 方法一:使用margin: 0 auto;(只适用于子盒子有宽的时候) 方法二:text-align + disp ...

  9. CSS布局之-水平垂直居中

    对一个元素水平垂直居中,在我们的工作中是会经常遇到的,也是CSS布局中很重要的一部分,本文就来讲讲CSS水平垂直居中的一些方法.另外,文中的css都是用less书写的,如果看不懂less,可以把我给的 ...

随机推荐

  1. The parameter to the method is the basic data type

    package method.invocation; public class TheParameterToTheMethodIsTheBasicDataType { public static vo ...

  2. 使用NodeJs搭建的小型web应用

    原文英文链接:http://www.nodebeginner.org 中文翻译链接:http://www.nodebeginner.org/index-zh-cn.html 学习链接:一本全面的Nod ...

  3. 搭建高可用mongodb集群(二)—— 副本集

    在上一篇文章<搭建高可用MongoDB集群(一)--配置MongoDB> 提到了几个问题还没有解决. 主节点挂了能否自动切换连接?目前需要手工切换. 主节点的读写压力过大如何解决? 从节点 ...

  4. DHCP服务搭建

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP ...

  5. google学习

    https://developers.google.com/machine-learning/crash-course/ https://developers.google.com/machine-l ...

  6. cocos2dx中node的pause函数(lua)

    time:2015/05/14 描述 lua下使用node的pause函数想暂停layer上的所有动画,结果没有效果 1. pause函数 (1)cc.Node:pause 代码: void Node ...

  7. css动画 文字闪烁效果

    /*定义页面基础CSS*/ body{ font-family: 'microsoft yahei',Arial,sans-serif; color: #EFEFEF; background: #22 ...

  8. 【2】【MOOC】Python游戏开发入门-北京理工大学【第三部分-游戏开发之机制(屏幕绘制机制)】

    学习地址链接:http://www.icourse163.org/course/0809BIT021E-1001873001?utm_campaign=share&utm_medium=and ...

  9. BZOJ2004:[HNOI2010]Bus 公交线路(状压DP,矩阵乘法)

    Description 小Z所在的城市有N个公交车站,排列在一条长(N-1)km的直线上,从左到右依次编号为1到N,相邻公交车站间的距离均为1km. 作为公交车线路的规划者,小Z调查了市民的需求,决定 ...

  10. programming-languages学习笔记--第4部分

    programming-languages学习笔记–第4部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} program ...