列表

详情


CSS14. display - flex

描述

 Flexbox,也就是Flexible Box Layout模块,是CSS提供的用于布局的一套新属性。这套属性包含针对容器、容器直接子元素(弹性项)的两类属性。Flexbox可以控制弹性项的这几个方面:
1. 大小,基于内容以及可用空间
2. 流动方向,水平还是垂直,正向还是反向
3. 两个轴向上的对齐与分布
4. 顺序,与源代码中的顺序无关
 现在给"ul"标签添加"display: flex"属性,让该列表成为flex容器,注意:除非另有声明,否则该属性默认弹性项排列方向为正横向,即"flex-direction: row"。现在可以看到,列表内容已经是水平排列,并且根据各自的内容宽度进行了收缩。现在对主轴(即"flex-direction"属性声明的轴向)进行排列,该属性为"justify-content"且默认值为"flex-start",现在给"ul"标签设置"justify-content: center"属性,可以发现弹性项在主轴上已经居中了。但是此时会发现由于"home"的字体比其他字体大,所以弹性项在辅轴(与主轴对应的另一方向的轴)上并没有对其,这是由于控制辅轴对齐方式的"align-items"属性默认为"stretch"即拉伸,会占满"ul"的所有高度空间,并且该行的基线是默认的。现在再给"ul"标签添加"align-items: center"属性,会发现"li"标签此时没有撑满"ul"标签的高度,并且在辅轴上是居中对齐的,如果给该属性设置"baseline"值,仅仅会在辅轴上将内容文字对齐。
 以上所提到的容器属性都有多个属性值,可查阅官网。完成以上所讲的步骤即可通过测试,进入下一节的学习吧。

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1690ms, 内存消耗: 77820KB, 提交时间: 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            ul {\n                display: flex;\n                justify-content: center;\n                align-items: center;\n            }\n            nav ul {\n                height: 2rem;\n                list-style: none;\n            }\n            nav ul li {\n                border: 1px solid black;\n            }\n            nav ul li:first-child {\n                font-size: 1.2rem;\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 解法, 执行用时: 1744ms, 内存消耗: 77772KB, 提交时间: 2022-01-04

{"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            ul{\n                display:flex;\n                justify-content:center;\n                align-items:center \n                \n            }\n            nav ul {\n                height: 2rem;\n                list-style: none;\n            }\n            nav ul li {\n                border: 1px solid black;\n            }\n            nav ul li:first-child {\n                font-size: 1.2rem;\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 解法, 执行用时: 1744ms, 内存消耗: 77772KB, 提交时间: 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                box-sizing: border-box;\n            }\n            nav ul {\n                height: 2rem;\n                list-style: none;\n                display:flex;\n                justify-content: center;\n                align-items: center;\n            }\n            nav ul li {\n                border: 1px solid black;\n            }\n            nav ul li:first-child {\n                font-size: 1.2rem;\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 解法, 执行用时: 1744ms, 内存消耗: 77900KB, 提交时间: 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            nav ul {\n                height: 2rem;\n                list-style: none;\n                background-color:#bfa;\n                display:flex;\n                justify-content: center;\n                align-items: center\n            }\n            nav ul li {\n                border: 1px solid black;\n            }\n            nav ul li:first-child {\n                font-size: 1.2rem;\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 解法, 执行用时: 1744ms, 内存消耗: 77904KB, 提交时间: 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            nav ul {\n                height: 2rem;\n                list-style: none;\n                display: flex;\n                flex-direction: row;\n                justify-content: center;\n                align-items:center;\n            \n            }\n            nav ul li {\n                border: 1px solid black;\n            }\n            nav ul li:first-child {\n                font-size: 1.2rem;\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":[]}

上一题