详解Sticky Footer 绝对底部的两种套路

<!-- 1.经典套路 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1.0, user-scalable=0">
    <title>垂直伸缩</title>
    <style type="text/css">
        html,body {
		  height: 100%;
		  margin: 0; 
		  padding: 0;
		}
		p { margin: 0; }
		body > .wrap {
		  min-height: 100%;
		}
		     
		.content {
		  /* padding-bottom 等于 footer 的高度 */
		  padding-bottom: 60px;
		}
		     
		.footer {
		  width: 100%;
		  height: 60px;
		  /* margin-top 为 footer 高度的负值 */
		  margin-top: -60px;
		}
    </style>
</head>
<body>
    <div class="wrap">
	  <div class="content">
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	    <p>填充内容</p>
	  </div>
	</div>
	<div class="footer">
	  <p>这是页脚</p>
	</div>
</body>
</html>

<!-- 2.flex布局 -->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1.0, user-scalable=0">
    <title>垂直伸缩</title>
    <style type="text/css">
    	html,body {
		  height: 100%;
		  margin: 0; 
		  padding: 0;
		}
        .wrap {
		  	display: flex;
		  	flex-direction: column;
		  	height: 100%;
		}
		.wrap .content {
			flex: 1;
			background-color: red;
		}	
		.wrap .content p { text-align: center; }
		.wrap .footer {
			height: 60px;
			background-color: blue;
		}
		.wrap .footer p {
			text-align: center;
		}
    </style>
</head>
<body>
	<div class="wrap">
	  <div class="content">
	    <p>填充内容</p>
	  </div>
	  <div class="footer">
		  <p>这是页脚</p>
		</div>
	</div>
</body>
</html>

效果展示: 输入图片说明

猜你喜欢

转载自my.oschina.net/shuaihong/blog/1627686