Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS relative + right (or bottom) almost NEVER work

I've been writing CSS for quite some time now.

I've noticed that

<div style="position: relative; right: 20%; bottom: 20%;">some text</div> 

never works!

relative positioning would work with left and top specified but not with right/bottom. Why?

A quick fix around this is to use "absolute" instead and specify right/bottom in pixels, but I need a reason.

Also, correct me if I'm wrong. Irrespective of whether the external container is positioned absolutely or relatively, does it not make much sense to position something "relative" to the boundaries of that container or should elements inside a container always be positioned "absolute"?

Thank you.

like image 805
A Sharma Avatar asked Apr 30 '13 01:04

A Sharma


People also ask

Is it bad to use position relative in CSS?

It's a bad idea imho as it changes the default behaviour of elements without clear indication and it will have unforseen consequences. If you really want to have most elements positioned relative, you might want to think about just making div and similar relative.

Why is position fixed not working?

Position fixed doesn't work with transform CSS property. It happens because transform creates a new coordinate system and your position: fixed element becomes fixed to that transformed element. To fix the problem you can remove transform CSS property from the parent element.

Is it bad to use absolute positioning CSS?

As long as you structure your HTML so that the elements follow a logical order that makes sense when rendered without CSS, there is no reason why using absolute positioning should be considered bad practice.

How do I fix the bottom element in CSS?

If position: absolute; or position: fixed; - the bottom property sets the bottom edge of an element to a unit above/below the bottom edge of its nearest positioned ancestor. If position: relative; - the bottom property makes the element's bottom edge to move above/below its normal position.


2 Answers

From Absolute vs. Relative - Explaining CSS Positioning

Relative positioning uses the same four positioning properties as absolute positioning. But instead of basing the position of the element upon the browser view port, it starts from where the element would be if it were still in the normal flow.
like image 70
oomlaut Avatar answered Sep 20 '22 15:09

oomlaut


Relative positioning does work with bottom/right values, just not the way you were expecting:

http://cssdesk.com/RX24j

Think about the position values on relative elements as margins, that the surrounding elements simply ignore. The "margins" will always move the element relative to it's previous position in the normal document flow, but remove it from the normal flow at the same time.

When out of the normal document flow, the surrounding elements act as if it were in it's original position in the normal flow... but it's not. This is why a relative element can overlap it's parent (like in Rel 1).

like image 27
user2313440 Avatar answered Sep 19 '22 15:09

user2313440