div水平垂直居中的方法
2018-01-26 15:47
309 查看
下面几个方法的html 代码
方法一:(只能用于已知宽高的div);
兼容性:IE7+;
方法二:(不需要知道div宽高)
兼容性:IE8+
方法三:(使用css3方法)
兼容性:IE9+;
方法四:(css3的弹性盒子-代码最少)
兼容性:IE10+
方法五:(利用table表格)
兼容性: IE8+
<div id="box"> <div id="div"> </div> </div>
方法一:(只能用于已知宽高的div);
兼容性:IE7+;
/* css代码:*/ #box{ position:relative; } #div { height: 200px; width: 200px; /* 主要代码 */ position:absolute; top: 50%; left: 50%; margin-top: -100px; margin-left: -100px; }
方法二:(不需要知道div宽高)
兼容性:IE8+
/* css代码*/ #box{ position:relative; } #div{ height: 200px; width: 200px; /* 主要代码 */ position: absolute; top: 0; botton: 0; left: 0; right: 0; margin: auto; }
方法三:(使用css3方法)
兼容性:IE9+;
/* css代码*/ #box{ position:relative; } #div{ height: 200px; width: 200px; /* 主要代码 */ position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%); }
方法四:(css3的弹性盒子-代码最少)
兼容性:IE10+
#box{ display: flex; justify-content: center; align-items: center; }
方法五:(利用table表格)
兼容性: IE8+
/* css代码*/ #table{ display: table; } #table-cell{ display: table-cell; text-align: center; vertical-align:middle; } #div{ diaplay:inline-block; } /* html代码 */ <div id="table"> <div id="table-cell"> <div id="div"> <div> </div> <div>
相关文章推荐
- 使一个div在body中水平垂直居中的方法
- 让DIV水平和垂直居中的几种方法
- 让div垂直水平居中的方法
- 让DIV水平和垂直居中的几种方法
- DIV里面的图片水平与垂直居中的方法
- div垂直水平居中的五种方法
- HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结
- 在DIV中图片垂直、水平居中(最简单方法)
- 未知高度的div在父元素中水平垂直居中的方法
- DIV里面的图片水平与垂直居中的方法
- 未知宽高div水平垂直居中3种方法
- 未知div宽度高度使div水平垂直居中(弹性盒子方法)
- div水平居中垂直居中方法
- 水平垂直居中div三种方法
- DIV的垂直和水平居中的各种方法,总有一个适合你!
- DIV和Table的水平、垂直居中方法
- 总结div里面水平垂直居中的实现方法
- div盒子水平垂直居中的方法
- HTML+CSS,让div在屏幕中居中(水平居中+垂直居中)方法总结
- 让DIV水平垂直居中的两种完美方法推荐