CSS水平垂直居中常见方法总结
1、元素水平居中
当然最好使的是:
margin: 0 auto;
居中不好使的原因:
1、元素没有设置宽度,没有宽度怎么居中嘛!
2、设置了宽度依然不好使,你设置的是行内元素吧,行内元素和块元素的区别以及如何将行内元素转换为块元素请看我的另一篇文章!
示例 1:
<div class="box">
<div class="content">
哇!居中了
</div>
</div> <style type="text/css">
.box {
background-color: #FF8C00;
width: 300px;
height: 300px;
margin: auto;
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
line-height: 100px;//文字在块内垂直居中
text-align: center;//文字居中
margin: 0 auto;
}
</style>
、元素水平垂直居中
方案1:position 元素已知宽度
父元素设置为:position: relative;
子元素设置为:position: absolute;
距上50%,据左50%,然后减去元素自身宽度的距离就可以实现
<div class="box">
<div class="content">
</div>
</div> .box {
background-color: #FF8C00;
width: 300px;
height: 300px;
position: relative;
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
margin: -50px 0 0 -50px;
}
方案2:position transform 元素未知宽度
如果元素未知宽度,只需将上面例子中的margin: -50px 0 0 -50px;替换为:transform: translate(-50%,-50%);
效果如上!
示例 3:
<div class="box">
<div class="content">
</div>
</div> .box {
background-color: #FF8C00;
width: 300px;
height: 300px;
position: relative;
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%,-50%);
}
方案3:flex布局 :父元素加
- display: flex;
- justify-content: center;
- align-items: center;
<div class="box">
<div class="content">
</div>
</div> .box {
background-color: #FF8C00;
width: 300px;
height: 300px;
display: flex; //flex布局
justify-content: center; //使子项目水平居中
align-items: center; //使子项目垂直居中
}
.content {
background-color: #F00;
width: 100px;
height: 100px;
}
方案4:table-cell布局
示例 5:
因为table-cell相当与表格的td,td为行内元素,无法设置宽和高,所以嵌套一层,嵌套一层必须设置display: inline-block;td的背景覆盖了橘黄色,不推荐使用
<div class="box">
<div class="content">
<div class="inner">
</div>
</div>
</div> .box {
background-color: #FF8C00;//橘黄色
width: 300px;
height: 300px;
display: table;
}
.content {
background-color: #F00;//红色
display: table-cell;
vertical-align: middle;//使子元素垂直居中
text-align: center;//使子元素水平居中
}
.inner {
background-color: #000;//黑色
display: inline-block;
width: 20%;
height: 20%;
}
CSS水平垂直居中常见方法总结的更多相关文章
- CSS水平垂直居中常见方法总结2
1.文本水平居中line-height,text-align:center(文字)元素水平居中 margin:0 auo 方案1:position 元素已知宽度 父元素设置为:position: re ...
- 53.CSS---CSS水平垂直居中常见方法总结
CSS水平垂直居中常见方法总结 1.元素水平居中 当然最好使的是: margin: 0 auto; 居中不好使的原因: 1.元素没有设置宽度,没有宽度怎么居中嘛! 2.设置了宽度依然不好使,你设置的是 ...
- CSS水平垂直居中的方法
原文链接:http://caibaojian.com/370.html 水平垂直居中,特别是使用在列表的时候经常会用到的,以前有需求的时候我也做过类似的代码,是使用display:table-cell ...
- 介绍一种css水平垂直居中的方法(非常好用!)
这次介绍一下一个水平垂直居中的css方法,这个方法可以说是百试百灵,废话不多说,直接附上代码: html,body{ width:100%; height:100%; } 你需要居中的元素{ posi ...
- css水平垂直居中的方法与 vertical-align 的用法
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 1. 已知元素宽度 方法一:已知宽高,可以用position定位 + margin负值的方法 : 绝对定位 ...
- css 水平垂直居中的方法总结
在项目中经常会遇到设置元素水平垂直居中的需求.而且具体的场景也不同,所以将个人总结的方法做个汇总,希望对浏览者有用. 以下所举的例子都以一个html为准,这里规定好一些公用样式. body { bac ...
- [css]水平垂直居中的方法
1.top:cale(50% - 2rem); left:cale(50% - 2rem);
- CSS水平垂直居中总结
行内元素水平居中 把行内元素包裹在块级父元素中,且父元素中的css设置text-align:center; <!DOCTYPE html> <html> <head> ...
- CSS水平垂直居中的几种方法2
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
随机推荐
- [NLP]Transformer-XL论文解读
关于Transformer的具体内容,可以访问:https://www.cnblogs.com/mj-selina/p/12369797.html 简介 Transformer是Google Brai ...
- 安装 Navicat for MySQL
安装 Navicat for MySQL 下载地址:https://www.pcsoft.com.cn/soft/20832.html
- 做新时代的奋斗者!(好吧,我还没弄出python的编译环境)
Pictures: 今日分来的补记来嘞: Game 1:Guess the number. Python包含许多内建的函数,有些函数存在于称为模块的单独的程序中,可以使用import语句把它们的模块导 ...
- sqlldr总结参数介绍
有效的关键字: userid -- ORACLE username/password control - 控制文件 log - 记录的日志文件 * 表示 ...
- python 递归调用 返回值问题
当使用递归时并有返回值时,调用自身函数时需要加上return语句如下: def daxiao(biao1,biao2): #判断两个列表的大小,根据里面的元素大小 #如果biao1大于 ...
- Java学习笔记(十)面向对象---接口
理解 初期理解 可以认为接口是一个特殊的抽象类.当接口中的方法都是抽象的,那么该类可以通过接口的形式来表示. class用于定义类 interface用于定义接口 格式特点 接口中常见定义:常量,抽象 ...
- 生成树计数 lighting 最终决定用这个模板! (有逆元的模板)
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> # ...
- Android学习15
Date&Time DatePicker(日期选择器),TimePicker(时间选择器),CalendarView(日期视图): 1.TextClock TextClock可以以字符串格式显 ...
- C++ 宏定义创建(销毁)单例
Util.h: #define CREATE_SINGLETON_POINTER(CLASS,INSTANCE,MUTEX) if (NULL == INSTANCE) \ { \ MUTEX.loc ...
- maven的安装与使用(运行单元测试和打包等)
maven的下载与安装 maven是用于java的自动化构建工具. 1.下载: http://maven.apache.org/download.cgi 下载maven包,比如 apache-mave ...