Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting CSS top percent not working as expected

Tags:

css

I tried a responsive css layout,but "top:50%" can't work and the "left:50%" can. Why it happened?

<div style="position:relative;top:0%;left:0%;height:100%;width:100%">
    <div style="position:absolute;top:50%;left:50%;">
    </div>
</div>
like image 922
HornedReaper Avatar asked Jan 30 '15 14:01

HornedReaper


People also ask

Why is percentage height not working CSS?

You can't base a percentage height on a parent that has no height set otherwise the height should default to auto (content doesn't count as height either). Also you can't base a child's percentage height on a parent that has min or max height set either as it must be a real height.

Why is top not working in CSS?

To answer your question why top: 50% is not working, when you use property top on an element, the parent of that element needs to have a static height set in place. This should be in px or a unit other than percent.

How do I do a percentage in CSS?

The <percentage> data type consists of a <number> followed by the percentage sign ( % ). Optionally, it may be preceded by a single + or - sign, although negative values are not valid for all properties. As with all CSS dimensions, there is no space between the symbol and the number.

Does percentage work for height in CSS?

The percentage is calculated with respect to the height of the generated box's containing block. If the height of the containing block is not specified explicitly (i.e., it depends on content height), and this element is not absolutely positioned, the value computes to auto .


2 Answers

Define a dimension for the parent container, e.g. div:

<div style="border: 2px solid red;position:relative;top:0%;left:0%;height:200px;width:300px">
    <div style="border: 2px solid green;position:absolute;top:50%;left:50%;height:50%;width:50%">
    </div>
</div>

Or

Another way is to just stretch the parent container, i.e. div, by its top, bottom, left, and right properties. Like this:

<div style="border: 2px solid red;position: absolute;top: 0px;bottom: 0px;left:0px;right:0px;">
    <div style="border: 2px solid green;position:absolute;top:50%;left:50%;height:50%;width:50%">
    </div>
</div>
like image 198
user3030212 Avatar answered Oct 13 '22 01:10

user3030212


Consider your original HTML:

html, body {
  height: 100%;
}
<div style="position:relative;top:0%;left:0%;height:100%;width:100%">
  <div style="position:absolute;top:50%;left:50%;">test</div>
</div>

The inner/child div has position: absolute, so it is out of the content flow of the parent element and will not contribute to the height or width of the parent element.

The parent div is in the content flow, but has no content, so its intrinsic height would be zero. However, you specified height: 100%, but this will not do anything because the div has no height reference on which to base a computed value. So the computed height value for the parent div is zero.

As a result, the child element's top offset computes to 50% of zero, so it is positioned at the top edge of the parent block.

You would need either to specify a height for the parent div or assign

html, body {height: 100%} 

as this would allow the div to compute its height based on the height of the body, which is based on the height of the html, which being 100%, takes that of the screen.

like image 26
Marc Audet Avatar answered Oct 12 '22 23:10

Marc Audet