CSS align-items 属性指定弹性项目(flex-item)的默认对齐方式。
它类似于 justify-content 属性,但是垂直版本。
此属性是 CSS3 属性之一。
align-items 属性接受以下值:
- stretch
- flex-start
- center
- flex-end
- baseline
初始值 | stretch |
---|---|
应用于 | 所有元素。 |
Inherited | 无效 |
Animatable | 无效 |
版本 | CSS3 |
DOM 语法 | object.style.alignItems=“center”; |
值说明
值 | 说明 |
---|---|
stretch | 使项目拉伸以适合容器。这是此属性的默认值。 |
center | 物品放在容器的中心。 |
flex-start | 物品放在容器的开头。 |
flex-end | 物品放在容器的末端。 |
baseline | 项目位于容器的基线处。 |
initial | 它使属性使用其默认值。 |
inherit | 它从其父元素继承属性。 |
语法
align-items: stretch | center | flex-start | flex-end | baseline | initial | inherit;
align-items 属性示例:
<!DOCTYPE html> <html> <head> <title>Title of the document</title> <style> #example { width: 220px; height: 300px; padding: 0; border: 1px solid #000; display: -webkit-flex; /* Safari */ -webkit-align-items: stretch; /* Safari 7.0+ */ display: flex; align-items: stretch; } #example li { -webkit-flex: 1; /* Safari 6.1+ */ flex: 1; list-style: none; } </style> </head> <body> <h2>Align-items: stretch; example</h2> <ul id="example"> <li style="background-color:#8ebf42;">Green</li> <li style="background-color:#1c87c9;">Blue</li> <li style="background-color:#ccc;">Gray</li> </ul> </body> </html>
结果
在以下示例中,将项目(item)容器的中心。
带有“center”值的 align-items 属性示例:
<!DOCTYPE html> <html> <head> <title>The title of the document</title> <style> #example { width: 220px; height: 300px; padding: 0; border: 1px solid #000; display: -webkit-flex; /* Safari */ -webkit-align-items: center; /* Safari 7.0+ */ display: flex; align-items: center; } #example li { -webkit-flex: 1; /* Safari 6.1+ */ flex: 1; list-style: none; } </style> </head> <body> <h2>Align-items: center; example</h2> <ul id="example"> <li style="background-color:#8ebf42;">Green</li> <li style="background-color:#1c87c9;">Blue</li> <li style="background-color:#ccc;">Gray</li> </ul> </body> </html>
在下一个示例中,项目(item)放置在开头。
带有“flex-start”值的 align-items 属性示例:
<!DOCTYPE html> <html> <head> <title>The title of the document</title> <style> #example { width: 220px; height: 300px; padding: 0; border: 1px solid #000; display: -webkit-flex; /* Safari */ -webkit-align-items: flex-start; /* Safari 7.0+ */ display: flex; align-items: flex-start; } #example li { -webkit-flex: 1; /* Safari 6.1+ */ flex: 1; list-style: none; } </style> </head> <body> <h2>Align-items: flex-start; example</h2> <ul id="example"> <li style="background-color:#8ebf42;">Green</li> <li style="background-color:#1c87c9;">Blue</li> <li style="background-color:#ccc;">Gray</li> </ul> </body> </html>
其中我们应用“flex-end”值,将项目放置在容器的末尾。
带有“flex-end”值的 align-items 属性示例:
<!DOCTYPE html> <html> <head> <title>The title of the document</title> <style> #example { width: 220px; height: 300px; padding: 0; border: 1px solid #000; display: -webkit-flex; /* Safari */ -webkit-align-items: flex-end; /* Safari 7.0+ */ display: flex; align-items: flex-end; } #example li { -webkit-flex: 1; /* Safari 6.1+ */ flex: 1; list-style: none; } </style> </head> <body> <h2>Align-items: flex-end; example</h2> <ul id="example"> <li style="background-color:#8ebf42;">Green</li> <li style="background-color:#1c87c9;">Blue</li> <li style="background-color:#ccc;">Gray</li> </ul> </body> </html>
让我们看看最后一个带有“baseline”值的示例,该值将项目放置在容器的基线处。
带有“基线”值的 align-items 属性示例:
<!DOCTYPE html> <html> <head> <title>The title of the document</title> <style> #example { width: 220px; height: 300px; padding: 0; border: 1px solid #000; display: -webkit-flex; /* Safari */ -webkit-align-items: baseline; /* Safari 7.0+ */ display: flex; align-items: baseline; } #example li { -webkit-flex: 1; /* Safari 6.1+ */ flex: 1; list-style: none; } </style> </head> <body> <h2>Align-items: baseline; example</h2> <ul id="example"> <li style="background-color:#8ebf42;">Green</li> <li style="background-color:#1c87c9;">Blue</li> <li style="background-color:#ccc;">Gray</li> </ul> </body> </html>
日期:2020-06-02 22:14:15 来源:oir作者:oir