CSS实现中间自适应布局有几种方式
Admin 2022-05-21 群英技术资讯 897 次浏览
这篇文章主要介绍“CSS实现中间自适应布局有几种方式”的相关知识,下面会通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“CSS实现中间自适应布局有几种方式”文章能帮助大家解决问题。前言
在做页面时,我们往往会碰到页面布局相关的内容,面试时也经常会被问到,那么今天我就来总结一下关于页面布局的内容。
问题:如何实现三栏布局(高度固定,左中右的结构)
假设高度已知,请写出三栏布局,其中左右宽度均为300px,中间自适应。
看了上面的题目,有经验的人也许会觉得很简单,仔细想想,如果我们来写,能写出几种方案呢?一般都会想到两种吧,float和position定位,其实除了这两种外,还有3种可以写,下面我就来一一介绍一下:
方案一(float浮动)
<section class='layout float'>
<style>
.layout.float .left-right-center{
height: 100px;
}
.layout.float .left{
float: left;
width: 300px;
background-color: red;
}
.layout.float .right{
float: right;
width: 300px;
background-color: blue;
}
.layout.float .center{
background-color: yellow;
}
</style>
<article class="left-right-center">
<div class="left"></div>
<div class="right"></div>
<div class="center">我是中间的自适应元素--浮动</div>
</article>
</section>
方案二(绝对定位)
<section class="layout absolute">
<style>
.layout.absolute .left-center-right div{
position: absolute;
height: 100px;
}
.layout.absolute .left{
left: 0;
width: 300px;
background-color: red;
}
.layout.absolute .center{
left: 300px;
right: 300px;
background-color: yellow;
}
.layout.absolute .right{
right: 0;
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中间的自适应元素--绝对定位
</div>
<div class="right"></div>
</article>
</section>
方案三(flex布局)
<section class="layout flex">
<style>
.layout.flex .left-center-right{
display: flex;
height: 100px;
}
.layout.flex .left{
width: 300px;
background-color: red;
}
.layout.flex .center{
flex: 1;
background-color: yellow;
}
.layout.flex .right{
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中间的自适应元素--flex布局
</div>
<div class="right"></div>
</article>
</section>
方案四(table布局)
<section class="layout table">
<style>
.layout.table .left-center-right{
display: table;
height: 100px;
width: 100%;
}
.layout.table .left{
display: table-cell;
width: 300px;
background-color: red;
}
.layout.table .center{
display: table-cell;
background-color: yellow;
}
.layout.table .right{
display: table-cell;
width: 300px;
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中间的自适应元素--table
</div>
<div class="right"></div>
</article>
</section>
方案五(网格布局)
<section class="layout grid">
<style>
.layout.grid .left-center-right{
display: grid;
width: 100%;
grid-template-rows: 100px;/*每一格都是100px高*/
grid-template-columns: 300px auto 300px;/*左右两格都是300px,中间是自适应*/
}
.layout.grid .left{
background-color: red;
}
.layout.grid .center{
background-color: yellow;
}
.layout.grid .right{
background-color: blue;
}
</style>
<article class="left-center-right">
<div class="left"></div>
<div class="center">
我是中间的自适应元素--grid布局
</div>
<div class="right"></div>
</article>
</section>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
本篇文章通过举例的方式来为大家讲解下如何在html中绘制一条水平线段,具体内容看以下详细步骤,供有需要的朋友参考参考。
background-color设置元素的背景色。background-image属性用于为一个元素设置一个或者多个背景图像。background-repeat定义背景图像的重复方式。
CSS缩写规则有哪些,使用缩写有什么好处?一些朋友可能会遇到这方面的问题,对此在下文小编向大家来讲解一下,内容详细,易于理解,希望大家阅读完这篇能有收获哦,有需要的朋友就往下看吧!
本文将结合实例代码,介绍html5表单的required属性使用,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧
设置方法:1、利用“background-color”属性设置,只需要给行元素添加“background-color:颜色值;”样式即可;2、利用background属性设置,只需要给行元素添加“background:颜色值;”样式即可。
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008