css让相对高度自适应的方式:1、给html原素设定“height:100%;display:table;”款式,给body原素设定“display:table-cell;height:100%;”款式就可以。2、应用flex合理布局。
本实例教程作业环境:windows7系统软件、CSS3&&HTML5版、Dell G3计算机。
写css静态网页时让Html高度自适应屏幕相对高度是一个比较常见的要求,例如你有一个必须置底的bottom按键,必须在内容不够一屏时表明在屏幕底部,在内容超出一屏时展示在全部内容底部。
设计效果图:
CSS的处理方式
方式1:
html {
height: 100%;
display: table;
}
body {
display: table-cell;
height: 100%;
}
方式2:应用flex合理布局:
<div class="container">
<header></header>
<content></content>
<footer></footer>
</div>
.container {
display: flex;
min-height: 100vh;
flex-direction: column;
}
header {
background: #cecece;
min-height: 100px;
}
content {
background: #bbbbbb;
flex: 1; /* 1 意味着盡很有可能较大,會自動填滿除开 header footer 之外的空間 */
}
footer {
background: #333333;
min-height: 100px;
}
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怎么让相对高度自适应的具体内容,大量欢迎关注AdminJS其他类似文章!
声明:本文为原创,版权声明:本文采用[BY-NC-SA]协议进行授权,如无特别说明,转载必须注明本文地址!
原文地址:css怎么让相对高度自适应发布于2023-05-27 16:45:01