实现CSS两端对齐的方式有以下几种:
justify-content: space-between;
属性将子元素在容器中两端对齐。.container {
display: flex;
justify-content: space-between;
}
text-align: justify;
属性将文本两端对齐。.container {
text-align: justify;
}
.container::after {
content: '';
display: inline-block;
width: 100%;
}
grid-template-columns
属性将子元素在容器中两端对齐。.container {
display: grid;
grid-template-columns: auto auto;
justify-content: space-between;
}
float: left;
和float: right;
将元素分别向左和向右浮动,从而实现两端对齐效果。.left {
float: left;
}
.right {
float: right;
}
display: inline-block;
将元素以行内块级元素的方式显示,然后使用text-align: justify;
将其两端对齐。.container {
text-align: justify;
}
.container > * {
display: inline-block;
}
.container::after {
content: '';
display: inline-block;
width: 100%;
}