實現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%;
}