用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,可以把我给的 ...
随机推荐
- 【 PostgreSQL】工作中常用SQL语句干货
接触gp数据库近一年的时间,语法上和其他数据库还是有些许不同,工作中常用的操作语句分享给大家! -- 建表语句 create table ods.ods_b_bill_m ( acct_month t ...
- CALayer的additive属性解析
CALayer的additive属性解析 效果: 源码:https://github.com/RylanJIN/ShareOfCoreAnimation // // CAPartAViewContro ...
- hbase shell基础和常用命令详解
HBase是Google Bigtable的开源实现,它利用Hadoop HDFS作为其文件存储系统,利用Hadoop MapReduce来处理HBase中的海量数据,利用Zookeeper作为协同服 ...
- 021.1 IO流——File类
########################################IO流: IO:用于处理设备上的数据的技术.设备:内存,硬盘,光盘 流:系统资源,Windows系统本身就可 ...
- #npm install# MSBUILD : error MSB4132: 无法识别工具版本“2.0”。可用的工具版本为 "4.0"。
0.问题描述 Windows 10 最近使用npm install安装项目依赖包,当自动执行至node-gyp rebuild时报错: C:\Users\dsl\Desktop\Pros\ant-de ...
- angularJs的工具方法3
一.angular.version 判断angular的版本 console.log(angular.version); 二.angular.equals 判断两 ...
- BZOJ3997:[TJOI2015]组合数学(DP,Dilworth定理)
Description 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一 ...
- gluoncv 用已经训练好的模型参数,检测物体
当然这个模型参数,最好用自己的,否则不够精确,我自己的还没训练完. from matplotlib import pyplot as plt import gluoncv from gluoncv i ...
- 【[HNOI2015]亚瑟王】
神仙题,抄题解 用\(tp_i\)表示\(i\)这个技能在\(r\)轮中被使用过的概率 于是最后的答案就是\(\sum_{i=1}^nd_i*tp_i\) 首先\(tp_1=1-(1-p_1)^r\) ...
- burpsuit常用功能
1.生成GET数据包:复制url -> 打开burp -> repeater -> 右键paste url as request 2.生成POST数据包:生成一个GET数据包 -&g ...