用CSS/CSS3 实现水平居中和垂直居中,水平垂直居中的方式
一.水平居中
(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 实现水平居中和垂直居中,水平垂直居中的方式的更多相关文章
- 用CSS/CSS3 实现 水平居中和垂直居中的完整攻略
水平居中:行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: .parent { text-align:center; } 水 ...
- 用CSS 实现 非浮动元素的 水平居中/垂直居中/水平垂直居中
一.水平居中 (1)行内元素解决方案 只需要把行内元素包裹在一个属性display为block的父层元素中,并且把父层元素添加如下属性即可: .parent { text-align:center ...
- CSS3中flexbox如何实现水平垂直居中和三列等高布局
最近这些天都在弥补css以及css3的基础知识,在打开网页的时候,发现了火狐默认首页上有这样一个东西.
- CSS实现文字和图片的水平垂直居中
关于文字和图片的水平垂直居中,在前端界绝对算是一个老生常谈的问题了,尤其是垂直居中,什么千奇百怪的解法都能想的出来.下面我就总结一些比较常用的方法: 一.文本的水平垂直居中: 1.水平居中: 是不是很 ...
- css — 定位、背景图、水平垂直居中
目录 1. 定位 2. 背景图 3. 水平垂直居中 1. 定位 position:static | relative | absolute | fixed; static 静态定位 relative ...
- 【css】常用的几种水平垂直居中方式与盒子模型,面试经常问到!
div水平垂直居中 假设结构为此,2个div嵌套 <div class="box"> <div class="content">< ...
- CSS之常见布局|常用单位|水平垂直居中
常见布局: 1. 流式布局:百分比布局,宽高.margin.pinding都是百分比 2. 固定布局:盒子的宽高固定,如:margin.padding等 3. 浮动布局:float 4. 弹性布局:f ...
- CSS 实现盒子水平居中、垂直居中和水平垂直居中的方法
CSS 实现盒子模型水平居中 水平居中效果图如下: HTML: CSS 全局样式: 方法一:使用margin: 0 auto;(只适用于子盒子有宽的时候) 方法二:text-align + disp ...
- CSS布局之-水平垂直居中
对一个元素水平垂直居中,在我们的工作中是会经常遇到的,也是CSS布局中很重要的一部分,本文就来讲讲CSS水平垂直居中的一些方法.另外,文中的css都是用less书写的,如果看不懂less,可以把我给的 ...
随机推荐
- qss 对子控件的设置样式 使用setProperty --Qt 之 QSS(动态属性)
https://blog.csdn.net/liang19890820/article/details/51693956 学习了 代码: 当鼠标划过控件时,设置样式 void CustomLabelW ...
- Python学习---面向对象的学习[深入]
类的深入学习 a. Python中一切事物都是对象 b. class Foo: pass obj = Foo() # ...
- c#listbox使用详解和常见问题解决
关于ListBox ListBox是WinForm中的 列表 控件,它提供了一个项目列表(一组数据项),用户可以选择一个或者多个条目,当列表项目过多时,ListBox会自动添加滚动条,使用户可以滚动查 ...
- 微软最新设计Fluent Design System初体验
微软最新设计Fluent Design System初体验 本文图片不全!建议移步知乎专栏查看!!! https://zhuanlan.zhihu.com/p/30582886 原创 2017-11- ...
- [T-ARA][Day by Day]
歌词来源:http://music.163.com/#/song?id=22704409 作曲 : 金泰贤/赵英秀 [作曲 : 金泰贤/赵英秀] [作曲 : 金泰贤/赵英秀] 作词 : 金泰贤/赵英秀 ...
- 对volatile不具有原子性的理解
在阅读多线程书籍的时候,对volatile的原子性产生了疑问,问题类似于这篇文章所阐述的那样.经过一番思考给出自己的理解. 我们知道对于可见性,Java提供了volatile关键字来保证可见性.有序性 ...
- web自动化_浏览器驱动chromedriver安装方法(适用RF框架/Selenium/Appium)
在进行UI自动化时,打开浏览器是第一步,这就必须要安装浏览器的驱动,chrome浏览器需要安装chromedriver,下载地址:http://chromedriver.storage.googlea ...
- Linux CPU的中断【转载】
中断其实就是由硬件或软件所发送的一种称为IRQ(中断请求)的信号. 中断允许让设备,如键盘,串口卡,并口等设备表明它们需要CPU. 一旦CPU接收了中断请求,CPU就会暂时停止执行正在运行的程序,并且 ...
- (九)Linux查看用户登录的命令
用户登录查看命令 w 含义:就这么简单,一个字母w就是一个命令.查看登录用户信息. 输出的结果的含义: USER 登录的用户名 TTY 登录终端 FROM 从哪个I ...
- TensorFlow函数(三)tf.variable_scope() 和 tf.name_scope()
tf.name_scope() 此函数作用是共享变量.在一个作用域scope内共享一些变量,简单来说,就是给变量名前面加个变量空间名,只限于tf.Variable()的变量 tf.variable_s ...