div盒子水平垂直居中的方法
一、盒子没有固定的宽和高
方案1、Transforms 变形
这是最简单的方法,不仅能实现绝对居中同样的效果,也支持联合可变高度方式使用。内容块定义transform: translate(-50%,-50%) 必须加上
top: 50%; left: 50%;
优点:
1. 内容可变高度
2. 代码量少
缺点:
1. IE8不支持
2. 属性需要写浏览器厂商前缀
3. 可能干扰其他transform效果
4. 某些情形下会出现文本或元素边界渲染模糊的现象
<div class="wrapper">我不知道我的宽度和高是多少,我要实现水平垂直居中。</div>
.wrapper {padding: 20px;background: orange;color: #fff;position: absolute;top: 50%;left: 50%;border-radius: 5px;-webkit-transform: translate(-50%, -50%);-moz-transform: translate(-50%, -50%);transform: translate(-50%, -50%);}
方案二2、在父级元素上面加上上面3句话,就可以实现子元素水平垂直居中。
<div class="wrapper">我不知道我的宽度和高是多少,我要实现水平垂直居中。
</div>
.wrapper {width: 500px;height: 300px;background: orange;color: #fff;/*只需要在父元素上加这三句*/justify-content: center; /*子元素水平居中*/align-items: center; /*子元素垂直居中*/display: -webkit-flex;}
二、盒子有固定的宽和高
方案1、margin 负间距
此方案代码关键点:1.必需知道该div的宽度和高度,
2.然后设置位置为绝对位置,
3.距离页面窗口左边框和上边框的距离设置为50%,这个50%就是指页面窗口的宽度和高度的50%,
4.最后将该div分别左移和上移,左移和上移的大小就是该DIV宽度和高度的一半。
<div class="wrapper">我知道我的宽度和高是多少,我要实现水平垂直居中。</div>
.wrapper {width: 400px;height: 18px;padding: 20px;background: orange;color: #fff;position: absolute;top:50%;left:50%;margin-top: -9px;margin-left: -200px;}
方案2、margin:auto实现绝对定位元素的居中(该方法兼容ie8以上浏览器)
此方案代码关键点:1、上下左右均0位置定位;
2、margin: auto;
<div class="wrapper">我不知道我的宽度和高是多少,我要实现水平垂直居中。</div>
.wrapper {width: 400px;height: 18px;padding:20px;background: orange;color: #fff;position: absolute;left:0;right:0;top: 0;bottom: 0;margin: auto;}
div盒子水平垂直居中的方法相关推荐
- css实现文字或者div盒子水平垂直居中的方法
实现水平垂直居中的方法 文本(文字)内容属于行内元素 行内元素水平垂直居中方法 方式一: 设置文字外层的盒子 text-align:center /*水平居中*/ height = 100px; /* ...
- HTML5/CSS3基础——div盒子水平垂直居中的三种方案
HTML5/CSS3基础--div盒子水平垂直居中的三种方案 一.基于定位的使div盒子水平居中的三种方式 1.使用top:50% left:50% 以及margin-top 和 margin-lef ...
- 文字居中、CSS中实现盒子水平垂直居中的方法
1. 文字居中: 第一种方式: 当用text-align:center时,达到的是水平居中的效果,那再用vertical-align:middle:其实是实现不了垂直居中的,此时就需要用到displa ...
- CSS实现div盒子水平垂直居中的方式
网上看到有很多方式,这里记录一下我用起来顺手的几种方式,这里敲黑板做笔记,哈哈~~ <div id='container'><div class='middle'></d ...
- div盒子水平垂直居中以及表格的居中的方法
目录 多种水平垂直居中方法 这里的方法和第二种一致,方便用来对系统后台进行布局 #admin {position: absolute;top: 50%;left: 50%;margin-top: -1 ...
- 如何实现div盒子水平垂直居中
关于如何使一个小盒子在大盒子中水平垂直居中有很多方法,下面将列举常用的几种: ###html 代码 <div class="parent"><div class= ...
- div盒子水平垂直居中的几种方式
1. div水平居中 div{width:100px;height:100px;margin:0 auto; } 2. div水平垂直居中 这里要有两层或者给body设置高度100vh,然后给元素设置 ...
- 如何将一个div盒子水平垂直居中?
1: 第一种方法: <div class="parent"> // 父级盒子<div class="child"><div> ...
- html flex上下居中,css3 flex实现div内容水平垂直居中的几种方法
一.flex-direction: (元素排列方向) ※ flex-direction:row (横向从左到右排列==左对齐) ※ flex-direction:row-reverse (与row 相 ...
最新文章
- barplot参数 python_Python零基础入门Python数据分析最好的实战项目
- 计算机组成原理第4章-指令系统
- ZooKeeper 定位:能解决什么问题?不能解决什么问题?
- C# 系统应用之注册表使用详解
- 一张图看懂小程序全生态
- 局域网速度变慢的故障分析
- oracle授权操作
- 16年的大数据经验,为了搞定数字化转型,我和老板做了个赌注
- Java文件编码格式转换
- 【云计算】Netflix 开源持续交付平台 Spinnaker
- 凯恩帝k1000ti参数设置_凯恩帝数控怎么修改参数
- This tag and its children can be replaced by one TextView/ and a compound drawable
- Shell脚本中各种括号用法
- 实验四 shell 编程
- 世界第 3 的滴滴裁员,求职必知独角兽公司排行榜
- VUE中的filters过滤器2种用法
- 塞力斯是鸿蒙系统吗,赛力斯“驼峰”智能增程系统为何物?一亮相就引起行业沸腾...
- You have to use a classifier to attach supplemental artifacts to the project instead of replacing th
- 解决lightdm启动时黑屏的问题
- 阿里云短信服务不对个人开放?如何在阿里云市场免费购买短信服务?云市场购买到的短信服务如何使用?(以谷粒学院项目为例)