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 ...
随机推荐
- 夏令营提高班上午上机测试 Day 3 解题报告
今天的题的确水.T3还是一道NOIP原题. 嘛,多刷点水题也不是什么坏事嘛. 说来也快,夏令营结束了整一星期了呢.大家也都回到了日常的暑假生活呢. 今天学业水平测试出成绩了...嗯结果还算满意呢,至少 ...
- Heartbeat实现热备
1.环境准备:1)主节点:master eth0:192.168.0.201 eth1:192.168.0.03 2)备节点:slave eth0 :192.168.0.215 eth1:192.16 ...
- ASCII Art (English)
Conmajia, 2012 Updated on Feb. 18, 2018 What is ASCII art? It's graphic symbols formed by ASCII char ...
- GO开发:接口
接口 接口定义 Interface类型可以定义一组方法,但是这些不需要实现.并且interface不能包含任何变量. type example interface{ Method1(参数列表) 返回值 ...
- mac攻略(1) -- 简单配置php开发环境
[http://www.cnblogs.com/redirect/p/6112154.html] 最简单直接的方式还是使用 Mac 上自带的 Apache 和 PHP. 1.启动 Apache ...
- 关于ssh登录出现异常警告:WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!
提示警告信息如下: arnold@WSN:~$ ssh 10.18.46.111 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- 解决CXF的java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
以下是错误信息 九月 25, 2017 8:22:04 下午 org.springframework.web.context.support.XmlWebApplicationContext prep ...
- Java经典编程题50道之九
一个数如果恰好等于它的因子之和,这个数就称为"完数".例如6=1+2+3.编程找出1000以内的所有完数. public class Example09 { public s ...
- 基于jquery的城市选择插件
城市选择插件的难度不是很大,主要是对dom节点的操作.而我写的这个插件相对功能比较简答,没有加入省市联动. 上代码好了,参照代码的注释应该比较好理解. /* *基于jquery的城市选择插件 *aut ...
- LeetCode第七天
==数组 Medium== 40.(162)Find Peak Element JAVA //斜率思想,二分法 class Solution { public int findPeakElement( ...