css 水平、垂直居中
水平居中
行内元素
行内元素:(img、span、文字等行内元素),通过在父级元素设置 text-align:center 使元素水平居中。
块级元素
块级元素:(div、p、h1...h6、ul、li等块级元素),通过在要居中元素设置 margin:0 auto(上、右、下、左),这里表示上下0 左右自适应,达到元素水平居中。
垂直居中
行内元素
行内元素:(img、span、文字等行内元素),通过在父级元素设置display: table-cell;vertical-align: middle; 使元素垂直居中,如果是单行文字或者其他 可以设置line-height:容器高;
块级元素
高度固定通常是使用
- 绝对定位与负边距
- 绝对定位与margin
- display table-cell
高度不固定
- display table-cell
- translate
- flex 布局
还是上代码比较实在
行内元素水平、垂直居中
文字、图片水平、垂直居中
主要是利用
- text-align: center; //水平对齐方式
- display: table-cell;//以td形式渲染
- vertical-align: middle;//垂直对齐方式
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<style>
* {
padding: 0;
margin: 10px 0 0 0;
} div {
width: 200px;
height: 200px;
border: solid 1px red;
} img {
width: 100px;
height: 100px;
border: 0px;
} span {
border: solid 1px blue
} .div1 {
text-align: center;
display: table-cell;
vertical-align: middle;
} .div2 {
line-height: 200px;
text-align: center;
margin: 0 auto;
}
</style>
<body> <p>文字</p>
<div class="div1" title="行内元素水平垂直居中">
文字水平垂直居中1
</div> <p>文字</p>
<div class="div2" title="行内元素水平垂直居中">
文字水平垂直居中2
</div> <p>图片水平垂直居中</p>
<div class="div1" title="行内元素水平垂直居中">
<img src="data:images/pro_1.jpg">
</div> <p>span水平垂直居中</p>
<div class="div1" title="行内元素水平垂直居中">
<span>this is span</span>
</div>
</body>
</html>

块级元素水平、垂直居中
固定高度水平、垂直居中
固定高度水平垂直居中一般思路:


绝对定位与负边距
脱离文档流之后,在通过设置负的边距来达到水平、垂直居中效果;
<style>
* {
padding: 0;
margin: 0 0 0 0;
box-sizing: border-box;
} p{margin:20px 0 0 20px;} /*绝对定位与负边距实现*/
.divBox { height: 200px;
border: solid 1px red;
position: relative;
}
.divContentBox{
width: 100px;
height: 100px;
border: solid 1px blue;
text-align: center;
position: absolute;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px; } </style> <p>绝对定位与负边距实现</p>
<div class="divBox">
<div class="divContentBox">固定宽度、高度水平垂直居中</div>
</div>
如果不能脱离文档流就增加一个空的容器
<style>
/*增加空容器与margin */ #divBox{
width: 200px;
height: 200px;
border: solid 1px red; }
#nullBox{
width: 100%;
height: 50%;
background-color: blue;
}
#divContentBox {
width: 100px;
height: 100px;
margin: -50px auto;
border: solid 1px red; } </style> <p>增加一个空的容器</p>
<div id="divBox">
<div id="nullBox"></div>
<div id="divContentBox">固定宽度、高度水平垂直居中</div>
</div>
利用绝对定位与margin
<style>
#div1Box{
/*width: 200px;*/
height: 200px;
border: solid 1px red;
position: relative;
}
#ContentBox{ height: 100px;
display: inline;
border: solid 1px blue;
position: absolute;
margin: auto;
left: 0;
right: 0;
top: 0;
bottom: 0;
}
</style> <p>绝对定位与margin</p>
<div id="div1Box">
<div id="ContentBox">固定高度水平垂直居中</div>
</div>
css3 transform
<style>
#container{
height: 300px;
position:relative;
border: solid 1px red;
}
#center{
position: absolute;
width:100px;
height:100px;
border: solid 1px red;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
<p>css3 transform</p>
<div id="container">
<div id="center">固定高度水平垂直居中</div>
</div>
display table-cell
<style>
#container1{
width: 600px;
text-align: center;
vertical-align: middle;
border: solid 1px red;
display: table-cell;
height: 300px;
}
#center2{
width: 100px;
height: 100px;
border: solid 1px red;
display: inline-block;
}
</style>
<p>display table-cell</p>
<div id="container1">
<div id="center2">>固定高度水平垂直居中</div>
</div>
高度不固定水平、垂直居中
display:table-cell
父元素设置 display: table-cell;
text-align: center;
vertical-align: middle; 可以达到垂直居中;
子元素设置margin: 0 auto;可以达到水平居中;
translate
子元素脱离稳档流
top、left 偏移 50%
transform: translate(-50%, -50%);返回自身负的50%
flex布局
子元素设置水平、垂直对齐方式justify-content: center;align-items: center;
完整代码:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style>
/*-- display table-cell */ #root {
width: 100%;
display: table;
} .bigBox {
width: 100%;
height: 200px;
border: solid 1px red;
display: table-cell;
text-align: center;
vertical-align: middle;
} .content {
width: 300px;
background-color: green;
margin: 0 auto;
} /*translate */ .parent {
width: 100%;
height: 200px;
border: 1px solid red;
position: relative;
} .child {
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
background: #22B14C;
} /*flex*/
.parent1 {
width: 100%;
height: 200px;
border: solid 1px red;
display: flex;
justify-content: center;
align-items: center;
} .child1 {
background: green;
}
</style>
</head>
<body> <div id="root"> <div class="bigBox">
<div class="content">
<span>this is span</span><br/> <p>这是一个新的开始</p> <div>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
</div>
</div>
</div>
</div> <div class="parent">
<div class="child">
<p>this is demo2</p>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</div>
</div> <div class="parent1">
<div class="child1">
<p>this is demo3</p> <p>this is demo3</p> <p>this is demo3</p>
<li>a</li>
<li>b</li>
<li>c</li>
<li>d</li>
</div>
</div> </body>
</html>
css 水平、垂直居中的更多相关文章
- CSS水平垂直居中总结
行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...
- css水平垂直居中对齐方式
1.文字或者内联元素的垂直水平居中对齐 css属性 -- 水平居中:text-aligin:center; 垂直居中: line-height:height; 例子:. html: <div c ...
- 把简单做好也不简单-css水平垂直居中
44年前我们把人送上月球,但在CSS中我们仍然不能很好实现水平垂直居中. 作者:Icarus 原文链接:http://xdlrt.github.io/2016/12/15/2016-12-15 水平垂 ...
- CSS水平垂直居中的方法
原文链接:http://caibaojian.com/370.html 水平垂直居中,特别是使用在列表的时候经常会用到的,以前有需求的时候我也做过类似的代码,是使用display:table-cell ...
- css 水平垂直居中总结
空闲总结了下水平垂直居中方案,欢迎补充: 水平居中 水平居中有两种情况: 子元素是内联元素 这种那个情况下只需要在父元素定义: text-align:center; 例子: html: //省略了bo ...
- CSS水平垂直居中的几种方法2
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
- CSS水平垂直居中的几种方法
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
- 常见的几种 CSS 水平垂直居中解决办法
用CSS实现元素的水平居中,比较简单,可以设置text-align center,或者设置 margin-left:auto; margin-right:auto 之类的即可. 主要麻烦的地方还是在垂 ...
- 介绍一种css水平垂直居中的方法(非常好用!)
这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...
- css水平垂直居中
margin法(水平居中) 需要满足三个条件: 元素定宽 元素为块级元素或行内元素设置display:block 元素的margin-left和margin-right都必须设置为auto 三个条件缺 ...
随机推荐
- BZOJ 1264 基因匹配(DP+线段树)
很有意思的一道题啊. 求两个序列的最大公共子序列.保证每个序列中含有1-n各5个. 如果直接LCS显然是TLE的.该题与普通的LCS不同的是每个序列中含有1-n各5个. 考虑LCS的经典DP方程.dp ...
- bzoj4484[JSOI2015]最小表示
题意 给出一张DAG,要求删除尽量多的边使得连通性不变.(即:若删边前u到v有路径,则删边后仍有路径).点数30000,边数100000. 分析 如果从u到v有(u,v)这条边,且从u到v只有这一条路 ...
- 【bzoj4487】[Jsoi2015]染色问题 容斥原理
题目描述 棋盘是一个n×m的矩形,分成n行m列共n*m个小方格.现在萌萌和南南有C种不同颜色的颜料,他们希望把棋盘用这些颜料染色,并满足以下规定: 1. 棋盘的每一个小方格既可以染色(染成C种颜色中 ...
- 【bzoj3203】[Sdoi2013]保护出题人 凸包+二分
题目描述 输入 第一行两个空格隔开的正整数n和d,分别表示关数和相邻僵尸间的距离.接下来n行每行两个空格隔开的正整数,第i + 1行为Ai和 Xi,分别表示相比上一关在僵尸队列排头增加血量为Ai 点的 ...
- JVM堆内存控制/分代垃圾回收
JVM的堆的内存, 是通过下面面两个参数控制的 -Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后 ...
- 前端基础----html初识、常用标签
一.HTML初识 web服务本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) ...
- 【题解】【LibreOJ Round #6】花团 LOJ 534 时间线段树分治 背包
Prelude 题目链接:萌萌哒传送门(/≧▽≦)/ Solution 如果完全离线的话,可以直接用时间线段树分治来做,复杂度\(O(qv \log q)\). 现在在线了怎么办呢? 这其实是个假在线 ...
- 手脱tElock 0.98b1 -> tE!
声明: 只为纪录自己的脱壳历程,高手勿喷 第一种:两次内存法 注: ①这是在win7x32系统上运行的脱壳,所以可能地址不同 ②修复的时候用等级三修复,最后修复不了的剪切掉然后转存合一正常运行,已测试 ...
- centos中设置swap交换空间的大小设置和swappiness的比例设置
首先使用free -m命令查看内存使用情况和swap的大小 关闭swap: 设置swap的大小: bs指的是Block Size,就是每一块的大小.这里的例子是1M,意思就是count的数字,是以1M ...
- PlantUML —— 应用于 Eclipse 的简单快速的 UML 编辑软件
PlantUML —— 应用于 Eclipse 的简单快速的 UML 编辑软件 简介: 在应用系统软件开发过程中,如果软件由很多对象组成,它的结构仅仅凭借分析很难理清,同时为了有利于软件的开发及重用, ...