Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relative parent, absolute positioning vertically by percentage?

Tags:

I'm trying to create a vertically positioned DIV by percentage. I have the parent container to set to relative and the content div set to absolute. This works fine when I position the content div with pixels, but when I try percentages the percentages are disregarded:

.container { position: relative; }   .content { position: absolute; left: 10%; top: 50%; }   <div class="container"><div class="content"> This is the content div. It should be 10% from the left of the container div. </div></div> 

The content div appears at the top of the page, disregarding the 50% vertical placement. What am I missing? Thanks in advance!

like image 250
user1801221 Avatar asked Nov 05 '12 20:11

user1801221


People also ask

Is absolute position relative to parent?

Absolute 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.

What is absolute and relative positioning?

position: relative places an element relative to its current position without changing the layout around it, whereas position: absolute places an element relative to its parent's position and changing the layout around it.

Should I use relative or absolute positioning?

As a special, use “relative” positioning with no displacement (just setting position: relative ) to make an element a frame of reference, so that you can use “absolute” positioning for elements that are inside it (in markup).

What is the difference between relative static and absolute positioning?

Static - this is the default value, all elements are in order as they appear in the document. Relative - the element is positioned relative to its normal position. Absolute - the element is positioned absolutely to its first positioned parent.


1 Answers

The absolutely positioned element is taken out of the natural flow of the document which means your container has zero height and width.

10% and 50% of that zero height and width are, of course, zero.

If you give your container a height and width, your percentage positions will start to work as you want.

Here is a working example.

.container { position: relative; width:500px; height:500px; }  
like image 174
Jamie Dixon Avatar answered Oct 14 '22 16:10

Jamie Dixon