How to center horizontally div inside parent div

匿名 (未验证) 提交于 2019-12-03 01:11:01

问题:

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

Text

回答1:

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; } 


回答2:

Text


回答3:

Text


回答4:

You can use the "auto" value for the left and right margins to let the browser distribute the available space equally at both sides of the inner div:

Text


回答5:

Just out of interest, if you want to center two or more divs (so they're side by side in the center), then here's how to do it:

Div 1
Div 2


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!