Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS responsive absolute position between elements

I will like to get help please with building a responsive design.
The thing is that I don't know how to position elements as absolute but keep the same distance from top proportions between them.

Here's a link to an example where you can resize the window width and see that the two elements are moving away from each other instead of always keep the same space between them from top. So what I'm looking for is to kind of faking scaling of the whole thing so it will only get smaller/larger but look always the same.

How can I make the elements to go up and shrink the space from top when window resize please?

http://jsfiddle.net/QV6DR/

.container {
    width: 100%;
    height: 1000px;
    position: relative;
    background: #eee;
}

.container div {
    height: 0;
    position: absolute;
    background: #ccc;
}

.elm1 {
    width: 20%;
    padding-bottom: 20%;
    top: 20%;
    left: 5%;
}

.elm2 {
    width: 30%;
    padding-bottom: 30%;
    top: 40%;
    right: 10%;
}

enter image description here

like image 240
Gil Goldshlager Avatar asked Nov 08 '13 16:11

Gil Goldshlager


People also ask

How do you position an element with an absolute position?

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.

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.

Can I use display flex with position absolute?

Chen Hui Jing notes that when you absolutely position a flex item, it's no longer part of the flex layout. Except… it kinda is a little bit. If you make the child position: absolute; but don't apply any top/right/bottom/left properties, then flexbox alignment will still apply to it.


1 Answers

Because your container has a height of 1000px and your elements are positioned 20% relative to the top of the container(which is always 200px), they wouldn't be able to shift up when the browser window is resized.

If you change the container styles to the following:

.container {
    width: 100%;
    height: 100%;
    position: absolute;
    background: #eee;
} 

The elements will shift up when your browser window is resized vertically.

I believe the only way to shift them up vertically without resizing the window vertically, would be by using media queries and modifying the top: 40%; styles on your elements.

Here's the fiddle without media queries.

like image 121
Mathias Rechtzigel Avatar answered Oct 28 '22 11:10

Mathias Rechtzigel