當(dāng)我們?cè)趯戫撁鏁r(shí)經(jīng)常會(huì)遇到頁面內(nèi)容少的時(shí)候,footer會(huì)戳在頁面中間或什么?反正就是不在最底部顯示,反正就是很難看,下面要講的布局就是解決如何使元素粘住瀏覽器底部,
方法一:footer高度固定+絕對(duì)定位
html
<div class="dui-container"> <header>Header</header> <main>Content</main> <footer>Footer</footer> </div>
CSS
.dui-container{ position: relative; min-height: 100%; } main { padding-bottom: 100px; } header, footer{ line-height: 100px; height: 100px; } footer{ width: 100%; position: absolute; bottom: 0 }
方法二:在主體content上的下邊距增加一個(gè)負(fù)值等于底部高度
html
<header>Header</header> <main>Content</main> <footer>Footer</footer>
CSS
html, body { height: 100%; } main { min-height: 100%; padding-top: 100px; padding-bottom: 100px; margin-top: -100px; margin-bottom: -100px; } header, footer{ line-height: 100px; height: 100px; }
方法三:將頁腳的margin-top設(shè)為負(fù)數(shù)
html
<header>Header</header> <main>Content</main> <footer>Footer</footer>
CSS
main { min-height: 100%; padding-top: 100px; padding-bottom: 100px; } header, footer{ line-height: 100px; height: 100px; } header{ margin-bottom: -100px; } footer{ margin-top: -100px; }
方法四: 通過設(shè)置flex,將footer的margin-top設(shè)置為auto
html
<header>Header</header> <main>Content</main> <footer>Footer</footer>
CSS
body{ display: flex; min-height: 100vh; flex-direction: column; } header,footer{ line-height: 100px; height: 100px; } footer{ margin-top: auto; }
方法五: 通過函數(shù)calc()計(jì)算內(nèi)容的高度
html代碼
<header>Header</header> <main>Content</main> <footer>Footer</footer>
CSS代碼
main{ min-height: calc(100vh - 200px); /* 這個(gè)200px是header和footer的高度 */ } header,footer{ height: 100px; line-height: 100px; }
方法六: 通過設(shè)置flexbox,將主體main設(shè)置為flex
html
<header>Header</header> <main>Content</main> <footer>Footer</footer>
CSS代碼
body{ display: flex; min-height: 100vh; flex-direction: column; } main{ flex: 1 }
方法七: 使用grid布局
Html代碼
<header>Header</header> <main>Content</main> <footer>Footer</footer>
CSS代碼
html { height: 100%; } body { min-height: 100%; display: grid; grid-template-rows: auto 1fr auto; } .footer { grid-row-start: 3; grid-row-end: 4; }
方法八: display-*
html
<header>Header</header> <main>Content</main> <footer>Footer</footer>
CSS
body { min-height: 100%; display: table; width: 100%; } main { display: table-row; height: 100%; }
Copyright ? 2019- 91gzw.com 版權(quán)所有 湘ICP備2023023988號(hào)-2
違法及侵權(quán)請(qǐng)聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市萬商天勤律師事務(wù)所王興未律師提供法律服務(wù)