Centering a div but normal CSS doesn't work

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 standalone. #crp is in rightmost order.

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

      

Thanks Jean

+2


a source to share


2 answers


This is because its position is absolute, it no longer works according to the margins, it expects you to fix its position.



Just like you set the vertical position with the top property, you must also set its horizontal position.

+3


a source


To center the div with margin:0 auto;

, you must specify the width

element.

You can also use text-align:center;

in parent container.



Also remove any absolute positioning because it interrupts the flow of standard elements.

+2


a source







All Articles