Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Align top of child container to bottom of parent container

Tags:

html

css

Consider this HTML

<div class="parent">
    <a href="#">Parent</a>
    <div class="child">
        <a href="#">Child</a>
    </div>
</div>

What I want to do is position the top of child to the bottom of parent.

Here's my CSS so far:

.parent {
    position: relative;
}

.child {
    position: absolute;
    left: 0;
    bottom: 0;
}

What this achieves is this:

Example 1

What I want to achieve is this:

Example 2

Please note: I do not know the height of either the parent or child container, and don't really want to set an arbitrary height, and I don't want to revert to using JavaScript.

like image 510
MrCarrot Avatar asked Feb 06 '16 10:02

MrCarrot


People also ask

How do you move a container to the bottom in CSS?

Set the position of div at the bottom of its container can be done using bottom, and position property. Set position value to absolute and bottom value to zero to placed a div at the bottom of container.

How do I fix the container position in CSS?

Set everything up as you would if you want to position: absolute inside a position: relative container, and then create a new fixed position div inside the div with position: absolute , but do not set its top and left properties. It will then be fixed wherever you want it, relative to the container.


2 Answers

.parent {
    position: relative;
    background-color: #F00;
}

.child {
    position: absolute;
    top: 100%;
    left: 0;
    background-color: #00F;
}
like image 147
linktoahref Avatar answered Nov 10 '22 04:11

linktoahref


You can do this with transform: translateY(100%)

The translateY() CSS function moves the element vertically on the plane. This transformation is characterized by a <length> defining how much it moves vertically.
translateY(ty) is a shortcut for translate(0, ty).

enter image description here

.parent {
    position: relative;
    background: red;
    width: 50%;
    height: 50vh;
}

.child {
    position: absolute;
    left: 0;
    bottom: 0;
    background: blue;
    height: 100px;
    width: 100px;
    transform: translateY(100%);
}
<div class="parent">
    <a href="#">Parent</a>
    <div class="child">
        <a href="#">Child</a>
    </div>
</div>
like image 45
Nenad Vracar Avatar answered Nov 10 '22 05:11

Nenad Vracar