一. 定宽 + 自适应
期望效果: 左侧宽度固定, 右侧宽度自适应
公共代码:
html:
<div class="parent"> <div class="left"> <p>left menu</p> </div> <div class="right"> <li>right item1</li> <li>right item2</li> <li>right item3</li> </div> </div>
css:
html, body, p, ul, li { margin: 0; padding: 0; } div.left { background: #d2e3e3; } div.right { background: #77DBDB; }
方案一: float
.left { float: left; width: 100px; } .right { margin-left: 100px; // 或 overflow: hidden }
方案二: table
.parent { display: table; width: 100%; table-layout: fixed; // https://blog.csdn.net/qq_36699230/article/details/80658742 .left, .right { display: table-cell; } .left { width: 100px; } }
方案三: flex
.parent { display: flex; .left { width: 100px; // 或 flex: 0 0 100px; } .right { flex: 1; } }
.parent { display: table; width: 100%; // 设置table-layout: fixed; 会使单元格等宽, 因此此处不设置 .left, .right { display: table-cell; } .left { width: 0.1%; // 宽度设置一个极小值, 由于没有设置table-layout: fixed; 所以宽度由内容决定 white-space:nowrap; } }
二. 等宽(两/多列)布局
公共代码:
html
<div class="parent"> <div class="column"> <p>1</p> </div> <div class="column"> <p>2</p> </div> <div class="column"> <p>3</p> </div> <div class="column"> <p>4</p> </div> </div>
css
html, body, div, p { margin: 0; padding: 0; } .parent { width: 800px; border: 1px solid coral; .column { height: 30px; background: bisque; p { background: #f0b979; height: 30px; } } }
方案一: float (个人并不喜欢, 写法很死, 需要知道有多少列, 而且有边框的情况下会超出容器)
.parent { margin-left: -20px; overflow: hidden; .column { float: left; width: 25%; padding-left: 20px; box-sizing: border-box; } }
方案二: flex (推荐)
.parent { display: flex; .column { flex: 1; &+.column { margin-left: 10px; } } }
三. 等高布局
推荐方案:
.parent { display: flex; } .left, .right { flex: 1; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
1、纯工具操作步骤,懂代码更容易 划线就是不符合国人的审美观念,看着就别扭,...
1.先瞧瞧效果: 2.代码是这样的: img src=images/circle.png alt= id=circle/@m...
前言 Canvas绘制多变形非常简单,只要懂得Canvas路径 + 简单的初中数学知识即可...
折叠式卡片布局在PC版网站中可能不常见,但是在手机版,小屏幕的网页浏览会大发...
在默认情况之下,如果在Canvas之中将某个物体(源)绘制在另一个物体(目标)之...
背景 在公司参与一个原生APP和h5混合开发的项目,本人在项目中负责h5部分,现将...
本文没有咬文嚼字的地方,只是一个配色技巧的分享,十分简单,简单到流泪,但或...
简介: 6月4日,以“开启分布式云新时代”为主题2021云边协同大会在北京举行,本...
一、虽然有的属性是boolean类型,但仍旧建议按照XHTML书写(属性名=属性值)格式...
作者:Joe Seifi 译者:前端小智 移动: https://mp.weixin.qq.com/s/p5... 有梦...