css 全局布局

104 阅读1分钟

1.理论

包含 上中下三部分,中间包含左边导航+右边内容

image.png

2.实现

已知

<html>
<head>
    <style>
        body{
            padding: 0;
            margin: 0;
        }
        .container{ 
            
        }
        .header{ 
            background-color: yellowgreen;
            height: 100px;
        }
        .content{  
            min-height: 500px; 
        }
        .content > .left{ 
            background-color: gainsboro;
            width: 200px;
        }
        .content > .right{ 
            background-color: rebeccapurple ; 
        }
        .footer{
            background-color: burlywood; 
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header"></div>
        <div class="content">
            <div class="left"></div>
            <div class="right"></div>
        </div>
        <div class="footer"></div>
    </div>
</body>
</html>

image.png

实现上下固定,中间内容左右布局,同时自适应高度

image.png

2.1 flexbox

 .container{ 
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
} 
.header{ 
    background-color: yellowgreen;
    height: 100px;
    width: 100%;
}
.content{  
    min-height: 500px;
    flex: 1;
    display: flex; 
}
.left{ 
    background-color: gainsboro;
    width: 200px;
    height: 100%;
}
.right{ 
    background-color: rebeccapurple ; 
    height: 100%;
    overflow: auto; 
    flex:1;
}
.footer{
    background-color: burlywood; 
    height: 100px;
}

2.2 grid

.container{ 
    display: grid;
    grid-template-rows: auto 1fr auto;
} 
.header{ 
    background-color: yellowgreen;
    height: 100px;
}
.content{  
    min-height: 500px; 
    display: grid;
    grid-template-columns: auto 1fr;
}
.content > .left{ 
    background-color: gainsboro;
    width: 200px; 
}
.content > .right{ 
    background-color: green ; 
}
.footer{
    background-color: burlywood; 
    height: 100px;
}