一个盒子在另一个盒子中垂直居中的方法
2017-05-09 18:41
232 查看
今天介绍几种让一个盒子在另一个盒子中居中的方法;
内联元素:给子盒子设置display属性为inline,然后在设置line-height为父盒子的高度
html:
结果:
对于内联元素垂直居中的方法还有很多,此处只介绍一种,重点介绍两种块状元素垂直居中的方法。
块状元素:
1、父盒子设置display属性为flex,flex-direction:column, justify-content: center;
html:
2、设置父盒子display属性值为 -webkit-box,;-webkit-box-orient:horizontal;-webkit-box-align:center;
html:
让元素垂直居中的方法还有很多,这里就不一一介绍了。本人水平有限,写的不好的地方欢迎指出,共同讨论。
内联元素:给子盒子设置display属性为inline,然后在设置line-height为父盒子的高度
html:
<div class="box"> <p>p标签</p> </div>css:
.box { width: 200px; height: 200px; margin: 100px auto; border: 1px solid #f00; text-align: center; } p { display: inline; line-height: 200px; background-color: #f40; }
结果:
对于内联元素垂直居中的方法还有很多,此处只介绍一种,重点介绍两种块状元素垂直居中的方法。
块状元素:
1、父盒子设置display属性为flex,flex-direction:column, justify-content: center;
html:
<div class="box"> <div class="small"></div> </div>css:
.box { width: 200px; height: 200px; margin: 100px auto; border: 1px solid #f00; display: flex; flex-direction: column; justify-content: center; } .small { width: 100px; height: 100px; background-color: #f40; }结果:
2、设置父盒子display属性值为 -webkit-box,;-webkit-box-orient:horizontal;-webkit-box-align:center;
html:
<div class="box"> <div class="small"></div> </div>css:
.box { width: 200px; height: 200px; margin: 100px auto; border: 1px solid #f00; display:-webkit-box; -webkit-box-orient:horizontal; -webkit-box-align:center; } .small { width: 100px; height: 100px; background-color: #f40; }结果:
让元素垂直居中的方法还有很多,这里就不一一介绍了。本人水平有限,写的不好的地方欢迎指出,共同讨论。
相关文章推荐
- 一个未知高度垂直居中的简单方法
- 一个垂直居中显示元素的方法
- div+css 怎么让一个小div在另一个大div里面 垂直居中
- 如何让一个盒子垂直居中
- div盒子水平垂直居中的方法
- 使一个div在body中水平垂直居中的方法
- div盒子水平垂直居中的方法
- div+css 让一个小div在另一个大div里面 垂直居中
- div+css 怎么让一个小div在另一个大div里面 垂直居中
- 让一个元素垂直水平居中的三种方法
- 一张图片在一个相应盒子中垂直居中
- 利用css来让一个div在页面中垂直居中的方法
- 一个好用的让DIV水平垂直居中的方法
- 如何将一个div水平垂直居中?4种方法做推荐
- 如何使一个div在另一个div中绝对居中(水平与垂直方向)
- div+css 怎么让一个小div在另一个大div里面 垂直居中
- css让一个div水平垂直居中方法大全
- 让一个元素垂直水平居中的四种方法
- css 几种水平垂直居中的方法 及 弹性盒子
- 深入理解盒子——模型文本垂直居中的N种方法 单行/多行文字(未知高度/固定高度)