列表

详情


CSS15. display - flex - 可伸缩项属性

描述

 Flexbox支持对弹性项的灵活控制。Flex的意思为可伸缩,这体现在以下三个属性中:
1. flex-basis:基础值
2. flex-grow:拉伸弹性系数,如果容器宽度减去弹性项的基础值之和之后还有剩余空间,那么就按照弹性系数比例去分配剩余空间
3. flex-shrink:缩减弹性系数,和拉伸弹性系数逻辑相反
 这三个属性应用给弹性项,而不是容器。
 现在首先给所有的"li"添加"flex: 1 0 0%"属性,该属性的三个值分别为flex-grow、flex-shrink和flex-basis,表示:当有剩余空间时均匀分配剩余空间、当超出容器宽度时不进行缩放、弹性项的基础值都为容器的0%。此时可以看到四个每个"li"标签的宽度都为125px,分别占据了容器的1/4。现在再单独给第一个"li"标签设置"flex-grow: 2"属性,此时又会发现所有"li"标签的宽度比值为2:1:1:1。
 完成以上所讲的步骤即可通过测试,进入下一节的学习吧。

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1697ms, 内存消耗: 77804KB, 提交时间: 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            }\n            nav {\n                width: 500px;\n            }\n            nav ul {\n                display: flex;\n                list-style: none;\n            }\n            nav ul li {\n                flex: 1 0 0%;\n            }\n            nav ul li:first-child{\n                flex-grow:2\n            }\n        </style>\n    </head>\n    <body>\n    \t <nav>\n            <ul>\n                <li>home</li>\n                <li>spaceships</li>\n                <li>planets</li>\n                <li>stars</li>\n            </ul>\n        </nav>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1738ms, 内存消耗: 77804KB, 提交时间: 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            }\n            nav {\n                width: 500px;\n            }\n            nav ul {\n                display: flex;\n                list-style: none;\n            }\n            li {\n                flex:1 0 0%;\n            }\n            li:first-child {\n                flex-grow:2;\n            }\n        </style>\n    </head>\n    <body>\n    \t <nav>\n            <ul>\n                <li>home</li>\n                <li>spaceships</li>\n                <li>planets</li>\n                <li>stars</li>\n            </ul>\n        </nav>\n    </body>\n</html>","libs":[]}

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

{"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            nav {\n                width: 500px;\n            }\n            nav ul {\n                display: flex;\n                list-style: none;\n            }\n            ul li{\n                flex:1 0 0%;\n            }\n          ul li:nth-child(1){\n                flex-grow:2;\n            }\n        </style>\n    </head>\n    <body>\n    \t <nav>\n            <ul>\n                <li>home</li>\n                <li>spaceships</li>\n                <li>planets</li>\n                <li>stars</li>\n            </ul>\n        </nav>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1747ms, 内存消耗: 77772KB, 提交时间: 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            nav {\n                width: 500px;\n            }\n            nav ul {\n                display: flex;\n                list-style: none;\n            }\n            nav ul li{\n                flex:100%;\n                flex-shrink:0;\n                flex-basis:0%;\n            }\n            nav ul li:nth-child(1){\n                flex-grow:2;\n            }\n        </style>\n    </head>\n    <body>\n    \t <nav>\n            <ul>\n                <li>home</li>\n                <li>spaceships</li>\n                <li>planets</li>\n                <li>stars</li>\n            </ul>\n        </nav>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1749ms, 内存消耗: 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            nav {\n                width: 500px;\n            }\n            nav ul {\n                display: flex;\n                list-style: none;\n            }\n            nav ul li{\n                flex:1 0 0%;\n            }\n            .a{\n               flex-grow:2; \n            }\n        </style>\n    </head>\n    <body>\n    \t <nav>\n            <ul>\n                <li class='a'>home</li>\n                <li>spaceships</li>\n                <li>planets</li>\n                <li>stars</li>\n            </ul>\n        </nav>\n    </body>\n</html>","libs":[]}

上一题