css布局方案汇总(28个实例图文并茂)
ccwgpt 2025-04-23 22:38 13 浏览 0 评论
简介
布局在我们前端日常开发来说是非常重要的,一个好的布局能简化代码的同时还能提高网页的性能。常见的布局方法有浮动(float)布局、绝对定位(position)布局、表格布局(table)、弹性(flex)布局、网格(grid)布局。关于布局方法本文不做详细讲解,笔者推荐看阮一峰老师 flex布局教程 和阮一峰老师 grid布局教程。
本文主要讲解水平垂直居中、单栏布局、双栏布局、三栏布局一些项目中常用的布局方案。
本文代码全部使用codepen在线代码工具进行演示。
居中
居中在我们日常工作中还是会经常碰到。
水平居中
对于水平居中一般可以使用如下四种方式
- 对于行内元素我们可以在父元素上设置text-align:center;来实现。
- 对于定长块级元素我们可以使用margin: 0 auto;来实现。
- 我们可以在父元素上使用flex布局来实现。
- 我们可以在父元素上使用grid布局来实现。
<div class="div1">
<span>行内元素水平居中</span>
</div>
<div class="div2">
<span>行内元素水平居中</span>
<div>块级元素水平居中</div>
</div>
<div class="div3">
<span>行内元素水平居中</span>
<div>块级元素水平居中</div>
</div>
<div class="div4">块级元素水平居中</div>
.div1 {
text-align: center;
}
.div2 {
display: flex;
justify-content: center;
}
.div3 {
display: grid;
justify-content: center;
}
.div4 {
width: 130px;
margin: 0 auto;
}
效果如下
点击查看代码运行实例
垂直居中
对于垂直居中一般可以使用如下三种方式
- 我们可以在父元素上设置line-height等于height来实现。
- 我们可以在父元素上使用flex布局来实现。
- 我们可以在父元素上使用grid布局来实现。
- 我们可以在父元素上使用table布局来实现。
<div class="div1">
<span>行内元素垂直居中</span>
<!-- <div>块级元素垂直居中</div> -->
</div>
<div class="div2">
<span>行内元素垂直居中</span>
<div>块级元素垂直居中</div>
</div>
<div class="div3">
<span>行内元素垂直居中</span>
<div>块级元素垂直居中</div>
</div>
<div class="div4">
<span>行内元素垂直居中</span>
<div>块级元素垂直居中</div>
</div>
.div1 {
height: 100px;
background: lightgreen;
line-height: 100px;
}
.div2 {
height: 100px;
background: lightblue;
display: flex;
align-items: center;
}
.div3 {
height: 100px;
background: lightgreen;
display: grid;
align-content: center;
}
.div4 {
height: 100px;
background: lightblue;
display: table-cell;
vertical-align: middle;
}
效果如下
点击查看代码运行实例
水平垂直同时居中
比如我们想实现如下水平垂直同时居中的效果
实现水平垂直同时居中我们可以使用绝对定位、table布局、flex布局 或 grid布局来实现。
首先我们创建一个需要居中的盒子。
<div class="box"></div>
纯绝对定位
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
top: 0;
left: 0;
right: 0;
bottom: 0;
margin: auto;
}
点击查看代码运行实例
绝对定位加负外边距
这种方式需要知道居中元素的具体宽高,不然负的margin没法设置。
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
left: 50%;
top: 50%;
margin-left: -100px;
margin-top: -50px;
}
点击查看代码运行实例
绝对定位加平移
这种平移的方式就不需要考虑居中盒子的具体宽高了。
.box {
position: absolute;
width: 200px;
height: 100px;
background: red;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
点击查看代码运行实例
使用flex实现
html,body {
height: 100%;
}
body {
background: gray;
display: flex;
align-items: center;
justify-content: center;
}
.box {
width: 200px;
height: 100px;
background: red;
}
点击查看代码运行实例
使用grid实现
html,body {
height: 100%;
}
body {
background: gray;
display: grid;
/* align-content: center;
justify-content: center; */
/* align-content和justify-content的简写 */
place-content: center;
}
.box {
width: 200px;
height: 100px;
background: red;
}
点击查看代码运行实例
使用table加外边距实现
使用table布局需要注意如下
- display: table时padding会失效
- display: table-row时margin、padding同时失效
- display: table-cell时margin会失效
<div class="box">
<div class="child"></div>
</div>
.box {
background: red;
height: 300px;
width: 600px;
display: table-cell;
vertical-align: middle;
}
.child {
width: 200px;
height: 200px;
background: lightgreen;
margin: 0 auto;
}
点击查看代码运行实例
单栏布局
单栏布局我们常用在网页框架上,一般我们把网页分为 header、content、footer三部分。
在不同的项目我们可能对这三部分的样式需求有所差别,比如需要顶部固定、需要底部固定等等。
顶底部都不固定
比如想实现如下效果,footer在内容不足的时候吸附在窗口底部,当内容多的时候又可以被抵到窗口下面。
使用padding加负margin实现
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
</div>
<div class="footer">footer</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
min-height: 100%;
padding-bottom: 50px;
overflow: auto;
box-sizing: border-box;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
/* 这里的高度只是为了模拟内容多少 */
height: 100px;
/* height: 1000px; */
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
点击查看代码运行实例
使用flex实现
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
flex-direction: column;
min-height: 100%;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
/* 这里的高度只是为了模拟内容多少 */
height: 100px;
/* height: 1000px; */
flex-grow: 1;
}
.footer {
height: 50px;
background: lightgreen;
}
点击查看代码运行实例
顶部固定
使用padding加负margin加fixed实现顶部固定布局
<div class="header">header</div>
<div class="wrap">
<div class="content">content</div>
</div>
<div class="footer">footer</div>
html, body {
height: 100%;
margin: 0;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.wrap {
min-height: 100%;
padding-bottom: 50px;
overflow: auto;
box-sizing: border-box;
}
.content {
margin-top: 50px;
background: lightpink;
/* 这里的高度只是为了模拟内容多少 */
height: 100px;
/* height: 1000px; */
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
点击查看代码运行实例
使用flex加fixed定位实现
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
min-height: 100%;
flex-direction:column;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.content {
background: lightpink;
/* 这里的高度只是为了模拟内容多少 */
/* height: 100px; */
height: 1000px;
margin-top: 50px;
flex-grow: 1;
}
.footer {
height: 50px;
background: lightgreen;
}
点击查看代码运行实例
底部固定
使用padding加负margin实现底部固定布局
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
</div>
<div class="footer">footer</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
height: 100%;
padding-bottom: 50px;
overflow: auto;
box-sizing: border-box;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
height: 100px;
height: 1000px;
}
.footer {
height: 50px;
background: lightgreen;
margin-top: -50px;
}
点击查看代码运行实例
使用flex加fixed定位实现
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
min-height: 100%;
flex-direction:column;
}
.header {
height: 50px;
background: lightblue;
}
.content {
background: lightpink;
/* 这里的高度只是为了模拟内容多少 */
/* height: 100px; */
height: 1000px;
flex-grow: 1;
margin-bottom: 50px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
width: 100%;
bottom: 0;
}
点击查看代码运行实例
顶底部都固定
使用fixed实现顶底部固定布局
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
复制代码
html, body {
height: 100%;
margin: 0;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.content {
background: lightpink;
padding-top: 50px;
padding-bottom: 50px;
/* height: 100px; */
height: 1000px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
bottom: 0;
width: 100%;
}
点击查看代码运行实例
使用flex加fixed定位实现
<div class="wrap">
<div class="header">header</div>
<div class="content">content</div>
<div class="footer">footer</div>
</div>
html, body {
height: 100%;
margin: 0;
}
.wrap {
display: flex;
min-height: 100%;
flex-direction:column;
}
.header {
height: 50px;
background: lightblue;
position: fixed;
width: 100%;
}
.content {
background: lightpink;
/* 这里的高度只是为了模拟内容多少 */
/* height: 100px; */
height: 1000px;
flex-grow: 1;
margin-bottom: 50px;
margin-top: 50px;
}
.footer {
height: 50px;
background: lightgreen;
position: fixed;
width: 100%;
bottom: 0;
}
点击查看代码运行实例
两栏布局
两栏布局就是一边固定,另外一边自适应,效果如下
实现两栏布局的方法也有很多,笔者接下来介绍用的比较多的几种方式。
左 float,然后右 margin-left(右边自适应)
<div class="aside"></div>
<div class="main"></div>
div {
height: 500px;
}
.aside {
width: 300px;
float: left;
background: yellow;
}
.main {
background: aqua;
margin-left: 300px;
}
点击查看代码运行实例
右 float,然后右 margin-right(左边自适应)
<div class="aside"></div>
<div class="main"></div>
div {
height: 500px;
}
.aside {
width: 300px;
float: right;
background: yellow;
}
.main {
background: aqua;
margin-right: 300px;
}
点击查看代码运行实例
absolute定位加margin-left(右边自适应)
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
div {
height: 500px;
}
.wrap {
position: relative;
}
.aside {
width: 300px;
background: yellow;
position: absolute;
}
.main {
background: aqua;
margin-left: 300px;
}
点击查看代码运行实例
absolute定位加margin-right(左边自适应)
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
div {
height: 500px;
}
.wrap {
position: relative;
}
.aside {
width: 300px;
background: yellow;
position: absolute;
right: 0;
}
.main {
background: aqua;
margin-right: 300px;
}
点击查看代码运行实例
使用flex实现
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
div {
height: 500px;
}
.wrap {
display: flex;
}
.aside {
flex: 0 0 300px;
background: yellow;
}
.main {
background: aqua;
flex: 1 1;
}
点击查看代码运行实例
使用grid实现
<div class="wrap">
<div class="aside"></div>
<div class="main"></div>
</div>
height: 500px;
}
.wrap {
display: grid;
grid-template-columns: 300px auto;
}
.aside {
background: yellow;
}
.main {
background: aqua;
}
点击查看代码运行实例
三栏布局
三栏布局就是两边固定,中间自适应布局,效果如下
实现三栏布局的方法也有很多,笔者接下来介绍用的比较多的几种方式。
position + margin-left + margin-right实现三栏布局
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.left {
position: absolute;
left: 0;
top: 0;
width: 200px;
background: green;
}
.right {
position: absolute;
right: 0;
top: 0;
width: 200px;
background: red;
}
.middle {
margin-left: 200px;
margin-right: 200px;
background: lightpink;
}
点击查看代码运行实例
float + margin-left + margin-right实现三栏布局
<div class="left"></div>
<div class="right"></div>
<div class="middle"></div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.left {
width: 200px;
background: green;
float: left;
}
.right {
width: 200px;
background: yellow;
float: right;
}
.middle {
margin-left: 200px;
margin-right: 200px;
background: lightpink;
}
点击查看代码运行实例
flex实现三栏布局
<div class="wrap">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.wrap {
display: flex;
}
.left {
flex: 0 0 200px;
background: green;
}
.right {
flex: 0 0 200px;
background: yellow;
}
.middle {
background: lightpink;
flex: 1 1;
}
点击查看代码运行实例
grid实现三栏布局
<div class="wrap">
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</div>
html,
body {
margin: 0;
}
div {
height: 500px;
}
.wrap {
display: grid;
grid-template-columns: 200px auto 200px;
}
.left {
background: green;
}
.right {
background: yellow;
}
.middle {
background: lightpink;
}
点击查看代码运行实例
圣杯布局
圣杯布局在项目中基本上不会再使用了,在面试中我们会经常碰到,所以需要了解。
主要用到了浮动和和相对定位。
<div class="container">
<div class="content">中间内容</div>
<div class="left">左侧区域</div>
<div class="right">右侧区域</div>
</div>
div {
height: 500px;
}
.container {
padding: 0 200px 0 200px;
border: 1px solid black;
}
.content {
float: left;
width: 100%;
background: #f00;
}
.left {
width: 200px;
background: #0f0;
float: left;
margin-left: -100%;
position: relative;
left: -200px;
}
.right {
width: 200px;
background: #00f;
float: left;
margin-left: -200px;
position: relative;
right: -200px;
}
点击查看代码运行实例
双飞翼布局
双飞翼布局在项目中基本上不会再使用了,在面试中我们会经常碰到,所以需要了解。
主要用到了浮动。
<div class="main">
<div class="content">content</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
div {
height: 500px;
}
.main {
float: left;
width: 100%;
background: #f00;
}
.main .content {
/* margin、padding这两种方式都可以 */
/* margin-left: 200px;
margin-right: 300px; */
padding-left: 200px;
padding-right: 300px;
}
.left {
width: 200px;
background: #0f0;
float: left;
margin-left: -100%;
}
.right {
width: 200px;
background: #00f;
float: left;
margin-left: -200px;
}
点击查看代码运行实例
总结
因为flex和grid布局方法已经很强大了,日常工作中99%的布局都可以使用这两种方式来实现。所以笔者建议能使用flex或者grid布局方法实现的就尽量使用这两种布局方式实现。因为不仅简单而且负面作用也很少。
浮动布局和绝对定位布局会导致元素脱离文档流,会带来一些负面作用,有时会导致一些意想不到的结果。
关于flex布局的兼容性和grid布局的兼容性,目前已经支持的很好了,大家可以放心使用。
flex布局的兼容性
grid布局的兼容性
后记
感谢小伙伴们的耐心观看,本文为笔者个人学习笔记,如有谬误,还请告知,万分感谢!如果本文对你有所帮助,还请点个关注点个赞~,您的支持是笔者不断更新的动力!
- 上一篇:十款免费的CSS框架加速Web开发
- 已经是最后一篇了
相关推荐
- css布局方案汇总(28个实例图文并茂)
-
简介布局在我们前端日常开发来说是非常重要的,一个好的布局能简化代码的同时还能提高网页的性能。常见的布局方法有浮动(float)布局、绝对定位(position)布局、表格布局(table)、弹性(fl...
- 十款免费的CSS框架加速Web开发
-
Pure这是Yahoo最新推出的一款CSS框架,它只有HTML和CSS,没有使用任何JavaScript语言。总大小只有4.4kb,但功能却非常丰富,支持响应式样式和各种导航、表格、表单、按钮、网格和...
- Tailwind CSS 是不是目前世上最好的CSS框架?
-
转载说明:原创不易,未经授权,谢绝任何形式的转载今天看了一篇国外大佬对TailwindCSS的看法,在这里分享给大家,看看大家是否赞同,以下是其相关内容的整理,由于翻译水平有限,欢迎大家讨论和指...
- 下一代 CSS 框架:Mojo CSS,为何如此受欢迎?
-
TailwindCSS推出即受到广大开发者的欢迎,当前Githubstar数已达77.8k。它是一个功能类优先(utility-first)的CSS框架,它提供了一系列功能类,让开发者...
- 常见的几种摄影构图方式
-
摄影构图,是一种在摄影画面中表现结构美、形式美的方式。构图能让摄影主体更加突出,画面更加有序。所以说,构图在摄影中是非常重要的一个环节。无论是前期构图还是后期构图,摄影者都要对构图有一个比较深的了解。...
- 风光摄影10大构图技巧,会用构图,照片更容易好看
-
风光摄影10大构图技巧,会用构图,照片更容易好看先解释一下,为什么会使用构图之后,照片更容易好看?因为,构图是根据很多好看的照片,总结出来的技巧,使用这些构图技巧,就相当于站在了巨人的肩膀上,也就是用...
- 掌握框式构图的摄影技巧,会让摄影爱好者的作品更有魅力!
-
很多摄影爱好者都知道摄影构图中有个框式构图,但大多数人对框式构图的摄影技巧,却一知半解。所以摄影爱好者们有必要更全面、深入的了解,并掌握框式构图,会对你摄影水平的提高更有帮助。【欢迎点击上方关注:金立...
- 这个构图很简洁,但为什么不耐看?
-
摄影爱好者最常犯的错就是过于复杂、主体不明确,但当遇到简单的场景往往又会出现单调、不耐看的状况。为什么会这样?说白了还是观察力不够。下面是本周的摄影入围习作,我们一起来看看这些照片中主体、陪体以及背景...
- 初学者需要记牢的八种常用构图法
-
作者:冯海军摄影中,构图很关键,决定照片是否成功,所以在构图上要加以重视和推敲,虽然说构图无定法,但是也有很多的规律可循,以下列举几种常用构图,会对初学者有很大的帮助。多彩刘卫洲摄苏州姑苏俱乐部(...
- 构图这件事不难!掌握14种构图模式就稳了
-
如果说视觉元素是视觉信息的载体,那么构图就是视觉元素的载体。没有适当形式的构图对视觉元素有机、有序地承载,平面设计将无法传达预定的设计意图和视觉信息。因此,对于平面设计而言,构图是平面设计不可或缺的重...
- 框架构图如何使用?
-
1分钟教你用手机拍大片。今天我们利用框架构图,在不同的运镜方法下拍摄。·首先将手机贴近地面,拍摄人物走过的画面。·然后利用3D效果的背景衬托,将手机贴近地面,以低角度仰拍人物。·最后我们用高清画质来呈...
- 面构图的5种超实用的构图形式 前景构图,框架构图,填充构图
-
面构图的5种超实用的构图形式。为什么有的人拍摄的照片好看又舒适?仔细观察会发现他们善用构图。大家好,今天带大家了解摄影中5种超实用的面构图形式。·一、前景构图。前景是构图中的神奇要素,可以提升照片的表...
- 一看就懂!跟着马格南的大师学构图
-
马格南图片社是迄今为止全球最重要的摄影图片社,其网站包涵了太多经典的名字和照片。细细品味这些经典图片,能够学到很多有用的构图手法。跟着大师走,总不会错吧?前后景的运用这似乎是非常常见的一种手法,仔细看...
- 这才是框架构图,有想法!能给你启发么?
-
框架构图大家并不陌生,但并不是有一个框就行了。框架构图用得不好,就很死板生硬,给人感觉很假。如果你理解透了,拍出的作品不会单调。今天就给大家分享一下框架构图,你看看有哪些妙用?1.广角与长焦的应用长焦...
- 7B小模型写好学术论文,新框架告别AI引用幻觉
-
ScholarCopilot团队投稿量子位|公众号QbitAI学术写作通常需要花费大量精力查询文献引用,而以ChatGPT、GPT-4等为代表的通用大语言模型(LLM)虽然能够生成流畅文本,但...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- 若依框架 (41)
- MVC框架 (46)
- spring框架 (46)
- 框架图 (58)
- bootstrap框架 (43)
- flask框架 (53)
- quartz框架 (51)
- abp框架 (47)
- jpa框架 (47)
- scrapy框架 (52)
- beego框架 (42)
- java框架spring (43)
- grpc框架 (55)
- 前端框架bootstrap (42)
- orm框架有哪些 (43)
- ppt框架 (48)
- 内联框架 (52)
- winform框架 (46)
- gui框架 (44)
- cad怎么画框架 (58)
- ps怎么画框架 (47)
- ssm框架实现登录注册 (49)
- oracle v (42)
- oracle字符串长度 (48)
- oracle提交事务 (47)