列表

详情


CSS29. 三列布局 - 圣杯布局

描述

 不像Flexbox或Grid布局可以控制元素显示的次序,圣杯布局是通过浮动元素和外边距属性实现三列布局,但最重要的一点是,在文档中需要将优先渲染的内容写在最前方,但显示时看起来却好像是按照显示次序书写的一样。
 首先给类名为"container"的盒子添加"overflow: hidden"和"padding: 0px 100px"属性,以为了防止容器盒子高度塌陷和给之后的左、右浮动元素预留位置。现在继续给类名为"left"的盒子添加以下属性:
1. "float: left",浮动,保证之后的"margin-left"属性可以将自身拉到上一行
2. "width: 100px",固定宽度
3. "margin-left: -100%",该属性可以将元素向左移动属性值的单位,100%相对于父容器计算
4. "position: relative",相对定位,需要将自身再向左移动自身的宽度,进入容器的"padding-left"区域
5. "left: -100px",自身的宽度,刚好进入容器的"padding-left"区域
 到这里圣杯布局中最核心的步骤和思想就完了。之后继续给类名为"right"的盒子添加"float: left"、"width: 100px"、"margin-left: -100px"、"position: relative"和"left 100px"属性,该右盒子的思想和左盒子一样,即,将右盒子向上拉一行并且再向右移动自身宽度进入"padding-right"区。最后再给类名为"center"的盒子添加"float: left"和"width: 100%"即可。此时中间内容区的宽度是自适应的,并且因为有内边距属性所以内容不会被两边的浮动盒子遮挡住。
 圣杯布局需要注意的是,当中间内容区域的宽度小于左、右盒子的宽度时,整个布局就会混乱,所以为了避免这种情况,再给容器盒子添加"min-width: 100px"属性,保证圣杯布局正确、有效。
 完成以上所讲的步骤即可通过测试,进入下一节的学习吧。

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1688ms, 内存消耗: 77820KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            .container {\n                overflow: hidden;\n                padding: 0 100px;\n                min-width: 100px;\n                border: 1px solid black;\n            }\n            .left {\n                float: left;\n                width: 100px;\n                height: 50px;\n                margin-left: -100%;\n                position: relative;\n                left: -100px;\n                background-color: greenyellow;\n            }\n            .center {\n                float: left;\n                width: 100%;\n                background-color: darkorange;\n            }\n            .right {\n                float: left;\n                width: 100px;\n                height: 100px;\n                margin-left: -100px;\n                position: relative;\n                left: 100px;\n                background-color: darkgreen;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"center\"><br />center<br /><br /></article>\n            <article class=\"left\"><br />left<br /><br /></article>\n            <article class=\"right\"><br />right<br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1693ms, 内存消耗: 77812KB, 提交时间: 2022-02-08

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            .container {\n                overflow: hidden;\n                padding: 0 100px;\n                min-width: 100px;\n                border: 1px solid black;\n            }\n            .left {\n                float: left;\n                width: 100px;\n                margin-left: -100%;\n                position: relative;\n                left: -100px;\n                background-color: greenyellow;\n            }\n            .center {\n                float: left;\n                width: 100%;\n                background-color: darkorange;\n            }\n            .right {\n                float: left;\n                width: 100px;\n                margin-left: -100px;\n                position: relative;\n                left: 100px;\n                background-color: darkgreen;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"left\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77796KB, 提交时间: 2022-01-23

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            .container {\n                overflow:hidden;\n                padding:0 100px;\n                min-width: 100px;\n                border: 1px solid black;\n            }\n            .left {\n                float:left;\n                width:100px;\n                margin-left:-100%;\n                position:relative;\n                left:-100px;\n                background-color: greenyellow;\n            }\n            .center {\n                float:left;\n                width:100%;\n                background-color: darkorange;\n            }\n            .right {\n                float:left;\n                width:100px;\n                margin-left:-100px;\n                position:relative;\n                left:100px;\n                \n                background-color: darkgreen;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"left\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1746ms, 内存消耗: 77772KB, 提交时间: 2022-01-24

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            .container {\n                border: 1px solid black;\n                overflow:hidden;\n                padding:0px 100px;\n                min-width:100px;\n            }\n            .left {\n                background-color: greenyellow;\n                float:left;\n                width:100px;\n                margin-left:-100%;\n                position:relative;\n                left:-100px;\n            }\n            .center {\n                background-color: darkorange;\n                float:left;\n                width:100%;\n            }\n            .right {\n                background-color: darkgreen;\n                float:left;\n                width:100px;\n                margin-left:-100px;\n                position:relative;\n                left:100px;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"center\">11<br /><br /><br /></article>\n            <article class=\"left\">22<br /><br /><br /></article>\n            <article class=\"right\">33<br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1747ms, 内存消耗: 77784KB, 提交时间: 2022-01-22

{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n    <head>\n        <meta charset=utf-8>\n        <style type=\"text/css\">\n            * {\n                margin: 0;\n                padding: 0;\n            }\n            .container {\n                border: 1px solid black;\n                overflow: hidden;\n                padding: 0px 100px;\n                min-width: 100px;\n            }\n            .left {\n                background-color: greenyellow;\n                float: left;\n                width: 100px;\n              margin-left: -100%;\n                position: relative;\n                left:-100px;\n            }\n            .center {\n                background-color: darkorange;\n                float:left;\n                width:100%;  \n            }\n            .right {\n                background-color: darkgreen;\n                float: left;\n                width: 100px;\n               margin-left:-100px;\n                position: relative;\n               left:100px;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"left\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

上一题