前台开发入门到实战:CSS三栏布局的5种方法详解
题目:假设高度已知,请写出三栏布局,其中左栏、右栏宽度各为300px,中间自适应.
三栏布局的5种方案
这是一道经典的面试题,下面记录了css布局的5种方法。
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>三栏布局</title> <style> * { margin: 0; padding: 0; } </style></head><body><!--5种处理方案--></body>1. 浮动处理方案
<!-- 1\. 浮动处理方案 --> <scetion class="layout float"> <!-- 样式 --> <style media="screen"> .layout.float article div { min-height: 80px; } .layout.float .left { width: 300px; background-color: red; float: left; } .layout.float .center { background-color: yellow; } .layout.float .right { width: 300px; background-color: blue; float: right; } </style> <!-- 结构 --> <!-- 注意:结构中 右浮动的div肯定要写在 center 的前面,否则无效. --> <h2>三栏布局</h2> <article class="left-ritgh-center"> <div class="left"></div> <div class="right"></div> <div class="center"> <h2>浮动处理方案</h2> 1.这是三栏布局的浮动处理方案; 2.这是三栏布局的浮动处理方案; 3.这是三栏布局的浮动处理方案; 4.这是三栏布局的浮动处理方案; 5.这是三栏布局的浮动处理方案; 6.这是三栏布局的浮动处理方案; </div> </article> </scetion>2. 绝对定位处理方案
<!-- 2\. 绝对定位处理方案 --> <section class="layout absolute"> <!-- 样式 --> <style> .layout.absolute article div { min-height: 80px; position: absolute; } .layout.absolute .left { width: 300px; background-color: red; left: 0; } .layout.absolute .center { background-color: yellow; left: 300px; right: 300px; } .layout.absolute .right { width: 300px; background-color: blue; right: 0; } </style> <!-- 结构 --> <h1>三栏布局</h1> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h2>绝对定位处理方案</h2> 1.这是三栏布局的绝对定位处理方案; 2.这是三栏布局的绝对定位处理方案; 3.这是三栏布局的绝对定位处理方案; 4.这是三栏布局的绝对定位处理方案; 5.这是三栏布局的绝对定位处理方案; 6.这是三栏布局的绝对定位处理方案; </div> <div class="right"></div> </article> </section>3. flexbox 处理方案
<!-- 3\. flexbox处理方案 --> <section class="layout flexbox"> <!-- 样式 --> <style> /* flexbox处理方案在浏览器中显示时被上面的绝对定位处理方案挡住了,设置一个margin-top */ .layout.flexbox { margin-top: 110px; } /* 设置最低高度 */ .layout.flexbox article div { min-height: 80px; } .layout.flexbox article { display: flex; } .layout.flexbox .left { width: 300px; background-color: red; } .layout.flexbox .center { /*center部分增长 使整行填充*/ flex: 1; background-color: yellow; } .layout.flexbox .right { width: 300px; background-color: blue; } </style> <!-- 结构 --> <h1>三栏布局</h1> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h2>flexbox处理方案</h2> 1.这是三栏布局的flexbox处理方案; 2.这是三栏布局的flexbox处理方案; 3.这是三栏布局的flexbox处理方案; 4.这是三栏布局的flexbox处理方案; 5.这是三栏布局的flexbox处理方案; 6.这是三栏布局的flexbox处理方案; </div> <div class="right"></div> </article> </section>4. 表格布局处理方案
<!-- 4\. 表格布局处理方案 --> <section class="layout table"> <!-- 样式 --> <style> .layout.table .left-center-right { width: 100%; min-height: 80px; display: table; } .layout.table .left-center-right>div { display: table-cell; } .layout.table .left { width: 300px; background-color: red; } .layout.table .center { background-color: yellow; } .layout.table .right { width: 300px; background-color: blue; } </style> <!-- 结构 --> <h1>三栏布局</h1> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h2>表格布局处理方案</h2> 1.这是三栏布局的表格布局处理方案; 2.这是三栏布局的表格布局处理方案; 3.这是三栏布局的表格布局处理方案; 4.这是三栏布局的表格布局处理方案; 5.这是三栏布局的表格布局处理方案; 6.这是三栏布局的表格布局处理方案; </div> <div class="right"></div> </article> </section>5. 网格布局处理方案
<!-- 5\. 网格布局处理方案 --> <section class="layout grid"> <!-- 样式 --> <style> .layout.grid .left-center-right { width: 100%; display: grid; /* 网格行高 */ grid-template-rows: 100px; /* 网格列宽 */ grid-template-columns: 300px auto 300px; } .layout.grid .left { background-color: red; } .layout.grid .center { background-color: yellow; } .layout.grid .right { background-color: blue; } </style> <!-- 结构 --> <h1>三栏布局</h1> <article class="left-center-right"> <div class="left"></div> <div class="center"> <h2>网格布局处理方案</h2> 1.这是三栏布局的网格布局处理方案; 2.这是三栏布局的网格布局处理方案; 3.这是三栏布局的网格布局处理方案; 4.这是三栏布局的网格布局处理方案; 5.这是三栏布局的网格布局处理方案; 6.这是三栏布局的网格布局处理方案; </div> <div class="right"></div> </article> </section>将浏览器窗口压窄,可以看到变化。因为上面的代码中设置的高度是min-width,而不是设置的固定高度width,所以现在看到的也就是高度不固定的情况。
5种布局方案在高度不固定的情况下呈现出不同的效果的分析
- 浮动处理方案中:center部分会被内容撑高并向两边扩充,两边盒子的大小不受影响。
- 绝对定位处理方案中:center部分会被内容撑高,不向两边扩充,两边盒子大小不受影响。
- flexbox处理方案中:center部分会被内容撑高,并且两边的盒子与center高度始终保持一致。
这是由于在flex布局中,align-items属性默认为stretch,假如设置为:align-items: center;或者align-items: start;或者align-items: end;或者其余值,那么就不会自动保持一样高。
- 表格布局处理方案中:center部分会被内容撑高,并且两边的盒子与center高度始终保持一致。
- 网格布局处理方案中:盒子大小都不变化,文字直接超出center部分。
自己是一个五年的前台工程师,希望本文对你有帮助!
这里推荐一下我的前台学习交流扣qun:731771211 ,里面都是学习前台的,假如你想制作酷炫的网页,想学习编程。自己整理了一份2019最全面前台学习资料,从最基础的HTML+CSS+JS【炫酷特效,游戏,插件封装,设计模式】到手机端HTML5的项目实战的学习资料都有整理,送给每一位前台小伙伴,每天分享技术
点击:加入
说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 前台开发入门到实战:CSS三栏布局的5种方法详解
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 前台开发入门到实战:CSS三栏布局的5种方法详解