[关闭]
@wpcfan 2024-08-02T20:39:59.000000Z 字数 15760 阅读 100

CSS基本功从头练之布局

css html web flex


CSS的display属性是控制元素布局的核心属性之一。虽然这个属性已经存在很久,但很多人(包括我自己)都没有系统地去学习过它。在这篇文章中,我将系统地梳理一下display属性的各种值,并通过实例来演示它们的效果。希望这篇文章能对大家有所帮助。

block

display: block 元素占据其父元素的整个宽度,并且会自动换行。它们通常用于结构性布局和容器元素。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .block {
  6. display: block;
  7. width: 100%;
  8. background-color: #DCEDC8;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div class="block">这是一个块级元素</div>
  14. <p class="block">这是另一个块级元素</p>
  15. </body>
  16. </html>

Block显示效果

在这个示例中,两个元素都被设置为 block,因此它们各自独占一行,并填满父容器的宽度。

常见的场景:

  1. 布局容器
    用于包含和排列其他元素,如 <div><section><article> 等。

  2. 标题
    用于定义文档结构,如 <h1><h6> 标签。

  3. 段落
    用于包含文本内容,如 <p> 标签。

  4. 列表
    用于创建列表,如 <ul><ol><li> 标签。

  5. 表单元素
    用于创建表单的结构和布局,如 <form><fieldset><legend> 标签。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .block-element {
  6. background-color: #e0f7fa;
  7. padding: 20px;
  8. margin: 10px 0;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div class="block-element">This is a block-level element.</div>
  14. <h1 class="block-element">This is a heading.</h1>
  15. <p class="block-element">This is a paragraph.</p>
  16. <ul class="block-element">
  17. <li>List item 1</li>
  18. <li>List item 2</li>
  19. <li>List item 3</li>
  20. </ul>
  21. </body>
  22. </html>

Block 级别元素

inline

inlinedisplay属性的另一个重要值。将元素的display属性设置为inline,该元素将与相邻的元素在同一行显示。常见的行内元素包括<span><a><strong>等。如下所示:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CSS Display</title>
  6. <style>
  7. .inline-element {
  8. display: inline;
  9. background-color: #d9edf7;
  10. margin: 10px;
  11. padding: 10px;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <span class="inline-element">这是一个行内元素。</span>
  17. <a href="#" class="inline-element">这是另一个行内元素。</a>
  18. </body>
  19. </html>

inline 效果

在上面的示例中,<span><a>元素都被设置为行内元素,它们在同一行内显示。

display: inline 元素只占据其内容的宽度,并且不会自动换行。它们通常用于文本内联和小型结构性元素。

常见的场景:

  1. 文本格式化
    用于格式化文本,如 <span><a><em><strong> 标签。

  2. 图标
    用于插入图标或小图形,如 <img> 标签。

  3. 表单控件
    用于内联的表单控件,如 <input><label><button> 标签。

示例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .inline-element {
  6. background-color: #ffecb3;
  7. padding: 5px;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <p>
  13. This is a paragraph with <span class="inline-element">inline</span> elements.
  14. Here is an <a href="#" class="inline-element">inline link</a>.
  15. And here is an <em class="inline-element">emphasized text</em>.
  16. </p>
  17. </body>
  18. </html>

富文本效果

inline 元素不能直接设置宽度和高度。虽然可以在 CSS 中指定 width 和 height,但这些属性不会对 inline 元素产生预期的效果。inline 元素的宽度和高度通常由其内容决定,并且不会像 block 元素那样响应 width 和 height 设置。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .inline-element {
  6. display: inline;
  7. width: 200px; /* 不会生效 */
  8. height: 100px; /* 不会生效 */
  9. background-color: #ffeb3b;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <span class="inline-element">This is an inline element.</span>
  15. <span class="inline-element">Another inline element.</span>
  16. </body>
  17. </html>

inline-block

inline-block结合了blockinline的特性。将元素的display属性设置为inline-block,该元素可以像行内元素一样与相邻元素在同一行显示,但同时可以像块级元素一样设置宽高。如下所示:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CSS Display</title>
  6. <style>
  7. .inline-block-element {
  8. display: inline-block;
  9. background-color: #d1ecf1;
  10. margin: 10px;
  11. padding: 10px;
  12. width: 100px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="inline-block-element">Inline-block Element 1</div>
  18. <div class="inline-block-element">Inline-block Element 2</div>
  19. <div class="inline-block-element">Inline-block Element 3</div>
  20. </body>
  21. </html>

在上面的示例中,.inline-block-element被设置为行内块级元素,它们在同一行内显示,但同时可以设置宽高和内外边距。

inline-block示例

display: inline-block 的特点

常见的应用场景

  1. 内联列表项
    用于在同一行内显示列表项,如导航菜单或图标列表。

  2. 按钮排列
    用于在同一行内排列多个按钮或表单控件。

  3. 图像和文本对齐
    用于图像和文本在同一行内对齐。

  4. 内联容器
    用于创建可以调整尺寸但仍然保持内联特性的容器。

内联列表项示例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .nav-item {
  6. display: inline-block;
  7. padding: 10px 20px;
  8. background-color: #81d4fa;
  9. margin: 5px;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <ul>
  15. <li class="nav-item">Home</li>
  16. <li class="nav-item">About</li>
  17. <li class="nav-item">Services</li>
  18. <li class="nav-item">Contact</li>
  19. </ul>
  20. </body>
  21. </html>

在这个示例中,ul 列表中的每个 li 元素被设置为 display: inline-block,它们在同一行内显示,形成一个水平导航菜单。

变更列表为水平

按钮排列示例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .button {
  6. display: inline-block;
  7. padding: 10px 20px;
  8. background-color: #ffab91;
  9. color: white;
  10. text-align: center;
  11. margin: 5px;
  12. border-radius: 5px;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div class="button">Button 1</div>
  18. <div class="button">Button 2</div>
  19. <div class="button">Button 3</div>
  20. </body>
  21. </html>

在这个示例中,三个 div 元素被设置为 display: inline-block,它们在同一行内排列,形成一个水平按钮组。

水平按钮组示例

总结

通过使用 inline-block,可以实现复杂的内联布局,同时保持灵活的尺寸调整。

none

none将元素隐藏,使其不在页面中显示。如下所示:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CSS Display</title>
  6. <style>
  7. .none-element {
  8. display: none;
  9. }
  10. </style>
  11. </head>
  12. <body>
  13. <div class="none-element">这个元素被隐藏了。</div>
  14. <div>这个元素没有被隐藏。</div>
  15. </body>
  16. </html>

在上面的示例中,.none-element被设置为none,因此它不会在页面中显示。

display: nonevisibility: hidden 都是用于隐藏元素的 CSS 属性,但它们有一些重要的区别:

display: none

  1. 元素不占据空间

    • 当你使用 display: none 时,元素会完全从文档流中移除,因此不会占据任何空间。
    • 其他元素将会像该元素不存在一样重新布局。
  2. 无法交互

    • 隐藏的元素无法被用户点击或交互。
  3. 子元素也被隐藏

    • 元素及其所有子元素都会被隐藏。
  4. 影响页面的性能

    • 如果有大量元素被设置为 display: none,页面的重新布局和渲染可能会变慢,因为浏览器必须重新计算布局。

visibility: hidden

  1. 元素占据空间

    • 当你使用 visibility: hidden 时,元素仍然保留在文档流中,占据原来的空间,但不会被显示。
    • 其他元素不会重新布局。
  2. 无法交互

    • 隐藏的元素无法被用户点击或交互。
  3. 子元素不会被隐藏

    • 元素本身会被隐藏,但它的子元素仍然可以设置为可见,例如 visibility: visible
  4. 较少的重排和重绘

    • display: none 相比,visibility: hidden 只需要重绘 (repaint) 而不需要重排 (reflow),性能开销较小。

举例

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <style>
  7. .display-none {
  8. display: none;
  9. }
  10. .visibility-hidden {
  11. visibility: hidden;
  12. }
  13. .container {
  14. border: 1px solid black;
  15. padding: 10px;
  16. margin: 20px;
  17. }
  18. </style>
  19. <title>CSS Visibility</title>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="display-none">This is hidden with display: none</div>
  24. <div class="visibility-hidden">This is hidden with visibility: hidden</div>
  25. <div>This is visible</div>
  26. </div>
  27. </body>
  28. </html>

总结

flex

Flexbox 简介

Flexbox(Flexible Box)布局模型是一种一维布局模型,主要用于在容器中分配空间并对齐内容。它可以使复杂的布局变得更加简单和直观,尤其是在处理元素的对齐、分配空间以及响应式设计时。

弹性容器和弹性项目

在 Flexbox 布局中,有两个重要的概念:弹性容器(flex container)和弹性项目(flex item)。设置 display: flex 的元素称为弹性容器,容器内的直接子元素称为弹性项目。

示例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .flex-container {
  6. display: flex;
  7. background-color: #e0f7fa;
  8. padding: 20px;
  9. }
  10. .flex-item {
  11. background-color: #00695c;
  12. color: white;
  13. padding: 20px;
  14. margin: 5px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div class="flex-container">
  20. <div class="flex-item">Item 1</div>
  21. <div class="flex-item">Item 2</div>
  22. <div class="flex-item">Item 3</div>
  23. </div>
  24. </body>
  25. </html>

Flex 示例

关键属性

flex-direction

flex-direction 属性定义了主轴的方向,可以是 row(默认)、row-reversecolumncolumn-reverse

示例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .flex-container-row {
  6. display: flex;
  7. flex-direction: row;
  8. background-color: #e0f7fa;
  9. padding: 20px;
  10. }
  11. .flex-container-column {
  12. display: flex;
  13. flex-direction: column;
  14. background-color: #ffecb3;
  15. padding: 20px;
  16. height: 200px;
  17. }
  18. .flex-item {
  19. background-color: #00695c;
  20. color: white;
  21. padding: 20px;
  22. margin: 5px;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div class="flex-container-row">
  28. <div class="flex-item">Row Item 1</div>
  29. <div class="flex-item">Row Item 2</div>
  30. <div class="flex-item">Row Item 3</div>
  31. </div>
  32. <div class="flex-container-column">
  33. <div class="flex-item">Column Item 1</div>
  34. <div class="flex-item">Column Item 2</div>
  35. <div class="flex-item">Column Item 3</div>
  36. </div>
  37. </body>
  38. </html>

方向的示例

justify-content

justify-content 属性用于定义项目在主轴上的对齐方式,包括 flex-startflex-endcenterspace-betweenspace-around 等。

1. flex-start

项目从主轴的起点开始排列,彼此紧挨在一起。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. justify-content: flex-start;
  8. border: 2px solid #4caf50;
  9. height: 100px;
  10. width: 400px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. <div class="item">Item 3</div>
  25. </div>
  26. </body>
  27. </html>

在这个示例中,所有项目都从容器的左边缘开始排列。

flex-start示例

2. flex-end

项目从主轴的终点开始排列,彼此紧挨在一起。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. justify-content: flex-end;
  8. border: 2px solid #4caf50;
  9. height: 100px;
  10. width: 400px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. <div class="item">Item 3</div>
  25. </div>
  26. </body>
  27. </html>

在这个示例中,所有项目都从容器的右边缘开始排列。

flex-end

3. center

项目在主轴上居中排列。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. justify-content: center;
  8. border: 2px solid #4caf50;
  9. height: 100px;
  10. width: 400px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. <div class="item">Item 3</div>
  25. </div>
  26. </body>
  27. </html>

在这个示例中,所有项目都在容器中水平居中排列。

center

4. space-between

项目在主轴上均匀分布,第一个项目在起点,最后一个项目在终点,项目之间的间隔相等。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. justify-content: space-between;
  8. border: 2px solid #4caf50;
  9. height: 100px;
  10. width: 400px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. <div class="item">Item 3</div>
  25. </div>
  26. </body>
  27. </html>

在这个示例中,第一个项目在左边缘,最后一个项目在右边缘,所有项目之间的间隔相等。

space-between

5. space-around

项目在主轴上均匀分布,项目之间的间隔相等,项目与容器边缘之间的间隔为项目之间间隔的一半。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. justify-content: space-around;
  8. border: 2px solid #4caf50;
  9. height: 100px;
  10. width: 400px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. <div class="item">Item 3</div>
  25. </div>
  26. </body>
  27. </html>

在这个示例中,所有项目之间的间隔相等,且项目与容器边缘之间的间隔为项目之间间隔的一半。

space-around

6. space-evenly

项目在主轴上均匀分布,包括项目与容器边缘之间的间隔在内,所有间隔都相等。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. justify-content: space-evenly;
  8. border: 2px solid #4caf50;
  9. height: 100px;
  10. width: 600px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. </div>
  25. </body>
  26. </html>

在这个示例中,所有项目之间以及项目与容器边缘之间的间隔都相等。

它和 space-around 是有些区别的,但需要空间足够大才能比较明显,比如

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. justify-content: space-around;
  8. border: 2px solid #4caf50;
  9. height: 100px;
  10. width: 600px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. </div>
  25. </body>
  26. </html>

这些示例展示了不同的 justify-content 设置如何影响项目在主轴上的对齐方式。通过这些例子,你可以更好地理解和应用 Flexbox 布局中的对齐技巧。

align-items

align-items 属性用于定义项目在交叉轴上的对齐方式,包括 flex-startflex-endcenterbaselinestretch 等。

align-items 属性用于定义项目在交叉轴上的对齐方式。主要的取值包括 flex-startflex-endcenterbaselinestretch。以下是每种取值的详细解释和示例:

1. flex-start

项目在交叉轴上向起点对齐。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. align-items: flex-start;
  8. flex-wrap: wrap;
  9. height: 200px;
  10. width: 300px;
  11. border: 2px solid #4caf50;
  12. }
  13. .item {
  14. background-color: #ffeb3b;
  15. border: 1px solid #000;
  16. padding: 20px;
  17. margin: 5px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="item">Item 1</div>
  24. <div class="item">Item 2</div>
  25. <div class="item">Item 3</div>
  26. <div class="item">Item 4</div>
  27. <div class="item">Item 5</div>
  28. <div class="item">Item 6</div>
  29. </div>
  30. </body>
  31. </html>

flex-start

2. flex-end

项目在交叉轴上向终点对齐。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. align-items: flex-end;
  8. flex-wrap: wrap;
  9. height: 200px;
  10. width: 300px;
  11. border: 2px solid #4caf50;
  12. }
  13. .item {
  14. background-color: #ffeb3b;
  15. border: 1px solid #000;
  16. padding: 20px;
  17. margin: 5px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="item">Item 1</div>
  24. <div class="item">Item 2</div>
  25. <div class="item">Item 3</div>
  26. <div class="item">Item 4</div>
  27. <div class="item">Item 5</div>
  28. <div class="item">Item 6</div>
  29. </div>
  30. </body>
  31. </html>

3. center

项目在交叉轴上居中对齐。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. align-items: center;
  8. flex-wrap: wrap;
  9. height: 200px;
  10. width: 300px;
  11. border: 2px solid #4caf50;
  12. }
  13. .item {
  14. background-color: #ffeb3b;
  15. border: 1px solid #000;
  16. padding: 20px;
  17. margin: 5px;
  18. }
  19. </style>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="item">Item 1</div>
  24. <div class="item">Item 2</div>
  25. <div class="item">Item 3</div>
  26. <div class="item">Item 4</div>
  27. <div class="item">Item 5</div>
  28. <div class="item">Item 6</div>
  29. </div>
  30. </body>
  31. </html>

4. baseline

项目在交叉轴上以其第一行文字的基线对齐。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. flex-direction: row;
  8. align-items: baseline;
  9. flex-wrap: wrap;
  10. height: 300px;
  11. width: 300px;
  12. border: 2px solid #4caf50;
  13. }
  14. .item {
  15. background-color: #ffeb3b;
  16. border: 1px solid #000;
  17. padding: 20px;
  18. margin: 5px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="item">Item 1...more.more.more.more.</div>
  25. <div class="item">Item 2</div>
  26. <div class="item">Item 3...</div>
  27. <div class="item">Item 4...</div>
  28. <div class="item">Item 5</div>
  29. </div>
  30. </body>
  31. </html>

5. stretch

项目在交叉轴上被拉伸以填满容器(默认值)。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. flex-direction: row;
  8. align-items: stretch;
  9. flex-wrap: wrap;
  10. height: 300px;
  11. width: 300px;
  12. border: 2px solid #4caf50;
  13. }
  14. .item {
  15. background-color: #ffeb3b;
  16. border: 1px solid #000;
  17. padding: 20px;
  18. margin: 5px;
  19. }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="container">
  24. <div class="item">Item 1...more.more.more.more.</div>
  25. <div class="item">Item 2</div>
  26. <div class="item">Item 3...</div>
  27. <div class="item">Item 4...</div>
  28. <div class="item">Item 5</div>
  29. </div>
  30. </body>
  31. </html>

通过这些示例,可以清晰地看到 align-items 属性的不同取值在交叉轴上如何影响项目的对齐方式。

主轴和交叉轴示例

在 Flexbox 布局中,主轴(main axis)是沿着 flex-direction 属性指定的方向排列的轴,而交叉轴(cross axis)是与主轴垂直的轴。了解主轴和交叉轴的概念可以帮助我们更好地掌握 Flexbox 的布局技巧。

示例 1:水平主轴和垂直交叉轴

默认情况下,flex-directionrow,即主轴是水平方向,交叉轴是垂直方向。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. border: 2px solid #4caf50;
  8. height: 200px;
  9. width: 400px;
  10. }
  11. .item {
  12. background-color: #ffeb3b;
  13. border: 1px solid #000;
  14. padding: 20px;
  15. margin: 5px;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="container">
  21. <div class="item">Item 1</div>
  22. <div class="item">Item 2</div>
  23. <div class="item">Item 3</div>
  24. </div>
  25. </body>
  26. </html>

在这个示例中,.containerflex-direction 是默认的 row,因此子元素在水平主轴上排列。

示例 2:垂直主轴和水平交叉轴

flex-direction 设置为 column,主轴变为垂直方向,交叉轴变为水平方向。

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. flex-direction: column;
  8. border: 2px solid #4caf50;
  9. height: 400px;
  10. width: 200px;
  11. }
  12. .item {
  13. background-color: #ffeb3b;
  14. border: 1px solid #000;
  15. padding: 20px;
  16. margin: 5px;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="container">
  22. <div class="item">Item 1</div>
  23. <div class="item">Item 2</div>
  24. <div class="item">Item 3</div>
  25. </div>
  26. </body>
  27. </html>

在这个示例中,.containerflex-direction 被设置为 column,因此子元素在垂直主轴上排列。

高级 Flexbox 技巧

换行

flex-wrap 属性允许项目换行,当项目超出容器的宽度时,可以换行显示。值包括 nowrap(默认)、wrapwrap-reverse

示例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .flex-container {
  6. display: flex;
  7. flex-wrap: wrap;
  8. background-color: #e0f7fa;
  9. padding: 20px;
  10. }
  11. .flex-item {
  12. background-color: #00695c;
  13. color: white;
  14. padding: 20px;
  15. margin: 5px;
  16. width: 30%;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="flex-container">
  22. <div class="flex-item">Item 1</div>
  23. <div class="flex-item">Item 2</div>
  24. <div class="flex-item">Item 3</div>
  25. <div class="flex-item">Item 4</div>
  26. <div class="flex-item">Item 5</div>
  27. <div class="flex-item">Item 6</div>
  28. </div>
  29. </body>
  30. </html>

排序

order 属性定义项目的排列顺序,默认为 0。值越小,项目排列越靠前。

示例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .flex-container {
  6. display: flex;
  7. background-color: #e0f7fa;
  8. padding: 20px;
  9. }
  10. .flex-item {
  11. background-color: #00695c;
  12. color: white;
  13. padding: 20px;
  14. margin: 5px;
  15. }
  16. .first {
  17. order: 1;
  18. }
  19. .second {
  20. order: 3;
  21. }
  22. .third {
  23. order: 2;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div class="flex-container">
  29. <div class="flex-item first">Item 1</div>
  30. <div class="flex-item second">Item 2</div>
  31. <div class="flex-item third">Item 3</div>
  32. </div>
  33. </body>
  34. </html>

对齐

align-self 属性允许单个项目在交叉轴上独立对齐,覆盖 align-items 属性的设置。

示例

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .container {
  6. display: flex;
  7. flex-direction: row;
  8. align-items: stretch;
  9. flex-wrap: wrap;
  10. height: 300px;
  11. width: 300px;
  12. border: 2px solid #4caf50;
  13. }
  14. .item {
  15. background-color: #ffeb3b;
  16. border: 1px solid #000;
  17. padding: 20px;
  18. margin: 5px;
  19. }
  20. .aligned {
  21. align-self: flex-end;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div class="container">
  27. <div class="item">Item 1...more.more.more.more.</div>
  28. <div class="item">Item 2</div>
  29. <div class="item aligned">Item 3...</div>
  30. <div class="item">Item 4...</div>
  31. <div class="item">Item 5</div>
  32. </div>
  33. </body>
  34. </html>

通过这些示例,您可以看到 Flexbox 如何提供强大的布局功能,从简单的水平和垂直对齐,到更高级的换行、排序和独立对齐。灵活运用这些属性,能够构建复杂且响应式的网页布局。

grid

grid也是CSS3引入的一种布局模式,可以将元素按行和列排列,形成网格布局。将父元素的display属性设置为grid,其子元素将按照网格布局的规则排列。如下所示:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CSS Display</title>
  6. <style>
  7. .grid-container {
  8. display: grid;
  9. grid-template-columns: repeat(3, 1fr);
  10. gap: 10px;
  11. background-color: #e9ecef;
  12. }
  13. .grid-item {
  14. background-color: #6c757d;
  15. padding: 20px;
  16. color: white;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <div class="grid-container">
  22. <div class="grid-item">Grid Item 1</div>
  23. <div class="grid-item">Grid Item 2</div>
  24. <div class="grid-item">Grid Item 3</div>
  25. <div class="grid-item">Grid Item 4</div>
  26. <div class="grid-item">Grid Item 5</div>
  27. <div class="grid-item">Grid Item 6</div>
  28. </div>
  29. </body>
  30. </html>

在上面的示例中,.grid-container被设置为网格容器,.grid-item被设置为网格项,它们根据网格布局的规则排列。


以上就是CSS display属性的几种常见值及其应用实例。希望通过这篇文章,大家能够对display属性有更深入的了解,并在实际项目中灵活运用。欢迎大家指出文章中的不足之处,共同学习进步。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注