列表

详情


CSS26. 三列布局 - 绝对定位

描述

 使用绝对定位实现三列布局,实际上是和之前使用绝对定位实现双列布局同一个原理。主要思路是,两边通过绝对定位定位到父盒子的左、右边框上,再根据实际的需要设置两边盒子的宽度,高度是根据内容自适应的。中间内容区通过定位属性左、右自适应宽度。
 现在给类名为"container"的盒子添加"position: relative",该属性使子元素可以相对该盒子做定位。继续给类名为"left"的盒子添加"position: absolute"、"left: 0px"和"width: 100px"三条属性。继续给类名为"right"的盒子添加"position: absolute"、"right: 0px"和"width: 100px"三条属性。最后给类名为"center"的盒子添加"position: absolute"、"left: 100px"和"right: 100px"三条属性,表示自适应区域为距离左边100px至距离右边100px。此时可以看到三个盒子的高度不同,根据内容高度撑开,实际中可以按需设置高度值。
 完成以上所讲的步骤即可通过测试,进入下一节的学习吧。

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1688ms, 内存消耗: 77808KB, 提交时间: 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                box-sizing: border-box;\n            }\n            .container{\n                position:relative;\n            }\n            .left {\n                border: 1px solid black;\n                position:absolute;\n                left:0;\n                width:100px;\n            }\n            .center {\n                border: 1px solid black;\n                position:absolute;\n                left:100px;\n                right:100px;\n            }\n            .right {\n                border: 1px solid black;\n                position:absolute;\n                right:0;\n                width:100px\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"left\"><br /><br /></article>\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

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

{"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                box-sizing: border-box;\n            }\n            .container{\n                position: relative;\n            }\n            .left {\n                position: absolute;\n                left: 0px;\n                width: 100px;\n                border: 1px solid black;\n            }\n            .center {\n                position:absolute;\n                left: 100px;\n                right: 100px;\n                border: 1px solid black;\n            }\n            .right {\n                position: absolute;\n                width: 100px;\n                right: 0px;\n                border: 1px solid black;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"left\"><br /><br /></article>\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77864KB, 提交时间: 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                box-sizing: border-box;\n            }\n            .left {\n                border: 1px solid black;\n                position:absolute;\n                left:0px;\n                width:100px;\n            }\n            .center {\n                border: 1px solid red;\n                position:absolute;\n                left:100px;\n                right:100px;\n            }\n            .right {\n                border: 1px solid blue;\n                position:absolute;\n                right:0;\n                width:100px;\n            }\n            .container{\n                position:relative;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"left\"><br /><br /></article>\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1744ms, 内存消耗: 77828KB, 提交时间: 2021-12-31

{"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                box-sizing: border-box;\n            }\n            .container {\n                position:relative;\n            }\n            .left {\n                border: 1px solid black;\n                position:absolute;\n                left:0;\n                width:100px;\n            }\n            .center {\n                border: 1px solid black;\n                position:absolute;\n                left:100px;\n                right:100px;\n                width:100px;\n            }\n            .right {\n                border: 1px solid black;\n                position:absolute;\n                right:0;\n                width:100px;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"left\"><br /><br /></article>\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

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

{"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                box-sizing: border-box;\n            }\n            .left {\n                border: 1px solid black;\n                position: absolute;\n                left: 0px;\n                width: 100px;\n            }\n            .center {\n                border: 1px solid black;\n                \n                position: absolute;\n                left: 100px;\n                right: 100px;\n            }\n            .right {\n                border: 1px solid black;\n                position: absolute;\n                right: 0px;\n                width: 100px;\n            }\n            .container{\n                position: relative;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"left\"><br /><br /></article>\n            <article class=\"center\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /><br /></article>\n        </section>\n    </body>\n</html>","libs":[]}

上一题