列表

详情


CSS25. 三列布局 - 浮动

描述

 使用浮动实现三列布局的注意点是浮动元素需要写在内容元素之前,否则布局是混乱的。这种方式实现三列布局是优点是简单、兼容性好,但缺点是需要清除浮动,否则父盒子的高度无法撑开,可能会导致其他页面元素的布局混乱。
 首先给类名为"container"的盒子添加"overflow: hidden"属性,该属性可以使盒子成为BFC,处理浮动元素父盒子高度塌陷的问题。再给类名为"left"的盒子添加"float: left"和"width: 100px"两条属性,首先往左浮动,宽度这里设置100px。继续给类名为"right"的盒子添加"float: right"和"width: 100px"两条属性,往右浮动。最后给类名为"center"的盒子设置外边距"margin: 0px 100px",该属性上下外边距为0px,左右外边距为两边浮动元素的宽度100px,这样保证了中间的内容区域不会被两边的浮动元素覆盖住。注意,HTML文档中两个浮动元素的顺序在内容元素之前。
 完成以上所讲的步骤即可通过测试,进入下一节的学习吧。

原站题解

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

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

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

{"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                border: 1px solid black;\n                overflow: hidden;\n            }\n            .left {\n                border: 1px solid black;\n                float: left;\n                width: 100px;\n            }\n            .right {\n                border: 1px solid black;\n                float: right;\n                width: 100px;\n            }\n            .center{\n                margin: 0 100px;\n                background: red;\n            }\n        </style>\n    </head>\n    <body>\n    \t<section class=\"container\">\n            <article class=\"left\"><br /><br /><br /></article>\n            <article class=\"right\"><br /><br /><br /></article>\n            <article class=\"center\"><br><br><br></article>\n        </section>\n    </body>\n</html>","libs":[]}

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

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

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

上一题