css如何让高度自适应
时间:2021-08-09 来源:互联网
今天PHP爱好者给大家带来css让高度自适应的方法:1、给html元素设置“display:table;”样式,给body元素设置“display:table-cell;”样式即可。2、使用flex布局。希望对大家有所帮助。
本教程操作环境:windows7系统、CSS3&&HTML5版、Dell G3电脑。
在写css静态页面的时候让Html的高度自适应屏幕高度是一个常见的需求,比如你有一个需要置底的bottom按钮,需要在内容不足一屏的时候显示在屏幕的底部,在内容超过一屏的时候显示在所有内容的底部。
效果图:
CSS的做法
方法1:
html {
display: table;
}
body {
display: table-cell;
}
方法2:使用flex布局:
<p class="container">
<header></header>
<content></content>
<footer></footer>
</p>
.container {
display: flex;
min-
flex-direction: column;
}
header {
background: #cecece;
min-
}
content {
background: #bbbbbb;
flex: 1; /* 1 代表盡可能最大,會自動填滿除了 header footer 以外的空間 */
}
footer {
background: #333333;
min-
}
JS的做法
css的做法有时候会在定位的时候造成一些麻烦,可以尝试使用js去动态改变html的高度
基于zepto
$(document).ready(function(){
var windowHeight = $(window).height();
if($(this).height() < windowHeight){
$(this).height(windowHeight);
}
});
原生js
window.onload = function(){
var winHeight = 0;
if (window.innerHeight){
winHeight = window.innerHeight;
}else if ((document.body) && (document.body.clientHeight)){
winHeight = document.body.clientHeight;
}
var html = document.getElementsByTagName('html')[0];
if(document.body.offsetHeight < windowHeight){
html.style.height = windowHeight;
}
};
以上就是css如何让高度自适应的详细内容,更多请关注php爱好者其它相关文章!
-
天青梗是什么梗揭秘网络热词天青梗的由来和爆火原因 2025-09-15
-
天青色等烟雨是什么梗 揭秘周杰伦歌词背后的唯美意境 2025-09-15
-
七日世界月之狂欢9月25日将来袭-新异色异常物出场 2025-09-15
-
逆水寒神相怎么搭配-英雄团本木桩一键10万 2025-09-15
-
第五人格中秋活动9月25日开启-海量福利奖励将上线 2025-09-15
-
天山雪莲的梗是什么梗?揭秘稀有植物背后的网络爆笑梗真相 2025-09-15