美文网首页
div垂直居中的几种方法

div垂直居中的几种方法

作者: 司小璇 | 来源:发表于2018-07-20 10:49 被阅读0次

Ⅰ 方法一:使用定位的方法(如下图)

图片.png

(我把我的样式代码粘贴到下边了)

.bg-box {width: 200px;height: 200px;margin:30px auto;border: 1px solid red;position:relative;}

.sm-box {width: 100px;height: 100px;border: 1px solid red;background: red;position:absolute; top: 50%;left:50%;margin-top: -50px; /这里是小盒子高的一半/margin-left: -50px; /这里是小盒子宽的一半/}


Ⅱ 方法二:利用定位及margin:auto实现(如下图)

图片.png

(我把我的样式代码粘贴到下边了)

.bg-box {width: 200px;height: 200px;margin:30px auto;border: 1px solid red;position:relative;}

.sm-box {width: 100px;height: 100px;border: 1px solid red;background: red;position: absolute;margin: auto;top: 0;left: 0;right: 0;bottom: 0;}


Ⅲ 使用display:table-cell; (如下图)

图片.png

(我把我的样式代码粘贴到下边了)

.bg-box {width: 200px; height: 200px;border: 1px solid red;display: table-cell;vertical-align: middle;text-align: center;}

.sm-box { width: 100px;height: 100px;border: 1px solid red;background: red; display: inline-block; }


Ⅳ 方法四:使用伸缩布局display:flex;(如下图)

图片.png

(我把我的样式代码粘贴到下边了)

.bg-box { width: 200px;height: 200px;border: 1px solid red;display: flex; justify-content: center; /水平居中/ align-items: center; /垂直居中/ }

.sm-box {width: 100px;height: 100px;border: 1px solid red; background: red;}


Ⅴ 计算四周的间距设置子元素与父元素之间的margin-top和margin-left;(如下图)

图片.png

(我把我的样式代码粘贴到下边了)

.bg-box { width: 200px;height: 200px;border: 1px solid red; }

.sm-box { width: 100px;height: 100px;border: 1px solid red; background: red; margin-top: 50px; margin-left: 50px; }

相关文章

  • 不定宽高的div盒子和不定宽高的盒子水平垂直居中

    让一个不定宽高的div,垂直水平居中的几种实现方式 不定宽高的div垂直居中的方式: 1、使用CSS方法: 父盒子...

  • CSS div居中的几种方法

    CSS实现div垂直居中的方法有很多,下面div居中的几种方法是自己平时写网页中经常用到的,最常见的例子就是登录注...

  • CSS水平垂直居中总结

    CSS水平居中、垂直居中、水平垂直居中方法总结 文字的水平居中: 单行文字的垂直居中: 让有宽度的div水平居中:...

  • css实现盒子内部 div水平垂直居中

    总结一下利用css实现盒子内部 div居中的几种方法 1.水平居中 1)margin: 0 auto 2.水平垂直...

  • 2018-05-23

    div高度不固定,垂直居中的方法-汇总 1、高度宽度不固定,内容区域垂直居中

  • div垂直居中的几种方法

    1.div绝对定位水平垂直居中【margin:auto实现绝对定位元素的居中】 兼容性:,IE7及之前版本不支持 ...

  • div垂直居中的几种方法

    Ⅰ 方法一:使用定位的方法(如下图) (我把我的样式代码粘贴到下边了) .bg-box {width: 200px...

  • Css让div在屏幕上居中

    # 让div在屏幕上居中(水平居中+垂直居中)的方法总结 - html代码如下: - Css居中方法 (敲黑板)...

  • 工作学习笔记

    1、div中文字水平垂直居中方法 垂直居中: align-items:center;display:-webkit...

  • 水平垂直居中的实现方法

    小div在大div里面水平垂直居中的实现方法 初学前端时,我们都经常会练习实现关于水平垂直居中。那么你们都会用...

网友评论

      本文标题:div垂直居中的几种方法

      本文链接:https://www.haomeiwen.com/subject/bryfmftx.html