三栏布局----方法总结

2023-10-19 17:59
文章标签 总结 方法 布局 三栏

本文主要是介绍三栏布局----方法总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

三栏布局介绍:

三栏布局,顾名思义就是两边固定宽度,中间内容宽度岁浏览器宽度自适应。如,打开某东首页:



三栏布局方法:

方法一:浮动布局

注:  浮动元素脱离了文档流,根据实际情况使用时清除浮动。 

<!DOCTYPE html>
<html lang="en">
<head><style>h1 {text-align: center;}.container {width: 100%;height: 200px;}.left {width: 200px;height: 100%;background-color: red;float: left;}.right {width: 200px;height: 100%;background-color: red;float: right;}.main {height: 100%;margin-left: 200px;margin-right: 200px;background-color: yellow;}</style>
</head>
<body><h1>三栏布局---第一种浮动布局</h1><div class="container"><div class="left"></div><div class="right"></div><div class="main"></div></div>
</body>
</html>
方法二:绝对定位布局
简单实用,并且主要内容可以优先加载。

<!DOCTYPE html>
<html lang="en">
<head><style>h1 {text-align: center;}.container {width: 100%;height: 200px;position: relative;}.left {width: 200px;height: 100%;background-color: red;position: absolute;left: 0;top: 0;}.right {width: 200px;height: 100%;background-color: red;position: absolute;right: 0;top: 0;}.main {height: 100%;margin-left: 200px;margin-right: 200px;background-color: yellow;}</style>
</head>
<body><h1>三栏布局---第二种绝对定位布局</h1><div class="container"><div class="left"></div><div class="right"></div><div class="main"></div></div>
</body>
</html>
方法三:BFC布局

注:BFC 规则有这样的描述:BFC 区域,不会与浮动元素重叠。因此我们可以利用这一点来实现 3 列布局。缺点:主要内容模块无法最先加载,当页面中内容较多时会影响用户体验。因此为了解决这个问题,有了下面要介绍的布局方案双飞翼布局。


<!DOCTYPE html>
<html lang="en">
<head><style>h1 {text-align: center;}.container {width: 100%;height: 200px;}.left {width: 200px;height: 100%;background-color: red;float: left;}.right {width: 200px;height: 100%;background-color: red;float: right;}.main {height: 100%;overflow: hidden;background-color: yellow;}</style>
</head>
<body><h1>三栏布局---第三种BFC(块级格式上下文)布局</h1><div class="container"><div class="left"></div><div class="right"></div><div class="main"></div></div>
</body>
</html>
方法四:双飞翼布局

主体内容可以优先加载,HTML 代码结构稍微复杂点。


<!DOCTYPE html>
<html lang="en">
<head><style>.content {float: left;width: 100%;}.main {height: 200px;margin-left: 200px;margin-right: 200px;background-color: green;}.left {float: left;height: 200px;width: 200px;margin-left: -100%;background-color: red;}.right {width: 200px;height: 200px;float: right;margin-left: -200px;background-color: blue;}</style>
</head>
<body><h1>三栏布局---第四种双飞翼布局</h1><div class="content"><div class="main"></div></div><div class="left"></div><div class="right"></div>
</body>
</html>
方法五:圣杯布局

和双飞翼布局很像,有一些细节上的区别,相对于双飞翼布局来说,HTML 结构相对简单,但是样式定义就稍微复杂,也是优先加载内容主体。


<!DOCTYPE html>
<html lang="en">
<head><style>.container {margin-left: 200px;margin-right: 200px;}.main {float: left;width: 100%;height: 200px;background-color: red;}.left {float: left;width: 200px;height: 200px;margin-left: -100%;position: relative;left: -200px;background-color: blue;}.right {float: left;width: 200px;height: 200px;margin-left: -200px;position: relative;right: -200px;background-color: green;}</style>
</head>
<body><h1>三栏布局---第五种圣杯布局</h1><div class="container"><div class="main"></div><div class="left"></div><div class="right"></div></div>
</body>
</html>
方法六:flex布局

需要考虑浏览器的兼容性

<!DOCTYPE html>
<html lang="en">
<head><style>.container {display: flex;}.main {flex-grow: 1;height: 200px;background-color: red;}.left {order: -1;flex: 0 1 200px;height: 200px;background-color: blue;}.right {flex: 0 1 200px;height: 200px;background-color: green;}</style>
</head>
<body><h1>三栏布局---第六种flex布局</h1><div class="container"><div class="main"></div><div class="left"></div><div class="right"></div></div>
</body>
</html>
方法七:表格布局

缺点:无法设置栏间距

<!DOCTYPE html>
<html lang="en">
<head><style>.container {display: table;width: 100%;}.left,.main,.right {display: table-cell;}.left {width: 200px;height: 200px;background-color: red;}.main {background-color: blue;}.right {width: 200px;height: 200px;background-color: green;}</style>
</head>
<body><h1>三栏布局---第七种表格布局</h1><div class="container"><div class="left"></div><div class="main"></div><div class="right"></div></div>
</body>
</html>

以上是我整理的关于三栏布局的方法,欢迎大家补充。提出问题。谢谢。


这篇关于三栏布局----方法总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/241430

相关文章

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

Python中判断对象是否为空的方法

《Python中判断对象是否为空的方法》在Python开发中,判断对象是否为“空”是高频操作,但看似简单的需求却暗藏玄机,从None到空容器,从零值到自定义对象的“假值”状态,不同场景下的“空”需要精... 目录一、python中的“空”值体系二、精准判定方法对比三、常见误区解析四、进阶处理技巧五、性能优化

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行

C++中初始化二维数组的几种常见方法

《C++中初始化二维数组的几种常见方法》本文详细介绍了在C++中初始化二维数组的不同方式,包括静态初始化、循环、全部为零、部分初始化、std::array和std::vector,以及std::vec... 目录1. 静态初始化2. 使用循环初始化3. 全部初始化为零4. 部分初始化5. 使用 std::a

如何将Python彻底卸载的三种方法

《如何将Python彻底卸载的三种方法》通常我们在一些软件的使用上有碰壁,第一反应就是卸载重装,所以有小伙伴就问我Python怎么卸载才能彻底卸载干净,今天这篇文章,小编就来教大家如何彻底卸载Pyth... 目录软件卸载①方法:②方法:③方法:清理相关文件夹软件卸载①方法:首先,在安装python时,下

电脑死机无反应怎么强制重启? 一文读懂方法及注意事项

《电脑死机无反应怎么强制重启?一文读懂方法及注意事项》在日常使用电脑的过程中,我们难免会遇到电脑无法正常启动的情况,本文将详细介绍几种常见的电脑强制开机方法,并探讨在强制开机后应注意的事项,以及如何... 在日常生活和工作中,我们经常会遇到电脑突然无反应的情况,这时候强制重启就成了解决问题的“救命稻草”。那

kali linux 无法登录root的问题及解决方法

《kalilinux无法登录root的问题及解决方法》:本文主要介绍kalilinux无法登录root的问题及解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录kali linux 无法登录root1、问题描述1.1、本地登录root1.2、ssh远程登录root2、

SpringMVC获取请求参数的方法

《SpringMVC获取请求参数的方法》:本文主要介绍SpringMVC获取请求参数的方法,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下... 目录1、通过ServletAPI获取2、通过控制器方法的形参获取请求参数3、@RequestParam4、@

Python中的魔术方法__new__详解

《Python中的魔术方法__new__详解》:本文主要介绍Python中的魔术方法__new__的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、核心意义与机制1.1 构造过程原理1.2 与 __init__ 对比二、核心功能解析2.1 核心能力2.2

Python Transformer 库安装配置及使用方法

《PythonTransformer库安装配置及使用方法》HuggingFaceTransformers是自然语言处理(NLP)领域最流行的开源库之一,支持基于Transformer架构的预训练模... 目录python 中的 Transformer 库及使用方法一、库的概述二、安装与配置三、基础使用:Pi