网站首页 > 知识剖析 正文
1. 实现两栏布局的方式你知道那些方法? (其实就是我们经常在开发中使用到的 '左边宽度固定右边宽度自适应' )
那我们应该如何回答呢?
1.1 下面是回答该问题的一种思路:
1. 实现两栏布局的方法我了解到的有 7 中方式 。
2. 这 7 中方式分别是:
1. 使用 calc() 函数计算 + float
2. 使用 float + overflow: hidden;
3. 使用 flex 弹性布局
4. 使用 绝对定位 absolute
5. 使用表格布局—table
6. 使用 inline-block 和 calc()函数
7. 使用双 float 浮动实现
3. 这些方法比较常用的是 'flex 弹性布局'、'绝对定位 absolute'、'calc() 函数计算 + float'、'float + overflow: hidden;' 这四种方法, 其余的方法在开发中使用的比较少 。
3.1 先说一下最最常用的 '弹性布局' 实现思路:
1. 首先设置最外层父元素 "display: flex;"
2. 左侧设置宽度 width 为固定像素, 例如设置 width: 200px;
3. 右侧区域设置 flex: 1;
3.2 再说说 '绝对定位 absolute' 的实现思路:
1. 首先设置外层父元素 "position: relative;"
2. 左侧设置宽度 width 为固定像素; 然后再设置定位属性 "position: absolute;" 、 " left: 0; " 、 "top: 0;"
3. 右侧设置 margin-left: 200px;(这里需要注意: margin 的设置需要与左侧的宽度设置相同) 。
3.3 'calc() 函数计算 + float' 的实现思路:
1. 首先设置外层父元素 清浮动方法: ':after 伪元素 + zoom:1 方法' (因为使用到了float浮动方法, 清浮动也会成为一个考察的知识点)。
2. 左侧设置宽度 width 为固定像素, 例如设置 width: 200px;
3. 右侧区域设置 float: right; 宽度 width 设置为: width: calc(100% - 200px);
3.4 'float + overflow: hidden;' 的实现思路:
1. 首先设置外层父元素 清浮动方法: ':after 伪元素 + zoom:1 方法' (因为使用到了float浮动方法, 清浮动也会成为一个考察的知识点)。
2. 左侧设置宽度 width 为固定像素例如设置 width: 200px; 同时设置 float: left;
3. 右侧区域设置 overflow: hidden;
3.5 'table 表格布局' 的实现思路:
1. 首先设置外层父元素display属性值为table, 即 display: table;
2. 左侧设置 display 属性值为 table-cell, 即 display: table-cell;同时设置宽度 width 为固定像素例如设置 width: 200px;
3. 右侧区域设置 display 属性值为 table-cell, 即 display: table-cell;就可以 。
3.6 'inline-block 和 calc()函数' 的实现思路:
1. 首先设置外层父元素需要设置 font-size: 0; 是为了清除因子元素设置 display: inline-block 而产生的默认间距 。
2. 左侧设置 display 属性值为 inline-block, 即 display: inline-block; 同时设置宽度 width 为固定像素例如设置 width: 200px;
3. 右侧区域设置 display 属性值为 inline-block, 即 display: inline-block; 同时设置宽度 width 为 width: calc(100% - 200px);
3.7 '双 float 浮动实现' 的实现思路:
1. 首先设置外层父元素 清浮动方法: ':after 伪元素 + zoom:1 方法' 。
2. 左侧设置 float 属性为 float: left; 同时设置宽度 width 为固定像素, 例如设置 width: 200px;
3. 右侧区域设置 float 属性为 float: right; 同时设置宽度 width 为 width: calc(100% - 200px);
4. 其余的几种方法在开发中使用几乎没有使用, 所以关注比较少, 上面介绍的几种方法强烈推荐 '弹性布局' , 其次推荐 '绝对定位 absolute' 。这两个方法对于一些特殊要求它有比较好的效果 。 例如侧边左侧需要两种背景色+高度设置时比较灵活 。
闲谈: 面试题目不是每一一个问题都要回答的特别精细, 因为时间有限制, 就比如这道题, 如果回答的特别精细大概要2-3分钟, 这是基础问题, 如果这一个问题就占用了你很多的面试时间, 那么你的优势或者说其它方面面试官就会了解的少了。 这样会得不偿失 。
2. 下面是详细的代码实现以及代码验证图例, 确保正确性;这样大家也可以放学去记忆和学习。
2.1 实现两栏布局实现方法: 使用 flex 弹性布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现两栏布局的方式: 左边宽度固定右边宽度自适应</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
/* 方法一: 使用 flex 弹性布局 */
.w_outer {
width: 100%;
/* 知识点: 下面的设置会让我们的页面呈现比较好的观赏效果, 高度与高度百分比设置也会非常灵活 */
min-height: 100vh;
background: rgb(88, 144, 196);
display: flex;
}
.w_son-left {
width: 200px;
background-color: violet;
}
.w_son-left p {
background-color: cyan;
height: 80%;
}
.w_son-right {
flex: 1;
height: 100%;
/* 慎用: 这里设置与否看项目需求, 但是个人开发经验来说, 这里不设置是做好的,具体效果会有在下面会有对比 */
/* height: 100vh; */
background-color: slategrey;
}
</style>
<body>
<div class="w_outer">
<div class="w_son-left">
<p>左侧导航</p>
</div>
<div class="w_son-right">右侧内容</div>
</div>
</body>
</html>
如图所示:
2.2 实现两栏布局实现方法: 使用绝对定位 absolute
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现两栏布局的方式: 左边宽度固定右边宽度自适应</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
/* 方法二: 使用绝对定位 absolute */
.w_outer {
position: relative;
background: rgb(88, 144, 196);
/* 知识点: 下面的设置会让我们的页面呈现比较好的观赏效果, 高度与高度百分比设置也会非常灵活 */
min-height: 100vh;
}
.w_son-left {
position: absolute;
left: 0;
top: 0;
width: 200px;
height: 100%;
/* 慎用: 这里设置与否看项目需求, 但是个人开发经验来说, 这里不设置是做好的,具体效果会有在下面会有对比 */
/* height: 100vh; */
background-color: tomato;
}
.w_son-left p {
/* 验证设置子元素高度百分比是否生效 */
height: 20%;
background-color: violet;
}
.w_son-right {
margin-left: 200px;
background: skyblue;
height: 100%;
/* 慎用: 这里设置与否看项目需求, 但是个人开发经验来说, 这里不设置是做好的,具体效果会有在下面会有对比 */
/* height: 100vh; */
}
</style>
<body>
<div class="w_outer">
<div class="w_son-left">
<p>左侧导航</p>
</div>
<div class="w_son-right">右侧内容</div>
</div>
</body>
</html>
如图所示:
2.3 实现两栏布局实现方法: float + overflow: hidden;
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现两栏布局的方式: 左边宽度固定右边宽度自适应</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
/* 方法三: 使用 float + overflow: hidden; */
.w_outer {
width: 100%;
background: rgb(88, 144, 196);
}
/* 清浮动 */
.w_outer::after {
display:block;
clear:both;
content:"";
/* 这里就起不到上面的两种方法设置的效果, 所以下面的 height 设置就注释掉了 */
/* min-height: 100vh; */
}
.w_outer {
zoom:1
}
.w_son-left {
/* height: 100%; */
width: 200px;
background-color: red;
float: left;
}
.w_son-left p {
background-color: burlywood;
/* height: 30%; */
}
.w_son-right {
background-color: yellow;
overflow: hidden;
}
</style>
<body>
<div class="w_outer">
<div class="w_son-left">
<p>左侧导航</p>
</div>
<div class="w_son-right">右侧内容</div>
</div>
</body>
</html>
如图所示:
2.4 实现两栏布局实现方法: calc() 函数计算 + float
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现两栏布局的方式: 左边宽度固定右边宽度自适应</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
/* 方法四: 使用 calc() 函数计算 + float */
.w_outer {
width: 100%;
background: rgb(88, 144, 196);
/* 这里就起不到上面的两种方法设置的效果, 所以下面的 height 设置就注释掉了 */
/* min-height: 100vh; */
}
/* 清浮动 */
.w_outer::after {
display:block;
clear:both;
content:"";
}
.w_outer {
zoom:1
}
.w_son-left {
width: 200px;
/* height: 80%; */
background-color: tan;
display: inline-block;
}
.w_son-left p {
background-color: coral;
/* height: 20%; */
}
.w_son-right {
float: right;
width: calc(100% - 200px);
background-color: thistle;
}
</style>
<body>
<div class="w_outer">
<div class="w_son-left">
<p>左侧导航</p>
</div>
<div class="w_son-right">右侧内容</div>
</div>
</body>
</html>
如图所示:
2.5 实现两栏布局实现方法: table 表格布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现两栏布局的方式: 左边宽度固定右边宽度自适应</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
/* 方法五: 使用表格布局 table */
.w_outer {
display: table;
width: 100%;
/* 这里就起不到上面的两种方法设置的效果, 所以下面的 height 设置就注释掉了 */
/* min-height: 100vh; */
}
.w_son-left {
display: table-cell;
width: 200px;
background-color: rgb(165, 144, 165);
/* height: 80%; */
}
.w_son-left p {
/* 验证设置子元素高度百分比是否生效 */
/* height: 20%; */
background-color: rgb(182, 122, 182);
}
.w_son-right {
display: table-cell;
background: skyblue;
height: 200px;
}
</style>
<body>
<div class="w_outer">
<div class="w_son-left">
<p>左侧导航</p>
</div>
<div class="w_son-right">右侧内容</div>
</div>
</body>
</html>
如图所示:
2.6 实现两栏布局实现方法: inline-block 和 calc()函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现两栏布局的方式: 左边宽度固定右边宽度自适应</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
/* 方法六: 使用 inline-block 和 calc()函数 */
.w_outer {
overflow: hidden;
width: 100%;
/* 清除因设置 display: inline-block 而产生的默认间距 */
/* 当我们设置了 font-size: 0; 时, 子元素需要设置各自的字号大小 */
font-size: 0;
/* 这里就起不到上面的两种方法设置的效果, 所以下面的 height 设置就注释掉了 */
/* min-height: 100%; */
}
.w_son-left {
display: inline-block;
width: 200px;
background: purple;
vertical-align: top;
font-size: 20px;
/* height: 80%; */
}
.w_son-left p {
background-color: rgb(182, 122, 182);
/* height: 20%; */
}
.w_son-right {
display: inline-block;
background: skyblue;
width: calc(100% - 200px);
font-size: 16px;
/* height: 100%; */
}
</style>
<body>
<div class="w_outer">
<div class="w_son-left">
<p>左侧导航</p>
</div>
<div class="w_son-right">右侧内容</div>
</div>
</body>
</html>
如图所示:
2.7 实现两栏布局实现方法: 双float + calc() 实现
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>实现两栏布局的方式: 左边宽度固定右边宽度自适应</title>
</head>
<style>
* {
padding: 0;
margin: 0;
}
/* 方法七: 使用 双float + calc() 实现 */
.w_outer {
overflow: hidden;
width: 100%;
}
/* 清浮动方法 */
.w_outer::after {
display:block;
clear:both;
content:"";
}
.w_outer {
zoom:1
}
.w_son-left {
float: left;
width: 200px;
background: rgb(116, 89, 116);
vertical-align: top;
}
.w_son-left p {
background-color: rgb(97, 116, 145);
}
.w_son-right {
float: right;
background: skyblue;
width: calc(100% - 200px);
}
</style>
<body>
<div class="w_outer">
<div class="w_son-left">
<p>左侧导航</p>
</div>
<div class="w_son-right">右侧内容</div>
</div>
</body>
</html>
如图所示:
之前有整理过部分知识点, 现在将整理的相关内容, 验证之后慢慢分享给大家; 这个专题是 "前端面试题" 的相关专栏; 大概会有200+的文章。
如果对大家有所帮助,可以点个关注、点个赞; 文章会持续打磨 。
有什么想要了解的前端知识, 可以在评论区留言, 会及时分享所相关内容 。
猜你喜欢
- 2025-05-26 强大而好用的选择器:focus-within
- 2025-05-26 CSS面试题:CSS实现自适应正方形以及等宽高比矩形
- 2025-05-26 零基础教你学前端——85、高度自适应
- 2025-05-26 Layui简单实现左侧菜单和Tab选项卡动态操作
- 2025-05-26 零基础教你学前端——72 CSS背景
- 2025-05-26 《随“机”应变》3步快速制作网站适配小样
- 2025-05-26 100行Html5+CSS3+JS代码实现元旦倒计时界面
- 2025-05-26 在移动端别再用 100vh 了!试试这些全新的 CSS 单位
- 2025-05-26 MFC转QT:Qt高级特性 - 样式表
- 2025-05-26 Next.js 零基础教程3|2024最新更新中|曲速引擎 Warp Drive
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)