Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

css relative positioning not working in chrome

I have a problem with relative positioning in Google Chrome The following code works fine in IE but not in Chrome. Relative positioning is relative to the normal position of an element. The normal postion of the red box is right under the black box. If I add a 10% space the red box should appear 10% under the normal position.

jsfiddle

Html

 <body>
 <div id="outer">
     <div id="inner1">
     </div>
     <div id="inner2">
     </div>
 </div>
 </body

Css

 #outer
 {
     position:absolute;     
     left:20%;
     right:20%;
     bottom:20%;
     top:20%;
     background-color:Blue;
  }

  #inner1
  {
      position:relative;
      width:20%;
      height:20%;
      background-color:Black; 
   }

   #inner2
   {
       position:relative;
       top:10%;   
       width:20%;
       height:20%;
       background-color:Red; 
   }
like image 496
user3120396 Avatar asked Dec 20 '13 11:12

user3120396


People also ask

How do I change a relative position in CSS?

Relative Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

How does position relative work in CSS?

An element with position: relative; is positioned relative to its normal position. Setting the top, right, bottom, and left properties of a relatively-positioned element will cause it to be adjusted away from its normal position. Other content will not be adjusted to fit into any gap left by the element.

How do you set position of an element relative to another?

In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.

Should I use position absolute or relative?

If you specify position:relative, then you can use top or bottom, and left or right to move the element relative to where it would normally occur in the document. Position Absolute: When you specify position:absolute, the element is removed from the document and placed exactly where you tell it to go.


2 Answers

Ok, just realized myself now, what the problem of this is:

  • the % for the top value is always referring the height of the parent element. Since you didn't set a height it is zero; if you set a height on the parent element everything is working as you wished...

see this jsfiddle, i just added

height: 60%

to the parent css.

like image 62
Kjell Avatar answered Sep 18 '22 08:09

Kjell


For relative positioning to work the parent should have a size:

width: 100%;
height: 100%;

check the result in this fiddle

like image 22
Wilt Avatar answered Sep 20 '22 08:09

Wilt