css设置居中的方案总结
回想一下,自己平时项目里遇到的比较多的就是css如何让元素居中显示,其实差不多每种情况都遇到过,所采用的方法也都各有利弊,下面对这些方法来做个概括,对其中的坑点,也会一一指出来,希望能给遇到问题的同学一些参考:
一、水平居中
01 行内元素 text-align: center;
.parent {
text-align: center;
}
02 块级元素 margin: auto;
注意:低版本的浏览器还需要设置text-align:center;
.parent {
text-align: center;
}
.child {
margin: auto;
border: 1px solid blue;
}
二、垂直居中
01 行内元素(单行文字垂直居中):设置 line-height = height;
.parent {
height: 200px;
line-height: 200px;
border: 1px solid red;
}
02 块级元素:绝对定位(需要提前知道尺寸)
优点:兼容性不错
缺点:需要提前知道尺寸,可拓展性和自适应性不好
.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
background: blue;
position: absolute;
left: 50%;
top: 50%;
margin-top: -20px;
margin-left: -40px;
}
03 块级元素:绝对定位 + transform
优点:不需要提前知道尺寸
缺点:兼容性不好,只支持ie9+浏览器,而且还会引发一些其他的奇怪的兼容问题
.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
background: blue;
}
04 块级元素:绝对定位 + margin: auto;
优点:不需要提前知道尺寸,兼容性好
缺点:我目前还没有遇到。
此方法出自张鑫旭老师的博客,我也是了解了之后,才运用到实际开发中的
.parent {
position: relative;
height: 200px;
}
.child {
width: 80px;
height: 40px;
position: absolute;
left:;
top:;
right:;
bottom:;
margin: auto;
background: blue;
}
05 块级元素:padding
缺点:如果高度固定,需要提前计算尺寸(只在某些特定情况适用)。
.parent {
padding: 5% 0;
}
.child {
padding: 10% 0;
background: blue;
}
06 块级元素:display: table-cell
父元素一定要设置display:table,这样子元素的table-cell才能生效
.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: table;
}
.child {
display: table-cell;
vertical-align: middle;
}
07 块级元素:display: flex(移动端页面推荐)
缺点:兼容性不好
.parent {
width: 600px;
height: 200px;
border: 1px solid red;
display: flex;
align-items: center;
justify-content: center; /*水平居中*/
}
.child {
background: blue;
}
08 块级元素:伪元素
这个方案是从张鑫旭大神的新书:《css世界》里读到的
vertical-align这个属性需要与同元素内的行内元素的基线为参考,高度100%自然就以高度50%处即平常所说的中线为基线,middle默认对齐其基线,自然也就垂直居中对齐了
.parent {
width: 300px;
height: 300px;
border: 1px solid red;
text-align: center;
}
.child {
background: blue;
width: 100px;
height: 40px;
display: inline-block;
vertical-align: middle;
}
.parent::before {
content: '';
height: 100%;
display: inline-block;
vertical-align: middle;
}
09 块级元素:inline-block
HTML代码:
<div class="parent">
<div class="child">child</div>
<div class="brother">brother</div>
</div>
CSS代码:
.parent {
width: 400px;
height: 400px;
border: 1px solid red;
position: relative;
}
.child, .brother {
display: inline-block;
vertical-align: middle;
}
.child {
background: blue;
font-size: 12px;
}
.brother {
height: 400px;
font-size:;
}
其他
table布局是十年前前端最常用的布局方式,最省心但是冗余也很多,结构嵌套也比较深。现在前端变化日新月异,不推荐使用
<table>
<tr>
<td align="center" valign="middle">content</td>
</tr>
</table>
希望上述的总结对相关的同学能起到参考性的作用。
css设置居中的方案总结的更多相关文章
- 6 种CSS设置居中的方法
原文 Demos of each of the methods below by clicking here. Horizontal centering with css is rather easy ...
- 关于HTML+CSS设置图片居中的方法
有的时候我们会遇到这样一个问题,就是当我们按下F12进行使用firebug调试的时候,我们发现图像没有居中,页面底下有横向的滑动条出现,图片没能够居中,默认状态下只是紧靠在页面最左侧,而我们对图像缩小 ...
- 几个简单的css设置问题:div居中,ul li不换行 ,内容超出自动变省略号等
1 div在页面居中的问题 1)position值为relative时(相对定位),css设置属性margin:0 auto;(0 auto,表示上下边界为0,左右则根据宽度自适应相同值,即居中)即 ...
- css设置图片居中、居左、居右
CreateTime--2017年12月8日14:25:09 Author:Marydon css设置图片居中.居左.居右 图片一般默认是居左显示的,如何更改它的水平位置呢? <div st ...
- CSS水平和垂直居中方案
我们在网页布局的时候,经常会碰到需要居中的情况,那下面就来讲一下有哪几种目前比较常用的居中方案,它们的优点和缺点分别又是什么. 一.水平居中 方法①:(父元素)text-align,(子元素)in ...
- 【转】css布局居中和CSS内容居中区别和对应DIV CSS代码
原文地址:http://www.divcss5.com/jiqiao/j771.shtml css布局居中和CSS内容居中区别和对应DIV CSS代码教程与图文代码案例篇 对于新手来说DIV CSS布 ...
- CSS设置技巧
一.单位和值 1.1 颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令 ...
- css常用居中
对一个已知大小的元素上下左右居中(已知大小了,直接margin也就行了): css如下:.parent{height:100px;width:100px;background:grey;positio ...
- CSS 图像居中对齐
CSS 图像居中对齐 我们在<CSS 内外边距>学过内容居中,它的原理是将外边左右设置为auto.图像居中也是这个原理. 示例 <!DOCTYPE html> <htm ...
随机推荐
- BZOJ 1483: [HNOI2009]梦幻布丁 [链表启发式合并]
1483: [HNOI2009]梦幻布丁 题意:一个带颜色序列,一种颜色合并到另一种,询问有多少颜色段 一种颜色开一个链表,每次遍历小的合并到大的里,顺带维护答案 等等,合并方向有规定? 令col[x ...
- Laravel5.5核心架构理解
1.依赖注入 方法传入组件名,框架会自动实例化,方法内可直接使用 例如最常用的requert对象 2.服务容器 其实,Laravel 的核心就是一个 IoC 容器,Laravel 的核心本身十分轻量, ...
- JavaScript正则表达式函数总结
/* 测试环境:Chrome 63.0.3239.132 */ JS中正则对象修饰符可选值为:"i" "g" "m",即忽略大小写 进行全局 ...
- 阿里云学习之API网关
注:此处仅供api的创建做一个补充参考,API网关的优缺点及创建过程中的参数详情,请参考阿里云开放文档:https://helpcdn.aliyun.com/document_detail/29478 ...
- 【解决问题】SSH连不上Ubuntu虚拟机解决办法
1. 安装openssh-client Ubuntu默认缺省安装了openssh-client,apt-get安装即可 sudo apt-get install openssh-client 2. 安 ...
- Sublime Text3 快捷键汇总及设置快捷键配置环境变量
Ctrl+D 选词 (反复按快捷键,即可继续向下同时选中下一个相同的文本进行同时编辑)Ctrl+G 跳转到相应的行Ctrl+J 合并行(已选择需要合并的多行时)Ctrl+L 选择整行(按住-继续选择下 ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields【状压DP】题解+AC代码
题目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ...
- HashMap原理阅读
前言 还是需要从头阅读下HashMap的源码.目标在于更好的理解HashMap的用法,学习更精炼的编码规范,以及应对面试. 它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而 ...
- SpringMvc笔记-对RESTFUL风格的配置
1.@RequestMapping注解可以使用如下参数: 1,params:例如params={'username',"age!=100"}表示需要usernmame并且age 属 ...
- ubuntu17.10 python3.6 install plugins for AI
install order: tensorflow-gpu scikit-learn numpy scipy matplotlib tkinter tensorflow-gpu : pip insta ...