css三列自适应布局怎么写?教你三种方法
Admin 2021-04-26 群英技术资讯 1113 次浏览
为了按照常规WEB布局,这里全部采用拥有header和footer模式进行左中右布局编写。
实现思路
常规思路,使左右两个Aside分别浮动至左右两侧即可
代码实现
<!-- HTML部分 -->
<div class="container">
<!-- 顶部Header -->
<header>这里是头部</header>
<!-- 中间aside及content -->
<div class="middle clearfix">
<aside class="left"></aside>
<aside class="right"></aside>
<!-- 中间content显示内容 为了防止将右侧挤下去故放置在右侧栏下方 -->
<div class="content">这里是内容</div>
</div>
<!-- 底部Footer -->
<footer></footer>
</div>
<!-- CSS部分 -->
<style lang="scss">
* {
margin: 0;
padding: 0;
}
.clearfix {
zoom: 1;
&::after {
display: block;
content: ' ';
clear:both
}
}
html, body {
width: 100%;
height: 100%
}
.container {
width: 100%
height: 100%
header {
height: 80px;
background: rgba(0, 0, 0, 0.5)
}
footer {
height: 80px;
background: rgba(0, 0, 0, 0.5)
}
.middle {
height: calc(100% - 80px - 80px)
aside {
height: 100%;
width: 300px;
background: rgba(156, 154, 249, 1)
}
.left {
float: left
}
.right: {
float: right
}
}
}
}
</style>
实现效果

实现思路
为中间三栏父级赋予position: relative,赋予左右Aside position: absolute,并且分别赋予left: 0 right: 0 width:自定义值,赋予中间content left,right 分别等于左右width即可
代码实现
<!-- HTML相关代码 -->
<div class="container">
<!-- 顶部Header -->
<header></header>
<div class="middle">
<!-- 左侧Aside -->
<aside class="left"></aside>
<!-- 中间content内容 -->
<div class="content">这里是内容</div>
<!-- 右侧Aside -->
<aside class="right"></aside>
</div>
<!-- 底部Footer -->
<footer></footer>
</div>
<!-- CSS相关代码 -->
<style lang="scss">
* {
margin: 0;
padding: 0
}
html, body {
width: 100%;
height: 100%
}
.container {
width: 100%;
height: 100%;
header {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
footer {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
.middle {
height: calc(100% - 80px - 80px);
position: relative;
aside,
.content {
position: absolute;
}
.left {
width: 300px;
background: rgba(156, 154, 249, 1);
left: 0;
height: 100%;
}
.right {
width: 300px;
background: rgba(156, 154, 249, 1);
right: 0;
height: 100%;
}
.content {
left: 300px;
right: 300px;
}
}
}
</style>
实现效果

实现思路
赋予左中右三列父级display: flex,赋予左右Aside width自定义,赋予中间content flex:1即可
代码实现
<!-- HTML相关代码 -->
<div class="container">
<!-- 顶部Header -->
<header></header>
<div class="middle">
<!-- 左侧Aside -->
<aside class="left"></aside>
<!-- 中间content内容 -->
<div class="content">这里是内容</div>
<!-- 右侧Aside -->
<aside class="right"></aside>
</div>
<!-- 底部Footer -->
<footer></footer>
</div>
<!-- CSS部分 -->
<style lang="scss">
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.container {
header {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
footer {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
.middle {
display: flex;
height: calc(100% - 80px - 80px);
aside {
width: 300px;
background: rgba(156, 154, 249, 1);
}
.content: {
flex: 1;
}
}
}
</style>
实现效果

实现思路
赋予左中右三列父级display: table, width: 100%,分别赋予左中右三列display: table-cell,分别赋予左右Aside width即可。
代码实现
<!-- HTML相关代码 -->
<div class="container">
<!-- 顶部Header -->
<header></header>
<div class="middle">
<!-- 左侧Aside -->
<aside class="left"></aside>
<!-- 中间content内容 -->
<div class="content">这里是内容</div>
<!-- 右侧Aside -->
<aside class="right"></aside>
</div>
<!-- 底部Footer -->
<footer></footer>
</div>
<!-- CSS部分 -->
<style lang="scss">
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.container {
header {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
footer {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
.middle {
display: table;
width: 100%
height: calc(100% - 80px - 80px);
aside {
width: 300px;
display: table-cell;
background: rgba(156, 154, 249, 1);
}
.content: {
display: table-cell;
}
}
}
</style>
实现效果

实现思路
赋予左中右三列父级display: grid,并且使用grid-template-columns: 300px auto 300px,将其分为宽为300px、auto、300px三列布局即可。
代码实现
<!-- HTML相关代码 -->
<div class="container">
<!-- 顶部Header -->
<header></header>
<div class="middle">
<!-- 左侧Aside -->
<aside class="left"></aside>
<!-- 中间content内容 -->
<div class="content">这里是内容</div>
<!-- 右侧Aside -->
<aside class="right"></aside>
</div>
<!-- 底部Footer -->
<footer></footer>
</div>
<!-- CSS部分 -->
<style lang="scss">
* {
margin: 0;
padding: 0;
}
html, body {
width: 100%;
height: 100%;
}
.container {
header {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
footer {
height: 80px;
background: rgba(0, 0, 0, 0.5);
}
.middle {
display: grid;
grid-template-columns: 300px auto 300px;
height: calc(100% - 80px - 80px);
aside {
background: rgba(156, 154, 249, 1);
}
}
}
</style>
实现效果

文本介绍了五种css实现三列自适应布局的方法,有具体的代码示例,希望对大家学习CSS布局有帮助。更多CSS布局内容大家可以关注其他文章。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
HTML中form表单标签的简单使用是怎样?今天来学习下关于HTML中的form表单标签的相关内容,在HTML标记语言中经常用到,如果你是小白的话,小编接下来分享的内容你一定不要错过了。
HTML列表的主要标记怎样表示,用法是什么?在html页面中,列表可以起到提纲写领的作用。列表分为两种类型,一是有序列表,一是无序列表。前者用项目符号来标记无序的项目,而后者则使用编号来记录项目的顺序。
在前端开发中我们会经常提到渲染这个词,CSS网页渲染是浏览器工作,对于渲染简单的理解就是展示,将网站后台的数据渲染给用户看。我们能通过提升网页渲染速度来提升网页速度。
这篇文章主要介绍了使用纯CSS实现3D旋转效果的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
这篇文章我们来了解CSS样式层叠的相关内容,在使用CSS样式层叠时,有一些规则及注意的事项是要留意的,对此本文就给大家来介绍一下,对大家学习和理解CSS样式层叠会有一定的帮助,有需要的朋友可以参考,接下来就跟随小编来一起学习一下吧!
成为群英会员,开启智能安全云计算之旅
立即注册Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008