我想创建一个侧栏,年份显示在底部和顶部的链接列表。让div取其余的高度后,另一个div的高度低于它
- 的年文本应该留在底部,其高度应不
auto
绝对值(如30像素,1EM,10%等) - 顶部链接应该保持剩余高度内(屏幕高度 - 高度年文字)。
- 链接高度超过时,应提供滚动。
我已经成功实现了第2个,但不是第3个。当链接高度超过时,年从屏幕上隐藏。我使用table
来实现我指定的内容。 (我可以用display: table
,只是懒惰这里)
我想要实现这个只使用CSS,JavaScript的没有。
/* Some styles */
.side-bar table {
\t color: #fff;
}
.side-bar-links {
\t background-color: #48c;
\t padding-top: 15px;
}
.side-bar-links li:nth-child(even) {
\t background-color: rgba(0,0,0,0.1);
}
.side-bar-links a {
\t display: block;
\t color: #fff;
\t padding: 5px 15px;
\t text-decoration: none;
}
.side-bar-links a:hover {
\t background-color: rgba(0,0,0,0.2);
}
.year {
\t background-color: #338;
\t font-size: 36px;
\t font-weight: 700;
\t padding: 5px 15px;
}
/* Positioning */
.side-bar {
\t position: fixed;
\t left: 0;
\t top: 0;
\t bottom: 0;
}
.side-bar table {
\t height: 100%;
\t table-layout: fixed;
}
.side-bar table tr:last-child td {
\t height: 1px;
}
.side-bar table td {
\t vertical-align: baseline;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="side-bar">
<table>
\t <tr>
\t \t <td class="side-bar-links">
\t \t \t <ul class="list-unstyled">
\t \t \t \t \t \t \t \t <li><a href="#">> Link 1</a></li>
\t \t \t \t \t \t \t \t <li><a href="#">> Link 2</a></li>
\t \t \t \t \t \t \t \t <li><a href="#">> Link 3</a></li>
\t \t \t \t \t \t \t \t <li><a href="#">> Link 4</a></li>
\t \t \t \t \t \t \t \t <li><a href="#">> Link 5</a></li>
\t \t \t \t \t \t \t </ul>
\t \t </td>
\t </tr>
\t <tr>
\t \t <td class="year">
\t \t \t <b>2016</b>
\t \t </td>
\t </tr>
</table>
</div>
这是一个更容易千倍,如果你给year元素一个固定的高度。然后你可以做'.sidebar-links {height:calc(100% - [height of year element]); overflow-y:scroll; }' – Dom