css "left" not working

Oto Shavadze picture Oto Shavadze · Jan 5, 2013 · Viewed 24.3k times · Source

I have 2 divs, parent and child, I want that child left side (left border) will in center of parent.

Why this code not working? that is left: 50% for child, is not working.

<div id="outher">
    <div id="inner">

    </div>
</div>

css:

#outher {
   width: 1000px;
   height: 1000px;
   background-color: #ccc;
}

#inner {
   width: 400px;
   height: 300px;
   background-color: #090;
   left: 50%;
}

demo http://jsfiddle.net/vrse2/5/

Answer

Paul Fleming picture Paul Fleming · Jan 5, 2013

You need to set position to absolute or relative:

#inner {
   width: 400px;
   height: 300px;
   background-color: #090;
   position: absolute;
   left: 50%;
}