前端之旅HTML与CSS篇之自己总结的关于居中的方法
1.text-align:center ;在父容器里水平居中 inline 文字,或 inline 元素
2.line-height 与 height 相等时,垂直居中文字(文字垂直水平居中,同时满足1、2)
3.vertical-align:middle ;垂直居中 inline 文字,inline 元素,配合 display:table ,display:table-cell,有奇效,如下
.parent {
width:800px;height:500px;border:2px solid ;
display:table-cell;
vertical-align:middle;
text-align: center;
}
.child {
width:200px;height:200px;
display:inline-block;
background-color: red;
}
4.margin:auto 块的水平居中(标准写法为margin:0 auto;因为margin的top与bottom对设置auto没有效果)
原理为:auto默认是使用剩余空间,所以不论left还是right定义了auto,计算值都会是包含块的剩余空间,如果左右都设置了auto,那么就会均分剩余空间。
当然,这指的是书写模式为lr-tb的情况下,这种情况,宽度是一定的。
至于纵向,高度其实是没有一个固定值,auto无可refer的参照物,如果top或bottom设置了auto,那计算值其实会是0。
5.绝对定位(以父级为定位基准,设置绝对定位来垂直水平居中)
.big{
width:200px;height:200px; /*父级元素宽高设定 */
position:relative;
}
.small{
position:absolute;/*元素宽高任意 */
left:0;top:0;right:0;bottom:0;
margin:auto;
}
6.以FLEX的布局的方法居中,如下
.parent {
width:800px;height:500px;
border:2px solid;
display:flex;
justify-content:center;/*项目在主轴上居中 */
align-items:center;/*项目在交叉轴上居中*/
}
.child {
width:200px;height:200px;
background-color: red;
}
7.设定水平和垂直偏移父元素的50%,再根据实际长度将子元素上左挪回一半大小,如下
.parent {
width:800px;height:500px;
border:2px solid;
position:relative;
}
.child {
width:300px;height:200px;
margin:auto;
position:absolute;
left:50%;top:50%;
margin-left: -150px;margin-top:-100px;/*自己宽高的一半*/
background-color: red;
}
以下为大牛司徒正美总结的方法(还在理解中):
hacks, hacks(小技巧)
有许多 hacks ,负 margin,影子元素 ::before 等。如果你的内容不是固定大小的话,它们大部分是很脆弱的。
translate(-50%,-50%)
用 position 加 translate translate(-50%,-50%) 比较奇特,百分比计算不是以父元素为基准,而是以自己为基准。
参考文章:居中百分比宽高的元素
示例:
<style>
#container {
width:200px; height:200px;
background-color:yellow;
position:relative; }
#content { left:50%; top:50%;
transform:translate(-50%,-50%); -webkit-transform:translate(-50%,-50%);
background-color:gray; color:white; position:absolute; }
</style>
<div id="container"><div id="content">Hello World</div></div>
这个技巧相当嚣张,同样适用于没固定大小的内容,min-width,max-height,overflow:scroll 等。
绝对定位居中
父容器元素:position: relative
.Absolute-Center {
width: 50%;
height: 50%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
注意:高度必须定义,建议加 overflow: auto,防止内容溢出。
视口居中
内容元素:position: fixed,z-index: 999,记住父容器元素 position: relative
.Absolute-Center.is-Fixed {
width: 50%;
height: 50%;
overflow: auto;
margin: auto;
position: fixed;
top: 0; left: 0; bottom: 0; right: 0;
z-index: 999;
}
模态窗口实例
响应式
百分比宽高,最大、最小宽度均可以,加 padding 也可以
.Absolute-Center.is-Responsive {
width: 60%;
height: 60%;
min-width: 400px;
max-width: 500px;
padding: 40px;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
偏移
只要 margin: auto; 在,内容块将垂直居中,top, left, bottom, right 可以设置偏移。
.Absolute-Center.is-Right {
width: 50%;
height: 50%;
margin: auto;
overflow: auto;
position: absolute;
top: 0; left: auto; bottom: 0; right: 20px;
text-align: right;
}
溢出
居中内容比父容器高时,防止溢出,加 overflow: auto (没有任何 padding 时,也可以加 max-height: 100%;)。
.Absolute-Center.is-Overflow {
width: 50%;
height: 300px;
max-height: 100%;
margin: auto;
overflow: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
调整尺寸
resize 属性可以让尺寸可调。 设置 min- /max- 限制尺寸,确定加了 overflow: auto 。
.Absolute-Center.is-Resizable {
min-width: 20%;
max-width: 80%;
min-height: 20%;
max-height: 80%;
resize: both;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
图像
图像同样适用,设置 height: auto;
.Absolute-Center.is-Image {
width: 50%;
height: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
可变高度
高度必须定义,但可以是百分比或 max-height。不想定义高度的话,用 display: table (需要考虑 Table-Cell 兼容性)。
.Absolute-Center.is-Variable {
display: table;
width: 50%;
overflow: auto;
margin: auto;
position: absolute;
top: 0; left: 0; bottom: 0; right: 0;
}
负 margin
确切知道宽高,负 margin 是宽和高的一半。
.is-Negative {
width: 300px;
height: 200px;
padding: 20px;
position: absolute;
top: 50%; left: 50%;
margin-left: -170px; /* (width + padding)/2 */
margin-top: -120px; /* (height + padding)/2 */
}
Table-Cell结构:
<div class="Pos-Container is-Table">
<div class="Table-Cell">
<div class="Center-Block">
<!-- CONTENT -->
</div>
</div>
</div>
样式:
.Pos-Container.is-Table { display: table; }
.is-Table .Table-Cell {
display: table-cell;
vertical-align: middle;
}
.is-Table .Center-Block {
width: 50%;
margin: 0 auto;
}
前端之旅HTML与CSS篇之自己总结的关于居中的方法的更多相关文章
- 前端之旅HTML与CSS篇之IE6常见BUG
1.IE6怪异解析之padding与border算入宽高原因:未加文档声明造成非盒模型解析解决方法:加入文档声明<!doctype html> 2.IE6在块元素.左右浮动.设定marin ...
- 前端之旅HTML与CSS篇之block与inline的区别
display:block;和display:inline;的区别block元素特点: 1)处于常规流中时,如果width没有设置,会自动填充满父容器 2)可以应用margin/padding 3)在 ...
- 前端之旅HTML与CSS篇之清除浮动塌陷
以下内容为转载. 方法1:给浮动的元素的上级添加高度如果一个元素要浮动,那么它的祖先元素一定要有高度.高度的盒子,才能关住浮动.只要浮动在一个有高度的盒子中,那么这个浮动就不会影响后面的浮动元素.所以 ...
- 前端之旅HTML与CSS篇之a便签中放入其他块元素会撑大高度的原因
原因:a元素下有一个匿名文本,这个文本外有一个匿名行级盒子,它有的默认vertical-align是baseline的,而且往往因为上文line-height的影响,使它有个line-height,从 ...
- (转)BAT及各大互联网公司2014前端笔试面试题--Html,Css篇
BAT及各大互联网公司2014前端笔试面试题--Html,Css篇 很多面试题是我自己面试BAT亲身经历碰到的.整理分享出来希望更多的前端er共同进步吧,不仅适用于求职者,对于巩固复习前端基础更是 ...
- BAT及各大互联网公司2014前端笔试面试题--Html,Css篇
很多面试题是我自己面试BAT亲身经历碰到的.整理分享出来希望更多的前端er共同进步吧,不仅适用于求职者,对于巩固复习前端基础更是大有裨益. 而更多的题目是我一路以来收集的,也有往年的,答案不确保一定正 ...
- BAT及各大互联网公司前端笔试面试题--Html,Css篇
注意 转载须保留原文链接(http://www.cnblogs.com/wzhiq896/p/5931347.html )作者:wangwen896 整理分享出来希望更多的前端er共同进步吧,不仅适用 ...
- BAT及各大互联网公司2014前端笔试面试题--Html,Css篇(昨天有个群友表示写的简单了点,然后我无情的把他的抄了一遍)
某个群友 http://www.cnblogs.com/coco1s/ 很多面试题是我自己面试BAT亲身经历碰到的.整理分享出来希望更多的前端er共同进步吧,不仅适用于求职者,对于巩固复习前端基础 ...
- 各大互联网公司2014前端笔试面试题–HTML,CSS篇
Html篇: 1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? IE: trident内核 Firefox:gecko内核 Safari:webkit内核 Opera:以前是presto ...
随机推荐
- Python第二话 初识复杂数据类型(list、dictionary、tuple)
上一篇我们简单认识了数据类型:数字number和字符串string,这篇我们就来隆重介绍一下重量级的数据类型:列表list.字典dictionary和元组tuple. 一.列表List: ①列表是什么 ...
- [luogu1402]酒店之王_网络流
酒店之王 luogu-1402 题目大意:有n个人,p道菜,q个房间,每个人喜欢吃一些菜.喜欢住一些房间,如果一个人即住到了他喜欢的房间有吃到了他喜欢的菜,就对答案贡献++,求最大贡献. 注释:1&l ...
- Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版
一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...
- CSS服务器字体
1,首先要下载ttf文件 推荐下载网站: https://www.dafont.com/ 2,写css样式 3,服务器字体 font-family:自己随便取个名字就行 注意url里的ttf文件和f ...
- beta冲刺总结-咸鱼
前言:emmmmmmm冲刺总结应该可以吐槽了?我发誓后面几篇冲刺我是很努力用正经语言描述了!!!!! 心得:emmmmm,说真的--到beta冲刺的时候才是真正感受到了组队的存在,基本上隔三差五就约一 ...
- C语言的第一次作业总结
PTA实验作业 题目一:温度转换 本题要求编写程序,计算华氏温度150°F对应的摄氏温度.计算公式:C=5×(F−32)/9,式中:C表示摄氏温度,F表示华氏温度,输出数据要求为整型. 1.实验代码: ...
- Beta阶段敏捷冲刺报告-DAY2
Beta阶段敏捷冲刺报告-DAY2 Scrum Meeting 敏捷开发日期 2017.11.3 会议时间 13:00 会议地点 微信群 参会人员 项目组全体成员 会议内容 打包问题修复, 爬虫优化, ...
- 数字是否可以被3和5同时整除,use if and % (21.9.2017)
#include <stdio.h> int main(){ int a; //a是所输入的数字 printf("输入数字: "); scanf("%d&qu ...
- python 继承基础
class annamal: def chi(self): print(self.name + '吃') def he(self): print(self.name + '喝') class dog( ...
- RxSwift 函数响应式编程
Max 在 Boston 上学,在 San Francisco 工作,是一名软件工程师及创业者.当他还在高中的时候就在一家创业公司工作了,他非常喜欢使用 iOS.Android 以及 JavaScri ...