前言
在使用 CSS 进行布局的时候,我们通常会用到 div 布局和 float padding margin position 之类的CSS属性,使用不同的组合可以得出很多的布局方案。本篇介绍的是三栏布局方案中的双飞翼布局和圣杯布局,它们解决的问题是一样的:左侧和右侧的部分宽度固定,中间部分宽度随浏览器宽度的变化而自适应变化。它们的实现方案在前半部分大同小异,不同之处是如何使中间部分的内容不被两侧遮挡。
准备阶段
此部分属于双飞翼布局和圣杯布局实现方案的前半部分,实现原理是利用 float 和 negative margin 技术。可以按以下步骤进行:
-
按 middle left right 的顺序写出三个 div ,设置 left 和 right 固定宽度分别为 190px , 220px (可以给这三栏都加上 min-height ,让它们的初始高度一致。还可以给作为容器的 container 加上 min-width ,以避免窗口宽度缩小时,中间部分消失的问题)
-
给这三栏都加上 float: left
-
给 middle 加上 width: 100% ,这时候 left 和 right 会被挤下去
-
给 left 加上 margin-left: -100% ,拉到 middle 的左边
-
给 right 加上 margin-left: -220 px ,拉到 middle 的右边
在准备阶段完成后,中间部分的内容会被两侧遮挡到,接下来分别介绍两种布局对于该问题的解决方案。
解决方案
双飞翼布局
给 middle 加一层包裹 middle-wrap ,并设置其 margin: 0 220px 0 190px
以下是完整的代码实现:
<!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>双飞翼布局</title> <styletype="text/css"> *{ margin: 0; padding: 0; } .middle, .left, .right{ float: left; min-height: 200px; } .middle{ width: 100%; background-color: green; } .middle-wrap{ margin: 0 220px 0 190px; } .left{ width: 190px; margin-left: -100%; background-color: red; } .right{ width: 220px; margin-left: -220px; background-color: blue; } .container{ min-width: 600px; } .header, .footer{ text-align: center; background-color: yellow; } .footer{ clear: both; } </style></head> <body> <divclass="header"> <h2 id="header">header</h2> </div> <divclass="container"> <divclass="middle"> <divclass="middle-wrap"> <h2 id="middle">middle</h2> </div> </div> <divclass="left"> <h2 id="left">left</h2> </div> <divclass="right"> <h2 id="right">right</h2> </div> </div> <divclass="footer"> <h2 id="footer">footer</h2> </div></body> </html>
圣杯布局
-
给这三栏的容器 container 加上 padding: 0 220px 0 190px,这会使整个容器左右两边各留出 190px 和 220px 的 padding 。
-
对 left 和 right 设置相对定位 position: relative 和偏移量,从而将它们移动到左右两边的 padding 中。
以下是完整的代码实现:
<!DOCTYPEhtml><htmllang="en"> <head> <metacharset="UTF-8"> <title>圣杯布局</title> <styletype="text/css"> *{ margin: 0; padding: 0; } .middle, .left, .right{ float: left; min-height: 200px; } .left, .right{ position: relative; } .middle{ width: 100%; background-color: green; } .left{ width: 190px; margin-left: -100%; left: -190px; background-color: red; } .right{ width: 220px; margin-left: -220px; right: -220px; background-color: blue; } .container{ padding: 0 220px 0 190px; min-width: 600px; } .header, .footer{ text-align: center; background-color: yellow; } .footer{ clear: both; } </style></head> <body> <divclass="header"> <h2 id="header">header</h2> </div> <divclass="container"> <divclass="middle"> <h2 id="middle">middle</h2> </div> <divclass="left"> <h2 id="left">left</h2> </div> <divclass="right"> <h2 id="right">right</h2> </div> </div> <divclass="footer"> <h2 id="footer">footer</h2> </div></body> </html>
结论
-
两种布局都可以实现中间内容最先加载。
-
相比圣杯布局,双飞翼布局多创建了一个 div ,但不用相对定位。双飞翼布局兼容性很好,可以兼容到IE6。此处的圣杯布局经个人测试,可以兼容到IE8。
参考文章
CSS布局 — 圣杯布局 & 双飞翼布局
双飞翼布局介绍-始于淘宝UED
圣杯布局
CSS布局终极方案之–圣杯布局
HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

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.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version
Useful JavaScript development tools