CSS实现水平垂直同时居中的6种思路
前面的话
水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的6种思路
水平对齐+行高
【思路一】text-align + line-height实现单行文本水平垂直居中
<style>
.test{
text-align: center;
line-height: 100px;
}
</style>
<div class="test" style="background-color: lightblue;width: 200px;">测试文字</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>
.parent{
display: table-cell;
text-align: center;
vertical-align: middle;
}
.child{
display: inline-block;
}
</style>
<div class="parent" style="background-color: gray; width:200px; height:100px;">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
【2】若子元素是图像,可不使用table-cell,而是其父元素用行高替代高度,且字体大小设为0。子元素本身设置vertical-align:middle
<style>
.parent{
text-align: center;
line-height: 100px;
font-size: 0;
}
.child{
vertical-align: middle;
}
</style>
<div class="parent" style="background-color: gray; width:200px; ">
<img class="child" src="http://sandbox.runjs.cn/uploads/rs/26/ddzmgynp/img1.gif" width="50%" alt="test">
</div>
margin+垂直对齐
【思路三】margin + vertical-align
要想在父元素中设置vertical-align,须设置为table-cell元素;要想让margin:0 auto实现水平居中的块元素内容撑开宽度,须设置为table元素。而table元素是可以嵌套在tabel-cell元素里面的,就像一个单元格里可以嵌套一个表格
[注意]若兼容IE7-浏览器,需将结构改为<table>结构
<style>
.parent{
display:table-cell;
vertical-align: middle;
}
.child{
display: table;
margin: 0 auto;
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
绝对定位
【思路四】使用absolute
【1】利用绝对定位元素的盒模型特性,在偏移属性为确定值的基础上,设置margin:auto
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
height: 50px;
width: 80px;
margin: auto;
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
【2】利用绝对定位元素的偏移属性和translate()函数的自身偏移达到水平垂直居中的效果
[注意]IE9-浏览器不支持
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
【3】在子元素宽高已知的情况下,可以配合margin负值达到水平垂直居中效果
<style>
.parent{
position: relative;
}
.child{
position: absolute;
top: 50%;
left: 50%;
width: 80px;
height: 60px;
margin-left: -40px;
margin-top: -30px;
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
flex
【思路五】使用flex
[注意]IE9-浏览器不支持
【1】在伸缩项目上使用margin:auto
<style>
.parent{
display: flex;
}
.child{
margin: auto;
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
【2】在伸缩容器上使用主轴对齐justify-content和侧轴对齐align-items
<style>
.parent{
display: flex;
justify-content: center;
align-items: center;
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
grid
【思路六】使用grid
[注意]IE10-浏览器不支持
【1】在网格项目中设置justify-self、align-self或者margin: auto
<style>
.parent{
display: grid;
}
.child{
align-self: center;
justify-self: center;
/* margin: auto; */
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
【2】在网格容器上设置justify-items、align-items或justify-content、align-content
<style>
.parent{
display: grid;
align-items: center;
justify-items: center;
/* align-content: center; */
/* justify-content: center; */
}
</style>
<div class="parent" style="background-color: lightgray; width:200px; height:100px; ">
<div class="child" style="background-color: lightblue;">测试文字</div>
</div>
CSS实现水平垂直同时居中的6种思路的更多相关文章
- CSS实现水平垂直同时居中的5种思路
× 目录 [1]水平对齐+行高 [2]水平+垂直对齐 [3]margin+垂直对齐[4]absolute[5]flex 前面的话 水平居中和垂直居中已经单独介绍过,本文将介绍水平垂直同时居中的5种思路 ...
- css 元素水平垂直方向居中
html部分 <div class="parent"> <div class="child"> - -居中- - </div> ...
- css3-7 如何让页面元素水平垂直都居中(元素定位要用css定位属性)
css3-7 如何让页面元素水平垂直都居中(元素定位要用css定位属性) 一.总结 一句话总结:元素定位要用css定位属性,而且一般脱离文档流更加好操作.先设置为绝对定位,上左都50%,然后margi ...
- CSS 布局 - 水平 & 垂直对齐
CSS 布局 - 水平 & 垂直对齐 一.元素居中对齐 要水平居中对齐一个元素(如 <div>), 可以使用 margin: auto;. 设置到元素的宽度将防止它溢出到容器的边缘 ...
- CSS:CSS 布局 - 水平 & 垂直对齐
ylbtech-CSS:CSS 布局 - 水平 & 垂直对齐 1.返回顶部 1. CSS 布局 - 水平 & 垂直对齐 水平 & 垂直居中对齐 元素居中对齐 要水平居中对齐一个 ...
- 小div在大div里面水平垂直都居中的实现方法
关于如何设置小盒子在大盒子里面水平垂直方向同时居中的实现方法有很多种,下面仅列举了常用的几种. 首先看一下要实现的效果图及对应的html代码: <div class="parent&q ...
- margin:-75px的理解及妙用——纯CSS制作水平/垂直都居中短边为50px/长边为150px的红色十字架
有这么一个题目: 使用重构的方式制作出一个如下图的水平.垂直都居中短边为50px,长边为150px的红色十字架. 要求只使用2个div完成 答案: <!DOCTYPE html PUBLIC & ...
- 如何将一个div盒子水平垂直都居中?
html代码如下: 固定样式: 方法一:利用定位(常用方法,推荐) .parent{ position:relative; } .child{ position:absolute; top:50%; ...
- CSS div水平垂直居中和div置于底部
一.水平居中 .hor_center { margin: 0 auto; } 二.水平垂直居中 .content { width: 360px; height: 240px; } .ver_hor_c ...
随机推荐
- 理解socket.io(一)---相关的API
理解socket.io(一)---相关的API 1. 什么是Socket.IO?Socket.IO是node.js的一个模块,它用于浏览器与服务端之间实时通信.它提供了服务器和客户端的组件,只需一个模 ...
- NSTimer深入理解
NSTimer,即计时器,用于定时执行一些任务,一次或者多次. 系统Foundation框架提供的最常用方法如下,创建一个NSTimer,并将它放到当前runloop的default mode中. + ...
- NDK toolchain对应ABI
有些时候,解决一些问题,我们需要多一些耐心. 从今天起,正式开始SkylineGlobe移动端Android版本的二次开发. Application.mk修改为NDK_TOOLCHAIN := arm ...
- 9、链表 & 状态机 & 多线程
链表的引入 从数组的缺陷说起 数组有2个缺陷:一个是数组中所有元素的类型必须一致:第二个是数组的元素个数必须事先制定并且一旦指定之后不能更改. 如何解决数组的2个缺陷:数组的第一个缺陷靠结构体去解决. ...
- WebService与CXF
一:Webservice 1:WebService是干什么的?有什么用? 一言以蔽之:WebService是一种跨编程语言和跨操作系统平台的远程调用技术. 所谓跨编程语言和跨操作平台,就是说服务端程序 ...
- 微信小程序之wx.request:fail错误,真机预览请求无效问题解决,安卓,ios网络预览异常
新版开发者工具增加了https检查功能:可使用此功能直接检查排查ssl协议版本问题: 可能原因:0:后台域名没有配置0.1:域名不支持https1:没有重启工具:2:域名没有备案,或是备案后不足24小 ...
- Nowcoder 牛客练习赛23
Preface 终于知道YKH他们为什么那么喜欢打牛客网了原来可以抽衣服 那天晚上有空就也去玩了下,刷了一波水TM的YKH就抽到了,我当然是没有了 题目偏水,好像都是1A的.才打了一个半小时,回家就直 ...
- bitcoin 源码解析 - 交易 Transaction(二) - 原理篇
这篇文章我断断续续写了呃···· 应该快三个星期了? 所以前后的风格可能差别相当大.真是十分的怠惰啊··· 最近实在是不够努力.用python重写bitcoin的项目也卡在网络编程部分(这方面真是我的 ...
- vue 路由拦截、axios请求拦截
路由拦截 项目中,有些页面需要登录后才能进入,例如,在某页面A,用户在操作前需要先进入登录页(此时需要将上一页的地址(/survey/start)作为query存入login页面的地址中,如: htt ...
- 【适配整理】Android 7.0 调取系统相机崩溃解决android.os.FileUriExposedException
一.写在前面 最近由于廖子尧忙于自己公司的事情和 OkGo (一款专注于让网络请求更简单的网络框架) ,故让LZ 接替维护 ImagePicker(一款支持单.多选.旋转和裁剪的图片选择器),也是处理 ...