Клейкий нижній колонтитул Райана Фейта дуже приємний, проте я вважаю, що його основної структури бракує *.
Версія Flexbox
Якщо вам пощастило, що ви можете використовувати flexbox, не потребуючи підтримки старих браузерів, липкі колонтитули стають тривіально простими та підтримують нижній колонтитул динамічного розміру.
Фокус у тому, щоб колонтитули прилипали до дна за допомогою flexbox, полягає в тому, щоб інші елементи в тому ж контейнері згиналися вертикально. Для цього потрібен лише елемент обгортки на всю висоту з display: flexпринаймні одним братом та сестрою зі flexзначенням, більшим за 0:
CSS:
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#main-wrapper {
display: flex;
flex-direction: column;
min-height: 100%;
}
article {
flex: 1;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#main-wrapper {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
min-height: 100%;
}
article {
-webkit-box-flex: 1;
-ms-flex: 1;
flex: 1;
}
header {
background-color: #F00;
}
nav {
background-color: #FF0;
}
article {
background-color: #0F0;
}
footer {
background-color: #00F;
}
<div id="main-wrapper">
<header>
here be header
</header>
<nav>
</nav>
<article>
here be content
</article>
<footer>
here be footer
</footer>
</div>
Якщо ви не можете використовувати flexbox, моя основна структура вибору:
<div class="page">
<div class="page__inner">
<header class="header">
<div class="header__inner">
</div>
</header>
<nav class="nav">
<div class="nav__inner">
</div>
</nav>
<main class="wrapper">
<div class="wrapper__inner">
<div class="content">
<div class="content__inner">
</div>
</div>
<div class="sidebar">
<div class="sidebar__inner">
</div>
</div>
</div>
</main>
<footer class="footer">
<div class="footer__inner">
</div>
</footer>
</div>
</div>
Що далеко не все так далеко:
<div id="main-wrapper">
<header>
</header>
<nav>
</nav>
<article>
</article>
<footer>
</footer>
</div>
Фокус, щоб закріпити нижній колонтитул, полягає в тому, щоб нижній колонтитул прикріпився до нижньої оббивки його вміщуючого елемента. Для цього потрібно, щоб висота нижнього колонтитула була статичною, але я виявив, що нижній колонтитул зазвичай має статичну висоту.
HTML:
<div id="main-wrapper">
...
<footer>
</footer>
</div>
CSS:
#main-wrapper {
padding: 0 0 100px;
position: relative;
}
footer {
bottom: 0;
height: 100px;
left: 0;
position: absolute;
width: 100%;
}
#main-wrapper {
padding: 0 0 100px;
position: relative;
}
footer {
bottom: 0;
height: 100px;
left: 0;
position: absolute;
width: 100%;
}
header {
background-color: #F00;
}
nav {
background-color: #FF0;
}
article {
background-color: #0F0;
}
footer {
background-color: #00F;
}
<div id="main-wrapper">
<header>
here be header
</header>
<nav>
</nav>
<article>
here be content
</article>
<footer>
here be footer
</footer>
</div>
Коли нижній колонтитул прикріплений до #main-wrapper, тепер вам потрібно #main-wrapperмати принаймні висоту сторінки, якщо тільки її діти не довші. Це робиться шляхом #main-wrapperє min-heightз 100%. Ви також повинні пам’ятати, що його батьки htmlтакож bodyповинні мати висоту сторінки.
CSS:
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#main-wrapper {
min-height: 100%;
padding: 0 0 100px;
position: relative;
}
footer {
bottom: 0;
height: 100px;
left: 0;
position: absolute;
width: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#main-wrapper {
min-height: 100%;
padding: 0 0 100px;
position: relative;
}
footer {
bottom: 0;
height: 100px;
left: 0;
position: absolute;
width: 100%;
}
header {
background-color: #F00;
}
nav {
background-color: #FF0;
}
article {
background-color: #0F0;
}
footer {
background-color: #00F;
}
<div id="main-wrapper">
<header>
here be header
</header>
<nav>
</nav>
<article>
here be content
</article>
<footer>
here be footer
</footer>
</div>
Звичайно, ви повинні поставити під сумнів моє судження, оскільки цей код змушує нижній колонтитул падати внизу сторінки, навіть коли вмісту немає. Останній фокус полягає в тому, щоб змінити модель коробки, яка використовується таким #main-wrapperчином, щоби min-heightof 100%включала 100pxпрокладку.
CSS:
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#main-wrapper {
box-sizing: border-box;
min-height: 100%;
padding: 0 0 100px;
position: relative;
}
footer {
bottom: 0;
height: 100px;
left: 0;
position: absolute;
width: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
#main-wrapper {
box-sizing: border-box;
min-height: 100%;
padding: 0 0 100px;
position: relative;
}
footer {
bottom: 0;
height: 100px;
left: 0;
position: absolute;
width: 100%;
}
header {
background-color: #F00;
}
nav {
background-color: #FF0;
}
article {
background-color: #0F0;
}
footer {
background-color: #00F;
}
<div id="main-wrapper">
<header>
here be header
</header>
<nav>
</nav>
<article>
here be content
</article>
<footer>
here be footer
</footer>
</div>
І ось у вас це, липкий нижній колонтитул із оригінальною структурою HTML. Просто переконайтесь, що footer's heightдорівнює #main-wrapper' padding-bottom, і ви повинні бути встановлені.
* Причиною того, що я виявляю недолік у структурі Fait, є те, що вона встановлює елементи .footerand .headerна різні ієрархічні рівні, додаючи непотрібний .pushелемент.