A sticky footer pattern is one where the footer of your page "sticks" to the bottom of the viewport in cases where the content is shorter than the viewport height. We'll look at a couple of techniques for creating one in this recipe.
The Sticky footer pattern needs to meet the following requirements:
注意
: In this example and the following one we are using a wrapper set to
min-height: 100%
in order that our live example works. You could also achieve this for a full page by setting a
min-height
of
100vh
on the
<body>
and then using it as your grid container.
In the above example we achieve the sticky footer using CSS Grid Layout. The
.wrapper
has a minimum height of
100%
which means it is as tall as the container it is in. We then create a single column grid layout with three rows, one row for each part of our layout.
Grid auto-placement will place our items in source order and so the header goes into the first auto sized track, the main content into the
1fr
track and the footer into the final auto sized track. The
1fr
track will take up all available space and so grows to fill the gap.
If you need compatibility with browsers that do not support Grid Layout you can also use Flexbox to create a sticky footer.
The flexbox example starts out in the same way, but we use
display:flex
而不是
display:grid
on the
.wrapper
; we also set
flex-direction
to
column
. Then we set our main content to
flex-grow: 1
and the other two elements to
flex-shrink: 0
— this prevents them from shrinking smaller when content fills the main area.
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
grid-template-rows
|
Chrome
完整支持
57
|
Edge
完整支持
16
|
Firefox
完整支持
52
|
IE
完整支持
10
Alternate Name
|
Opera
完整支持
44
|
Safari 完整支持 10.1 | WebView Android 完整支持 57 |
Chrome Android
完整支持
57
|
Firefox Android
完整支持
52
|
Opera Android
完整支持
43
|
Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
| Animation of tracks | Chrome 不支持 No | Edge 不支持 No | Firefox 完整支持 66 | IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 完整支持 66 | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
fit-content()
|
Chrome 完整支持 29 | Edge 完整支持 16 | Firefox 完整支持 51 | IE 不支持 No | Opera 完整支持 44 | Safari 完整支持 10.1 | WebView Android 完整支持 57 | Chrome Android 完整支持 57 | Firefox Android 完整支持 51 | Opera Android 完整支持 43 | Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
minmax()
|
Chrome
完整支持
57
|
Edge 完整支持 16 |
Firefox
完整支持
52
|
IE 不支持 No |
Opera
完整支持
44
|
Safari 完整支持 10.1 | WebView Android 完整支持 57 |
Chrome Android
完整支持
57
|
Firefox Android
完整支持
52
|
Opera Android
完整支持
43
|
Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
repeat()
|
Chrome
完整支持
57
|
Edge 完整支持 16 |
Firefox
部分支持
57
注意事项
|
IE 不支持 No |
Opera
完整支持
44
|
Safari 完整支持 10.1 | WebView Android 完整支持 57 |
Chrome Android
完整支持
57
|
Firefox Android
完整支持
52
|
Opera Android
完整支持
43
|
Safari iOS 完整支持 10.3 | Samsung Internet Android 完整支持 6.0 |
subgrid
|
Chrome 不支持 No | Edge 不支持 No |
Firefox
完整支持
71
|
IE 不支持 No | Opera 不支持 No | Safari 不支持 No | WebView Android 不支持 No | Chrome Android 不支持 No | Firefox Android 不支持 No | Opera Android 不支持 No | Safari iOS 不支持 No | Samsung Internet Android 不支持 No |
完整支持
部分支持
不支持
见实现注意事项。
用户必须明确启用此特征。
使用非标名称。
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
flex-direction
|
Chrome 完整支持 29 | Edge 完整支持 12 |
Firefox
部分支持
20
注意事项
|
IE
完整支持
11
|
Opera 完整支持 12.1 | Safari 完整支持 9 | WebView Android 完整支持 4.4 | Chrome Android 完整支持 29 |
Firefox Android
部分支持
20
注意事项
|
Opera Android 完整支持 12.1 | Safari iOS 完整支持 9 | Samsung Internet Android 完整支持 2.0 |
完整支持
部分支持
见实现注意事项。
用户必须明确启用此特征。
要求使用供应商前缀或不同名称。
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
flex-grow
|
Chrome 完整支持 29 | Edge 完整支持 12 |
Firefox
完整支持
20
注意事项
|
IE
完整支持
11
|
Opera 完整支持 12.1 | Safari 完整支持 9 | WebView Android 完整支持 4.4 | Chrome Android 完整支持 29 |
Firefox Android
完整支持
20
注意事项
|
Opera Android 完整支持 12.1 | Safari iOS 完整支持 9 | Samsung Internet Android 完整支持 2.0 |
| <0 animate 非标 | Chrome 完整支持 49 | Edge 完整支持 79 |
Firefox
完整支持
32
注意事项
|
IE 不支持 No | Opera 完整支持 36 | Safari 不支持 No | WebView Android 完整支持 49 | Chrome Android 完整支持 49 |
Firefox Android
完整支持
32
注意事项
|
Opera Android 完整支持 36 | Safari iOS 不支持 No | Samsung Internet Android 完整支持 5.0 |
完整支持
不支持
非标。预期跨浏览器支持较差。
见实现注意事项。
用户必须明确启用此特征。
使用非标名称。
要求使用供应商前缀或不同名称。
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
flex-shrink
|
Chrome 完整支持 29 | Edge 完整支持 12 |
Firefox
完整支持
20
注意事项
|
IE
完整支持
10
注意事项
|
Opera 完整支持 12.1 | Safari 完整支持 9 | WebView Android 完整支持 4.4 | Chrome Android 完整支持 29 |
Firefox Android
完整支持
20
注意事项
|
Opera Android 完整支持 12.1 | Safari iOS 完整支持 9 | Samsung Internet Android 完整支持 2.0 |
完整支持
见实现注意事项。
用户必须明确启用此特征。
要求使用供应商前缀或不同名称。