Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css "left" not working

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/

like image 962
Oto Shavadze Avatar asked Jan 05 '13 12:01

Oto Shavadze


People also ask

How do I make CSS left?

If position: absolute; or position: fixed; - the left property sets the left edge of an element to a unit to the left of the left edge of its nearest positioned ancestor.

What is left 0 right 0 in CSS?

Setting top: 0; left: 0; bottom: 0; right: 0; gives the browser a new bounding box for the block. At this point the block will fill all available space in its offset parent, which is the body or position: relative; container.

What does Left mean in CSS?

The left property in CSS is used to specify the horizontal position of a positioned element. It has no effect on non-positioned elements. Note: If position property is absolute or fixed, the left property specifies the distance between the element left edge and the left edge of its containing block.

How do I place a div to the left?

Use CSS property to set the height and width of div and use display property to place div in side-by-side format. float:left; This property is used for those elements(div) that will float on left side. float:right; This property is used for those elements(div) that will float on right side.


5 Answers

You need to set position to absolute or relative:

#inner {
   width: 400px;
   height: 300px;
   background-color: #090;
   position: absolute;
   left: 50%;
}
like image 69
Paul Fleming Avatar answered Oct 02 '22 21:10

Paul Fleming


CSS left only works with positioned elements.

Quoted from W3C

Values  <length> | <percentage> | auto | inherit
Initial value   auto
Applies to  positioned elements
Inherited   No

Try

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

Good read

  1. MDN : CSS Reference -left (Best IMHO)
  2. W3C : CSS/Properties/left
like image 43
NullPoiиteя Avatar answered Oct 02 '22 20:10

NullPoiиteя


You need to add position: absolute; to your CSS. left is used for absolute positioning.

In your case:

#inner {
   width: 400px;
   height: 300px;
   background-color: #090;
   position: absolute;
   left: 50%;
}
like image 38
Mateusz Rogulski Avatar answered Oct 02 '22 21:10

Mateusz Rogulski


Use:

margin-left: 50%;

Or:

position:relative;
left:50%;
like image 21
Tom Walters Avatar answered Oct 02 '22 21:10

Tom Walters


Try With the following :

HTML Part :

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

    </div>
</div>

CSS Part :

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

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

I think this may help you to resolve your problem.

like image 33
John Peter Avatar answered Oct 02 '22 20:10

John Peter