【CSS布局】响应式设计原则以及双飞翼布局&圣杯布局

历史小剧场

温体仁未必是奸臣,钱谦益未必是好人,不需要惊讶,历史往往跟你所想的并不一样。英雄可以写成懦夫,能臣可以写成奸臣,史实并不重要,重要的是,谁来写。----《明朝那些事儿》

响应式设计原则

  1. 移动优先原则;
  2. 内容优先;
  3. 灵活布局;
  4. 图片和资源;
  5. 交互和性能

双飞翼布局

特点:两侧固定,中间自适应

效果:

传统方案实现

浮动+calc()函数

html 复制代码
<style>
	 * {
        margin: 0;
         padding: 0;
     }
	.container {
        min-width: 100%;
     }
     .container div {
         float: left;
         padding: 20px;
         text-align: center;
     }
     .center {
         width: calc(100% - 400px - 20 * 6px);
         background-color: red;
     }
     .left {
         width: 200px;
         background-color: yellow;
     }
     .right {
         width: 200px;
         background-color: green;
     }
</style>
<div class="container">
    <div class="left">左侧内容</div>
    <div class="center">中间内容</div>
    <div class="right">右侧内容</div>
</div>
Flex布局实现
css 复制代码
 .container {
   display: flex;
    justify-content: space-between;
}
.container div {
    padding: 20px;
    text-align: center;
}
.left {
    width: 200px;
    background-color: red;
}
.right {
    width: 200px;
    background-color: greenyellow;
}
.center {
    background-color: gold;
    flex: 1;
}

圣杯布局

特点:

  • 头部和底部固定;
  • 中间部分自适应;
  • 且中间部分里的三个部分遵循双飞翼布局原则
    效果:
传统方案实现

浮动+calc()函数

html 复制代码
<style>
	 * {
        margin: 0;
         padding: 0;
     }
     header, footer {
        text-align: center;
        height: 40px;
        line-height: 40px;
        width: 100%;
    }
    header {
        background-color: skyblue;
    }
    footer {
        background-color: salmon;
    }
    main {
        height: calc(100vh - 80px);
    }
    main > div {
        float: left;
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    main .left {
        width: 200px;
        background-color: red;
    }
    main .center {
        background-color: deepskyblue;
        width: calc(100% - 400px);
    }
    main .right {
        width: 200px;
        background-color: greenyellow;
    }
</style>
<header>header</header>
<main>
    <div class="left">左侧内容</div>
    <div class="center">中间内容</div>
    <div class="right">右侧内容</div>
</main>
<footer>footer</footer>
Flex布局实现
css 复制代码
header, footer {
    height: 50px;
    line-height: 50px;
    text-align: center;
}
header {
    background-color: slateblue;
}
footer {
    background-color: slategray;
}
main {
    height: calc(100vh - 100px);
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
}
main div {
    display: flex;
    align-items: center;
    justify-content: center;
}
main .left, main .right {
    width: 200px;
    flex-shrink: 0;
}
main .left {
    background-color: red;
}
main .right {
    background-color: greenyellow
}
main .center {
    background-color: skyblue;
    flex-grow: 1;
}
相关推荐
DsirNg1 小时前
页面栈溢出问题修复总结
前端·微信小程序
小徐_23331 小时前
uni-app 也能远程调试?使用 PageSpy 打开调试的新大门!
前端·微信小程序·uni-app
大怪v1 小时前
【Virtual World 03】上帝之手
前端·javascript
招来红月3 小时前
记录JS 实用API
javascript
别叫我->学废了->lol在线等3 小时前
演示 hasattr 和 ** 解包操作符
开发语言·前端·python
霍夫曼3 小时前
UTC时间与本地时间转换问题
java·linux·服务器·前端·javascript
DARLING Zero two♡3 小时前
浏览器里跑 AI 语音转写?Whisper Web + cpolar让本地服务跑遍全网
前端·人工智能·whisper
꒰ঌ小武໒꒱3 小时前
文件上传全维度知识体系:从基础原理到高级优化
javascript·node.js
Lovely Ruby3 小时前
前端er Go-Frame 的学习笔记:实现 to-do 功能(三),用 docker 封装成镜像,并且同时启动前后端数据库服务
前端·学习·golang
深红4 小时前
玩转小程序AR-实战篇
前端·微信小程序·webvr