CSS 水平垂直居中的几种实现方法
前言
项目中经常碰到需要实现水平垂直居中的样式。下面就总结几种常用的方法
水平对齐+行高
【思路一】text-align + line-height实现单行文本水平垂直居中
<style>
.f10 .test{
text-align: center;
line-height: 100px;
}
</style>
<div class="case-box f10" data-case="f10">
<div class="test" style="background-color: lightblue;width: 200px;">测试文字</div>
</div>
效果展示:
水平+垂直对齐
【思路二】text-align + vertical-align
【1】在父元素设置text-align和vertical-align,并将父元素设置为table-cell元素,子元素设置为inline-block元素
[注意]若兼容IE7-浏览器,将结构改为<table>结构来实现table-cell的效果;用display:inline;zoom:1;来实现inline-block的效果
<style>
.f11 .parent{
display: table-cell;
text-align: center;
vertical-align: middle;
}
.f11 .child{
width: 80px;
display: inline-block;
}
</style>
<div class="case-box f11" data-case="f11">
<div class="parent" style="background-color: gray; width:200px; height:100px;">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
【2】若子元素是图像,可不使用table-cell,而是其父元素用行高替代高度,且字体大小设为0。子元素本身设置vertical-align:middle
<style>
.f12 .parent{
line-height: 200px;
text-align: center;
font-size:0;
}
.f12 .child{
vertical-align: middle;
}
</style>
<div class="case-box f12" data-case="f12">
<div class="parent" style="background-color: gray; width:200px;">
<img class="child" src="../../img/head.jpg" style="width:50px;" alt="test">
</div>
</div>
效果展示:
margin + vertical-align
要想在父元素中设置vertical-align,须设置为table-cell元素;要想让margin:0 auto实现水平居中的块元素内容撑开宽度,须设置为table元素。而table元素是可以嵌套在tabel-cell元素里面的,就像一个单元格里可以嵌套一个表格
[注意]若兼容IE7-浏览器,需将结构改为<table>结构
<style>
.f13 .parent{
display:table-cell;
vertical-align: middle;
}
.f13 .child{
display: table;
margin: 0 auto;
}
</style>
<div class="case-box f13" data-case="f13">
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
绝对定位
【1】利用绝对定位元素的盒模型特性,在偏移属性为确定值的基础上,设置margin:auto
<style>
.f14 .parent{
position: relative;
}
.f14 .child{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 50px;
width: 80px;
margin: auto;
}
</style>
<div class="case-box f14" data-case="f14">
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
【2】利用绝对定位元素的偏移属性和translate()函数的自身偏移达到水平垂直居中的效果
[注意]IE9-浏览器不支持
<style>
.f15 .parent{
position: relative;
}
.f15 .child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
<div class="case-box f15" data-case="f15">
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
flex
[注意]IE9-浏览器不支持
【1】在伸缩项目上使用margin:auto
<style>
.f16 .parent{
display: flex;
}
.f16 .child{
margin: auto;
}
</style>
<div class="case-box f16" data-case="f16">
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
【2】在伸缩容器上使用主轴对齐justify-content和侧轴对齐align-items
<style>
.f17 .parent{
display: flex;
justify-content: center;
align-items: center;
}
</style>
<div class="case-box f17" data-case="f17">
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
grid
[注意]IE10-浏览器不支持
【1】在网格项目中设置justify-self、align-self或者margin: auto
<style>
.f18 .parent{
display: grid;
}
.f18 .child{
align-self: center;
justify-self: center;
}
</style>
<div class="case-box f18" data-case="f18">
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
【2】在网格容器上设置justify-items、align-items或justify-content、align-content
<style>
.f19 .parent{
display: grid;
/*align-items: center;*/
/*justify-items: center;*/
align-content: center;
justify-content: center;
}
</style>
<div class="case-box f19" data-case="f19">
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
</div>
效果展示:
CSS 水平垂直居中的几种实现方法的更多相关文章
- 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实现水平垂直居中的1010种方式
转载自:CSS实现水平垂直居中的1010种方式 划重点,这是一道面试必考题,很多面试官都喜欢问这个问题,我就被问过好几次了 要实现上图的效果看似很简单,实则暗藏玄机,本文总结了一下CSS实现水平垂直居 ...
- CSS实现垂直居中的5种方法
利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 使用 CSS 实现垂直居中并不容易.有些方法在一些浏览器 ...
- CSS 垂直居中的5种实现方法
利用 CSS 来实现对象的垂直居中有许多不同的方法,比较难的是选择那个正确的方法.我下面说明一下我看到的好的方法和怎么来创建一个好的居中网站. 使用 CSS 实现垂直居中并不容易.有些 ...
- 顽石系列:CSS实现垂直居中的五种方法
顽石系列:CSS实现垂直居中的五种方法 在开发过程中,我们可能沿用或者试探性地去使用某种方法实现元素居中,但是对各种居中方法的以及使用场景很不清晰.参考的内容链接大概如下: 行内元素:https:// ...
- 手写面试编程题- 数组去重 深拷贝 获取文本节点 设置奇数偶数背景色 JS中检测变量为string类型的方法 第6题闭包 将两个数组合并为一个数组 怎样添加、移除、移动、复制、创建和查找节点? 继承 对一个数组实现随机排序 让元素水平 垂直居中的三种方式 通过jQuery的extend方法实现深拷贝
第1题==>实现数组去重 通过 new Set(数组名) // var arr = [12, 12, 3, 4, 5, 4, 5, 6, 6]; // var newarr1 = new Set ...
- css实现水平垂直居中的几种方式
梳理下平时常用css水平垂直居中方式- 使用flex布局 HTML <div class="box"> <div class="child"& ...
- 05. flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中)
flex元素水平垂直居中(三种position水平垂直居中和两种新老版本水平垂直居中) (1).position : <!DOCTYPE html> <html lang=" ...
随机推荐
- DP思想笔记
一.思想 DP也是把复杂的问题分解为许多子问题,与分治法不同的是,分治法的各个子问题互相之间没有联系,而动态规划却有.前一个子问题的结果与下一步的子问题的结果是什么有关系.这就决定了DP算法肯定有一个 ...
- pdf 下载demo
最近写了个pdf下载的demo,在这里记录一下.. 1 要下载pdf首先要有pdf 模板 ,制作pdf 模板就是 word 另存为 pdf . 2 用 Adobe Acrobat X Pro 这个软 ...
- Mac下使用国内镜像安装Homebrew
First MBP上的brew很老了,就想把brew更新一下,顺便安装一下NodeJs.无奈更新的过程一直卡在网络下载,毫不动弹.想想,应该是Repo访问不到的原因,于是重装brew. 根据官网上的方 ...
- 外部访问docker中的MySQL
注:192.168.1.203机器上装有docker,容器在该机器上 mysql> use mysql; mysql> update user set authentication_str ...
- Linux soft lockup分析
关键词:watchdog.soft lockup.percpu thread.lockdep等. 近日遇到一个soft lockup问题,打印类似“[ 56.032356] NMI watchdog: ...
- MyBatis 学习总结 01 快速入门
本文测试源码下载地址: http://onl5wa4sd.bkt.clouddn.com/MyBatis0918.rar 一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级 ...
- 阿里云 轻量应用服务器(LAMP) 使用日志记录
phpStudy(PHP运行环境一键安装包) https://www.jb51.net/softs/182860.html 0:PHP开发工具 https://netbeans.org/downloa ...
- js中的枚举
在JavaScript中,对象的属性分为可枚举和不可枚举之分,它们是由属性的enumerable值决定的.可枚举性决定了这个属性能否被for…in查找遍历到. js中基本包装类型的原型属性是不可枚举的 ...
- [转帖]Windows 10 部分早期版本已完全停止技术支持服务
Windows 10 部分早期版本已完全停止技术支持服务 2019-4-12 01:27| 发布者: cjy__05| 查看: 10186| 评论: 47|来自: pcbeta 收藏分享 转帖来源:h ...
- LeetCode-876 链表的中间结点
对于链表的中某个位置结点的定位一般都会用到两个链表结点指针,例如链表倒数第K个结点问题使用的是先后指针,该题中用到的快慢指针. 本题的具体解法就是快指针走两步.慢指针走一步知道遍历完结点,重点是分清题 ...