面试官:请你聊聊实现三栏布局的几种方式。
什么是三栏布局
三栏布局是实现左右栏定宽,中间栏自适应并且中间栏优先加载的一种布局方式。
三栏布局的实现
1.圣杯布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.page {
height: 200px;
padding: 0 200px;
}
.left,
.right {
height: 200px;
width: 200px;
background-color: green;
}
.page div {
float: left;
}
.content {
width: 100%;
height: 200px;
background-color: pink;
}
.left {
margin-left: -200px;
position: relative;
left: -100%;
}
.right {
margin-left: -200px;
right: -200px;
position: relative;
}
</style>
</head>
<body>
<div class="page">
<div class="content">主体内容</div>
<div class="left">广告位</div>
<div class="right">广告位</div>
</div>
</body>
</html>
实现效果
实现原理
- 中间内容
content
根据页面宽度变化而变化,将其设置为100%
,设置page
的padding属性为200px,使得页面左右两边流出200px大小的区域 - 将
left
,right
设置为左浮动使其脱离文档流 - 这是content占据整个页面宽度,
left
,right
不得不被挤下来 - 给
left
设置margin-left属性为-200px使得其向左移动200px占据右边部分,此时设置为相对定位再让left
向左移动'100%'也就是content
的宽度,此时的left
则到页面的左部分 - 给
right
设置'margin-left'属性为负值使其进入到content
中,再设置为相对定位右移至页面右部分
2.双飞翼布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.page {
height: 200px;
}
.left,
.right {
height: 200px;
width: 200px;
background-color: green;
}
.page>div {
float: left;
}
.content {
width: 100%;
height: 200px;
background-color: #f04747;
}
.inner {
margin: 0 200px;
height: 100%;
}
.left {
margin-left: -100%;
}
.right {
margin-left: -200px;
}
</style>
</head>
<body>
<div class="page">
<div class="content">
<div class="inner">主体内容</div>
</div>
<div class="left">广告位</div>
<div class="right">广告位</div>
</div>
</body>
</html>
实现效果
双飞翼布局的优点
主体内容可以先加载
与圣杯布局的差异
在解决中间栏内容被覆盖时,圣杯布局是设置父元素的padding
来空出中间内容,而双飞翼布局则是在内容栏在嵌套一个div放置主体内容设置margin来空出中间的位置。
3.弹性布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.container {
display: flex;
}
.left {
width: 200px;
height: 200px;
background: red;
}
.main {
flex: 1;
background: blue;
height: 200px;
}
.right {
height: 200px;
width: 200px;
background: red;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="main">center</div>
<div class="right">right</div>
</div>
</body>
</html>
实现效果
思路
- 使用弹性布局,子元素默认沿着主轴方向排列。
- 然后,定义了三个子元素,分别是left、main和right,其中,main元素使用flex: 1属性,使其自动占据剩余空间,从而实现左右两边固定宽度,中间自适应宽度的布局效果。
- 最终,通过将左侧和右侧元素的宽度固定为200px,使得中间元素随着浏览器窗口的大小变化而自适应调整。
4. table布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.page {
width: 100%;
height: 200px;
display: table;
table-layout: fixed;
}
.page>div {
display: table-cell;
}
.left,
.right {
height: 200px;
width: 200px;
background-color: green;
}
.content {
width: 100%;
height: 200px;
background: yellow;
}
</style>
</head>
<body>
<div class="page">
<div class="left">广告位</div>
<div class="content">主体内容</div>
<div class="right">广告位</div>
</div>
</body>
</html>
实现效果
思路
1.设置为 display: table;
:将 .page
元素转换为一个表格。并设置table-layout: fixed;
:使得表格布局为固定,这意味着列宽一旦设定就不会改变,即使内容溢出。
.page > div
选择器将.page
的直接子元素设置为display: table-cell;
,使它们表现为表格单元格。.content
类设置宽度为100%
表示主要内容区域。由于.content
占据了整个表格单元格,它会自动填充剩余的空间,位于.left
和.right
之间。.left
,.content
, 和.right
依次排列在.page
内,它们按照表格单元格的顺序从左到右显示,形成三栏布局。
5.Grid容器实现三栏布局
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
.page {
width: 100%;
height: 200px;
display: grid;
grid-template-columns: 200px auto 200px;
}
.left,
.right {
height: 200px;
background-color: green;
}
.content {
background: yellow;
}
</style>
</head>
<body>
<div class="page">
<div class="left">广告位</div>
<div class="content">主体内容</div>
<div class="right">广告位</div>
</div>
</body>
</html>
实现效果
思路
.page
类设置为display: grid;
:将.page
元素转换为一个Grid容器。grid-template-columns: 200px auto 200px;
:定义了Grid的列结构,这里设置三列,第一列和第三列固定宽度200px,中间列(.content
)自动扩展填满剩余空间。.left
和.right
类都设置相同的高度(200px),表示两侧的广告位。.content
类没有指定宽度,但因为设置了grid-template-columns
的中间列为auto
,它会自动占据剩余的空间,表示主要内容区域。.left
,.content
, 和.right
依次排列在.page
内,它们按照Grid布局的规则从左到右显示,形成三栏布局。
总结
三栏布局的重点和难度其实就是要想明白如何使得中间内容自适应,两边内容固定。而三栏布局其中最经典的布局思路就是圣杯布局
和双飞翼布局
,只要弄明白了这两种布局方式,其他的方式也自然容易搞懂。
本篇文章就到此为止啦,希望通过这篇文章能对你了解三栏布局
有所帮助,本人水平有限难免会有纰漏,欢迎大家指正。如觉得这篇文章对你有帮助的话,欢迎点赞收藏加关注,感谢支持🌹🌹。
转载自:https://juejin.cn/post/7371764248802164776