什么是三栏布局
三栏布局是实现左右栏定宽,中间栏自适应并且中间栏优先加载的一种布局方式。
三栏布局的实现
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;
order:0;
}
.main {
flex: 1;
background: pink;
height: 200px;
order:1;
}
.right {
height: 200px;
width: 200px;
background: red;
order:2;
}
</style>
</head>
<body>
<div class="container">
<div class="main">center</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
</body>
</html>
实现效果
思路
- 使用弹性布局,子元素默认沿着主轴方向排列。
- 然后,定义了三个子元素,分别是left、main和right,其中,main元素使用flex: 1属性,使其自动占据剩余空间,从而实现左右两边固定宽度,中间自适应宽度的布局效果。
- 最终,通过将左侧和右侧元素的宽度固定为200px,使得中间元素随着浏览器窗口的大小变化而自适应调整。
- 利用弹性布局中的
order属性也可以实现中间内容优先加载。
order 属性用于设置弹性子元素的排列顺序。它可以是一个整数,也可以是负数,默认值为 0。
当 order 属性的值为正数时,值越小,排列顺序越靠前。当 order 属性的值为负数时,值越大,排列顺序越靠前。
我们给左侧栏设置order:0;,给中间栏设置order:1;,给右侧栏设置order:2;。这样排列顺序就是左中右了(并不影响加载顺序,因为加载顺序由html里的代码块顺序决定)。
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布局的规则从左到右显示,形成三栏布局。