tags:

views:

41

answers:

2

Hello,

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

+3  A: 

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.

Francisco Soto
I tried with :relative
Jean
Try setting the width of your div, as @rochal suggested.
Francisco Soto
@soto Its not the contents, text-align, aligns the contents not the whole div.
Jean
+2  A: 

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.

rochal
Its not only text, but images etc.,#crp is the parent containerspecified width:100% does not seem to work though
Jean
<div style="text-align:center;"><img /></div> WILL center align the image as well.
rochal
I want to align the div not the contents
Jean