2017-02-09 26 views
4

二元我有一個簡單的ul元素具有較大的高度,我想在ul元素的底部最後兩個li元素和休息之上。在長個兒的UI元素如何在底部

你可以看到當前的實現here

代碼是這樣的:

.my-ul { 
 
    height: 90vh; 
 
    width: 40%; 
 
    background-color: grey; 
 
}
<div id="demo"> 
 
    <ul class="my-ul"> 
 
    <li>First Element</li> 
 
    <li>Second Element</li> 
 
    <li>Third Element</li> 
 
    <li>Fourth Element</li> 
 
    <li>Fifth Element</li> 
 
    </ul> 
 
</div>

我試圖從herehere解決方案,但這些都是not working

回答

8

解決方案我在這裏看到的是Flexbox的

ul { 
 
    display: flex; 
 
    flex-direction: column; 
 
    height: 300px; 
 
    justify-content: flex-start; 
 
} 
 

 
ul li:nth-last-child(2) { 
 
    margin-top: auto; 
 
}
<ul> 
 
    <li>first</li> 
 
    <li>first</li> 
 
    <li>first</li> 
 
    <li>first</li> 
 
    <li>first</li> 
 
    <li>first</li> 
 
</ul>

0

使用CSS選擇器的最後兩Elements.See下面的示例代碼:

<div id="demo"> 
    <ul class="my-ul"> 
    <li>First Element</li> 
    <li>Second Element</li> 
    <li>Third Element</li> 
    <li>Fourth Element</li> 
    <li>Fifth Element</li> 
    </ul> 
</div> 

.my-ul { 
    height: 90vh; 
    width: 40%; 
    background-color: grey; 
} 
li:last-child { 
    position: fixed; 
    top: 370px; 
} 
li:nth-child(4) { 
    position: fixed; 
    top: 350px; 
} 
+0

小提琴鏈接:http:// jsfiddle.net/gj0tb2fq/ – KiranPurbey

+0

'n-last-child'會更好,因爲你可能不確定有多少列表項。 –

+0

是的,對於較大的項目列表,使用「nth-last-child」更爲正確。 – KiranPurbey