开发者

Centering of a div, but the normal CSS does not work

开发者 https://www.devze.com 2022-12-28 23:16 出处:网络
I want to center a div, but the general way is #selector{position:relative;margin:0px开发者_运维百科 auto;}

I want to center a div, but the general way is

#selector{position:relative;margin:0px开发者_运维百科 auto;}

What exactly is wrong below

#crp{top:40%; position:absolute; margin:auto;}

The Div below is not nested but a standalone. The #crp is going to the extreme right.

<div id="crp">...something goes here....</div>

Thanks Jean


It is because its position is absolute, it does not work according to margins anymore, it is expecting you to fix its position.

Just like you are setting its vertical position with the top property, you have to set its horizontal position as well.


To center a div using margin:0 auto; you must specify width of the element.

You could also use text-align:center; on the parent container.

Also remove any absolute positioning because it breaks standard element flow.

0

精彩评论

暂无评论...
验证码 换一张
取 消