search
HomeWeb Front-endHTML Tutorial圣杯布局小结_html/css_WEB-ITnose

圣杯布局,很久之前就听过,但是一直都没详细了解过,最近因为做了一个项目,借鉴了薪人薪事这个公司的产品页面,才第一次用到这种布局方式。于是就花了点时间,测了下它所有分栏布局的代码,每段代码都非常简单,但布局效果很完美,比我以前用的方式好用不少。本文是对它实现方式的一些总结,希望可以把这种技术推荐给跟我之前一样对它比较陌生的开发人员:)

1. 从2个实际的需求说起

今年有2个项目,都是管理系统的项目,这种项目的界面特点基本都是左侧边栏显示菜单,右侧显示网页主体或者是顶部的导航栏显示菜单,导航栏以下显示网页主体,我这两个项目都是第一种类型:

项目一:

项目二:

在做项目一的时候,采用了以前做ERP软件的一些做法,右边的网页主体区域放置的是一个iframe,用来显示每个菜单点击之后的页面,这样每个菜单点击之后,外部页面都不会刷新,并且滚动也只发生在iframe里面,外部页面的菜单区域和顶部导航栏的状态始终不会改变,用户操作起来非常便捷。这种界面布局的做法非常简单,只要侧边栏和网页主体区域都采用固定定位即可:

<div class="sidebar"></div><div class="page-content"></div>.sidebar {    position: absolute;    width: 200px;    left: 0;    bottom: 0;    top: 50px;    border-right: 1px solid #E7E7E7;}.page-content {    position: absolute;    left: 205px;    bottom: 0;    top: 50px;    right: 0;}

由于这个项目是一个内部项目,所以采用这种界面结构完全是可以接受的,毕竟这只是一个管理系统,可以不那么在乎用户体验什么的。最近做项目二的时候,情况就不一样了,这个项目是一个企业级的管理应用,它不再是一个单纯的管理系统,而是面向外部用户提供的参与平台业务的一个终端应用,从用户体验的角度来说,项目一那种固定死板的方式不太拿得出手给别人用,不然别人肯定会认为你的应用做的很low。相对于项目一的界面,项目二有以下特点:

1)菜单点击之后,界面是整体刷新,没有iframe了;

2)侧边栏和主体内容栏的高度都是不固定的;

3)网页滚动的时候,是页面整体滚动,而不是只滚动主体内容。

几个礼拜前,看到薪人薪事融资的新闻,心想这是个什么公司,怎么之前都没听过,做什么业务的,于是就百度了下,注册了账号,进去体验了一下它的产品,后来发现它做的其实是一个SAAS应用,界面上看是一个典型的管理系统的风格,但是整体体验还不错,当时就觉得以后说不定有参考借鉴的价值。正好上周临时安排要做项目二,根据项目一提了一些要求,于是就想到薪人薪事的应用了。只有3天时间,工作除了界面之外,还有4个业务模块的功能要完成,为了完成这个东西,界面布局完全参考了薪人薪事的做法,由于以前没用过这种布局方式,所以觉得很新奇,就专门搜集知识学习了一下,才发现这个方法就是以前听过的圣杯布局。项目二所用的布局方法就是圣杯布局方式中侧边栏固定,主体内容栏自适应的一种做法。

2. 圣杯布局的传统实现方法

利用圣杯布局的方法,可以轻松实现下面的布局效果:

下面来一一说明上图中五种布局效果的实现方法(本文相关代码下载)。

1)布局一:2栏布局,侧边栏固定在左边,右侧是主体内容栏:

<div class="layout">    <aside class="layout__aside">侧边栏宽度固定</aside>    <div class="layout__main">主内容栏宽度自适应</div></div>
<style type="text/css">    .layout:after, .layout:before {        clear: both;        content: " ";        display: table;    }    .layout__aside, .layout__main {        float: left;    }    .layout {        padding-left: 210px;    }    .layout__main {        width: 100%;    }    .layout__aside {        width: 200px;        margin-left: -210px;    }</style>

效果是:

2)布局二:2栏布局,侧边栏固定在右边,左侧是主体内容栏:

<div class="layout">    <div class="layout__main">主内容栏宽度自适应</div>    <aside class="layout__aside">侧边栏宽度固定</aside></div>
<style type="text/css">    .layout:after, .layout:before {        clear: both;        content: " ";        display: table;    }    .layout {        padding-right: 210px;    }    .layout__main {        width: 100%;        float: left;    }    .layout__aside {        float: right;        width: 200px;        margin-right: -210px;    }</style>

效果是:

3)布局三:3栏布局,2个侧边栏分别固定在左边和右边,中间是主体内容栏:

<div class="layout">    <aside class="layout__aside layout__aside--left">左侧边栏宽度固定</aside>    <div class="layout__main">主内容栏宽度自适应</div>    <aside class="layout__aside layout__aside--right">右侧边栏宽度固定</aside></div>
<style type="text/css">    .layout:after, .layout:before {        clear: both;        content: " ";        display: table;    }    .layout__aside, .layout__main {        float: left;    }    .layout {        padding:0 210px;    }    .layout__main {        width: 100%;    }    .layout__aside {        width: 200px;    }    .layout__aside--left {        margin-left: -210px;    }    .layout__aside--right {        margin-right: -210px;        float: right;    }</style>

效果是:

4)布局四:3栏布局,2个侧边栏同时固定在左边,右边是主体内容栏:

<div class="layout">    <aside class="layout__aside layout__aside--first">第1个侧边栏宽度固定</aside>    <aside class="layout__aside layout__aside--second">第2个侧边栏宽度固定</aside>    <div class="layout__main">主内容栏宽度自适应</div></div>
<style type="text/css">    .layout:after, .layout:before {        clear: both;        content: " ";        display: table;    }    .layout__aside, .layout__main {        float: left;    }    .layout {        padding-left: 420px;    }    .layout__main {        width: 100%;    }    .layout__aside {        width: 200px;    }    .layout__aside--first {        margin-left: -420px;    }    .layout__aside--second {        margin-left: -210px;    }</style>

效果是:

5)布局五:3栏布局,2个侧边栏同时固定在右边,左边是主体内容栏:

<div class="layout">    <div class="layout__main">主内容栏宽度自适应</div>    <aside class="layout__aside layout__aside--first">第1个侧边栏宽度固定</aside>    <aside class="layout__aside layout__aside--second">第2个侧边栏宽度固定</aside></div>
<style type="text/css">    .layout:after, .layout:before {        clear: both;        content: " ";        display: table;    }    .layout {        padding-right: 420px;    }    .layout__main {        width: 100%;        float: left;    }    .layout__aside {        width: 200px;        float: right;    }    .layout__aside--first {        margin-right: -210px;    }    .layout__aside--second {        margin-right: -420px;    }</style>

效果是:

PS:

1)本文提供的这些布局方法,比网上看到的更加简洁一些,主要是因为不考虑兼容IE8及以下,不考虑把layout__main这个元素放在最前面,虽然经典的做法都要求把layout__main做法放在前面,这样可以让网页主体内容优先渲染,我认为这种考虑是完全多余的,2个元素的渲染顺序不同,实际上的用户体验差别又有多大呢,为了一个肉眼看不到的差异,而采用更复杂的做法,不值得;

2)css布局类的命名采用了BEM的命名规则,这个可以帮助你写出结构化,规范化的css,有兴趣的可以去了解:

http://www.zhihu.com/question/21935157

3. 圣杯布局的flex实现

flex布局是一种新的网页布局方式,目前的兼容性如下:

如果你还没有了解过flex布局,建议你赶紧去学习,虽然它在pc上兼容性还有点问题,但是在移动端已经完全没有问题了,微信官方推出的weui这个框架就大量地使用了这种布局,以下是2个学习这种布局方式的非常好的资源:

http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html

http://www.ruanyifeng.com/blog/2015/07/flex-examples.html

flex布局即将成为网页布局的首选方案,当你看到用flex来实现圣杯布局的代码有多简单的时候,你就会觉得前面那句话一点都没错。使用flex,可以只用同一段css实现第2部分提到的五种布局:

<div class="layout">    <aside class="layout__aside">侧边栏宽度固定</aside>    <div class="layout__main">主内容栏宽度自适应</div></div><div class="layout">    <div class="layout__main">主内容栏宽度自适应</div>    <aside class="layout__aside">侧边栏宽度固定</aside></div>
主内容栏宽度自适应
主内容栏宽度自适应
主内容栏宽度自适应
<style type="text/css">    .layout {        display: flex;    }    .layout__main {        flex: 1;    }    .layout__aside {        width: 200px;    }    .layout > .layout__aside:not(:first-child),    .layout > .layout__main:not(:first-child){        margin-left: 10px;    }</style>

效果与第2部分每种布局做法的结果一模一样,但是代码减少了很多,而且适用的场景更多,比如4栏布局,5栏布局。

4. 结束语

本文介绍了2种圣杯布局的方法,每种方法在当前或者将来的工作中,肯定会有很多场景都需要使用,所以有必要完全掌握这2种技术,内容不多,也不复杂,希望能对你有用,谢谢阅读:)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
HTML: Is It a Programming Language or Something Else?HTML: Is It a Programming Language or Something Else?Apr 15, 2025 am 12:13 AM

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

HTML: Building the Structure of Web PagesHTML: Building the Structure of Web PagesApr 14, 2025 am 12:14 AM

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.

From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

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.

Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

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

The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

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.

HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

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

HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

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.

The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

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.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

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.