標簽:css-常用技巧
下面的 css 和 js 是本文章案例中的公共代碼段,默認下面每個小dom都引用了這兩段代碼
* {
padding: 0;margin: 0;
box-sizing: border-box;
}
html body .hidden {display: none;}
.color-ff7d7d {background-color: #ff7d7d;}
.color-fbbb44 {background-color: #fbbb44;}
.color-beb1cd {background-color: #beb1cd;}
.color-7db9a9 {background-color: #7db9a9;}
.clearfix::after,
.clearfix::before {
display: table;
content: "";
}
.clearfix::after {
clear: both;
}
document.querySelector('#btn').addEventListener('click', function () {
document.querySelector('.wrap .content-text').classList.toggle('hidden');
}, false);
1. 這種 stick footer 的方式會增加一個多余的元素:即下面的div.push元素
body {
font-size: 30px;
line-height: 1.5;
}
body,
html {
height: 100%;
}
.wrap {
height: auto;
min-height: 100%;
margin-bottom: -60px;
overflow: auto;
}
.footer,
.wrap .push {
height: 60px;
overflow: hidden;
}
.footer {
clear: both;
}
<body>
<div class="wrap color-ff7d7d">
<div class="content clearfix color-fbbb44">
<button id="btn" style="display: block;padding: .2em;font: inherit;">顯示/隱藏主體內(nèi)容</button>
<h1 style="font-size: inherit;color: red;">這種 stick footer 的方式會增加一個多余的元素:即下面的div.push元素</h1>
<div class="content-text">
可以在這里添加一個超過視口高度的容器看看效果......
</div>
</div>
<div class="push color-beb1cd"></div>
</div>
<footer class="footer color-7db9a9">
<p>這里是footer</p>
</footer>
</body>
2. footer元素 負margin-top 版本
body {
font-size: 30px;
line-height: 1.5;
}
html,
body {
height: 100%;
}
.wrap {
height: auto;
min-height: 100%;
padding-bottom: 60px;
overflow: auto;
}
.footer {
position: relative;
height: 60px;
margin-top: -60px;
overflow: hidden;
clear: both;
}
<body>
<div class="wrap color-ff7d7d">
<div class="content clearfix color-fbbb44">
<button id="btn" style="display: block;padding: .2em;font: inherit;">顯示/隱藏主體內(nèi)容</button>
<h1 style="font-size: inherit;color: red;">footer元素 負margin-top 版本</h1>
<div class="content-text">
可以在這里添加一個超過視口高度的容器看看效果......
</div>
</div>
</div>
<footer class="footer color-7db9a9">
<p>這里是footer</p>
</footer>
</body>
3. calc版本
body {
font-size: 30px;
line-height: 1.5;
}
.wrap {
min-height: calc(100vh - 60px);
height: auto;
overflow: auto;
}
.footer {
position: relative;
height: 60px;
overflow: hidden;
clear: both;
}
<body>
<div class="wrap color-ff7d7d">
<div class="content color-fbbb44">
<button id="btn" style="display: block;padding: .2em;font: inherit;">顯示/隱藏主體內(nèi)容</button>
<h1 style="font-size: inherit;color: red;">calc版本</h1>
<div class="content-text">
可以在這里添加一個超過視口高度的容器看看效果......
</div>
</div>
</div>
<footer class="footer color-7db9a9">
<p>這里是footer</p>
</footer>
</body>
4. flex 版本
body {
font-size: 30px;
line-height: 1.5;
}
body,
html {
height: 100%;
}
.wrap {
display: flex;
flex-direction: column;
min-height: 100%;
}
.wrap .content {
flex-grow: 1;
}
.wrap .footer {
clear: both;
}
.color-ff7d7d {
background-color: #ff7d7d;
}
.color-fbbb44 {
background-color: #fbbb44;
}
.color-7db9a9 {
background-color: #7db9a9;
}
<body>
<!-- footer元素要包含于div.wrap中 -->
<div class="wrap color-ff7d7d">
<div class="content color-fbbb44">
<button id="btn" style="display: block;padding: .2em;font: inherit;">顯示/隱藏主體內(nèi)容</button>
<h1 style="font-size: inherit;color: red;">flex 版本</h1>
<div class="content-text">
可以在這里添加一個超過視口高度的容器看看效果......
</div>
</div>
<footer class="footer color-7db9a9">
<p>這里是footer</p>
</footer>
</div>
</body>
5. position: fixed 版本
/* 第一步設(shè)置盒子為滿屏大小 */
body {
font-size: 30px;
line-height: 2;
}
.wrap {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
overflow: auto;
}
/* 第二步子盒子設(shè)置最小高度且清除浮動 給一個padding-bottom 等于footer 避免內(nèi)容被footer遮蓋*/
.wrap .content {
min-height: 100%;
padding-bottom: 100px;
}
/* 第三步footer的height和margin-top要相等 */
.wrap .footer {
position: relative;
height: 100px;
margin-top: -100px;
clear: both;
}
<body>
<div class="wrap color-ff7d7d">
<div class="content clearfix color-fbbb44">
<button id="btn" style="display: block;padding: .2em;font: inherit;">顯示/隱藏主體內(nèi)容</button>
<h1 style="font-size: inherit;color: red;">position: fixed 版本</h1>
<div class="content-text">
可以在這里添加一個超過視口高度的容器看看效果......
</div>
</div>
<footer class="footer color-7db9a9">這是footer區(qū)域</footer>
</div>
</body>