如何在父div中水平div [英] How to center horizontally div inside parent div

查看:139
本文介绍了如何在父div中水平div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将 div 水平居中在其父 div 中与 CSS

How do I center a div horizontally inside its parent div with CSS?

<div id='parent' style='width: 100%;'>
 <div id='child' style='width: 50px; height: 100px;'>Text</div>
</div>


推荐答案

我假设父div没有宽度或宽宽,并且子div具有较小的宽度。以下将顶部和底部的边距设置为零,边将自动适合。

I am assuming the parent div has no width or a wide width, and the child div has a smaller width. The following will set the margin for the top and bottom to zero, and the sides to automatically fit. This centers the div.

div#child {
    margin: 0 auto;
}

这篇关于如何在父div中水平div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆