2016-02-06 59 views
6

的底部的CSS顶部对齐考虑这个HTML子容器的父容器

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

我想要做的就是位置child顶部的parent底部。

这里是我的CSS迄今:

.parent { 
    position: relative; 
} 

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

什么,这达到是这样的:

Example 1

我想实现的是:

Example 2

恳求se注意:我不知道父容器或子容器的高度,并不真正想设置任意高度,我不想恢复使用JavaScript。

+0

你就不能折腾''
那里,并在必要时设置该BR填充和保证金? –

回答

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

.child { 
    position: absolute; 
    top: 100%; 
    left: 0; 
    background-color: #00F; 
} 
+1

我现在觉得很愚蠢,'top:100%'是我一直在寻找的东西 – MrCarrot

2

您可以transform: translateY(100%)

平移Y() CSS功能上飞机垂直移动的元素做到这一点。这个转换的特点是定义了垂直移动的多少。
translateY(ty)快捷方式对于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>