三栏布局的n种实现
本文主要讨论左右边栏固定宽度,中间栏填满其余空间的布局。至于其他类型,基本上也就是半斤和八两。每一种布局都会有个Demo,个人依然认为文章里帖代码并没有Demo来的直接。所以正文负责解释,源码参见Demo。其中讨论了这么多种(6种)布局,有以下理由:1是每种布局也都有他的毛病,没有十全十美的,每种布局也都有人在用。2是虽然有相对优秀的方案,但是不优秀的方案也有有用的东西在里边,可能会启发其他的思路补充遗漏的知识点。
- 利用绝对定位特点:左右两栏采用绝对定位抽离文档流,分别固定于页面的左右两侧,中间栏用左右margin值撑开可以容纳左右边栏的距离。这个很简单不多解释。Demo:http://www.dabao.love/demo/layoutDemo/absoluteLayout.html
- 利用两侧浮动都不占据文档流:左栏左浮动,右栏右浮动,中间栏左右margin值等于左右栏宽度。html中,中间栏要放在左右边栏的后边。道理上和绝对定位差不多,就是不占文档流了其他元素就填充上了,只要把两边留出空间不导致重合即可。Demo:http://www.dabao.love/demo/layoutDemo/twoSidesFloatLayoutDemo.html
- 利用浮动和margin负值的特点:标签顺序为
middle 宽度 100%,sub 左右 margin 为左右栏留出空间,其中 sub 元素不要浮动,否则会包裹里面的内容而不会撑满空间,其他元素全部左浮动。left 的 margin-left 为-100%,right 的margin-left为sub的负的margin-right 。浮动的特点就是紧跟着前一个元素,放不下了就换行,本来middle元素占满了第一行,sub元素的margin留出的空间并不会给其他的浮动元素,左栏被迫换行,处于第二行最左侧,相当于紧跟着第一行后边,100%的负margin-left会让他从紧跟着第一行变成第一行最左边,这时左栏和sub元素的margin-left留出的空间重合,达到目的,此时右栏在原来左栏的位置,再用类似的处理方式,达到最终效果。其中sub的margin可以换为padding,相同的原理和效果。此处如果不需要撑满空间(有内容撑满sub)可以不要父元素(即middle)。Demo:http://www.dabao.love/demo/layoutDemo/floatLayoutDemo.html
- 利用inline-block特点:父元素包含左中右栏:(子元素顺序中,左,右)父元素设置padding为左右栏留出空间,然后中栏宽度100%占据除padding外的空间,左右栏利用margin-left等于自身宽的负值使其与中栏右边界重合,再调节left,左栏left为-100%右栏left为右栏宽度。左中右栏全部为子元素,所以margin-left无法占据padding空间,需要左右边栏相对定位后调解left值达到目标状态。其中注意父元素设置font-size: 0; letter-spacing: -4px; 子元素再重置这两个属性。Demo:http://www.dabao.love/demo/layoutDemo/inlineBlockLayoutDemo.html
- 利用calc属性和inline-block特点:(同样是父元素包含左中右栏)
父元素设置: text-align:center; font-size: 0; letter-spacing: -4px;
左中右栏再重置: font-size: 16px; letter-spacing: normal;
左右栏固定宽度, html中元素顺序为:左,中,右 ,假设左右栏宽度和300px;
中间拦: width:calc(100% - 300px);
百分比与固定宽度混合布局使用,支持ie9+,注意+-*/号两边留空格
Demo:http://www.dabao.love/demo/layoutDemo/useCalcLayoutDemo.html
- 利用flex布局:flex布局我认为是最先进而方便的布局,非常灵活不过内容也不少,只可惜兼容ie10+。不在这多废话,推荐阮一峰老师的教程:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html?utm_source=tuicool 父元素display:flex; ,左右两栏设置flex-basis 预置宽度,中间栏flex-grow: 1;自动伸展。完活。Demo:http://www.dabao.love/demo/layoutDemo/flexLayoutDemo.html
如有纰漏欢迎批评指正,没有纰漏也欢迎拍砖。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTML, CSS and JavaScript are the core technologies for building modern web pages: 1. HTML defines the web page structure, 2. CSS is responsible for the appearance of the web page, 3. JavaScript provides web page dynamics and interactivity, and they work together to create a website with a good user experience.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

The article discusses the differences between HTML tags , , , and , focusing on their semantic vs. presentational uses and their impact on SEO and accessibility.
