css宽高不固定如何实现居中


当前第2页 返回上一页

这方法功能很强大,也比较灵活,不仅仅局限在实现居中显示。 兼容方面也一样拿IE来做比较,第二种方法IE8以上都能使用。 IE8及IE8以下都会出现问题。

1

2

3

4

5

6

7

8

9

10

<body>

    <div id="box">

        <div id="content">div被其中的内容撑起宽高</div>

    </div>       

</body>

body,html { margin:0; width:100%; height:100%; }

#box { width:100%; height:100%; background:rgba(0,0,0,0.7); position:relative; }

#content{ position:absolute; background:pink; left:50%; top:50%; 

transform:translateX(-50%) translateY(-50%);

-webkit-transform:translateX(-50%) translateY(-50%); }

方法3:利用table-cell

利用 table 的单元格居中效果展示。与 flex 一样,需要写在父级元素上。

1

2

3

4

5

6

7

8

9

10

11

<div class="wrapper">

    <p>horizontal and vertical</p>

</div>

.wrapper {

    width: 300px;

    height: 300px;

    border: 1px solid #ccc;

    display: table-cell;

    text-align: center;

    vertical-align: middle;

}

以上就是css宽高不固定如何实现居中的详细内容,更多文章请关注木庄网络博客

返回前面的内容

相关阅读 >>

css怎么实现平方

css样式“list-style:none”是什么意思?

css如何设置行距

css在html中三种实现方式是什么

使用纯css画一个圆环(代码示例)

最受欢迎的10大css框架

css如何设置首行缩进2个字符

css中伪类是什么

css如何实现按钮透明

css响应式布局之媒体查询

更多相关阅读请进入《css》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...