英文:
Centering a single item in container with justify-between for multiple items in Tailwind CSS
问题
I'm trying to align an item to the center of the container if there is only one item, otherwise justify the items between each other. However, I'm having trouble with the alignment - if there is only one item, it appears on the extreme left instead of being centered.
以下是我的代码:
<div class="flex justify-between items-center w-full max-w-screen-lg">
<div class="py-4">01</div>
<div class="py-12">02</div>
</div>
我该如何使其正常工作?
英文:
I'm trying to align an item to the center of the container if there is only one item, otherwise justify the items between each other. However, I'm having trouble with the alignment - if there is only one item, it appears on the extreme left instead of being centered.
Following is my code:
<div class="flex justify-between items-center w-full max-w-screen-lg">
<div class="py-4">01</div>
<div class="py-12">02</div>
</div>
How I can make this working?
答案1
得分: 1
Use mx-auto
在子元素中以自动对齐,参见示例:
<!-- 原始 -->
<div class="flex justify-between items-center w-full max-w-screen-lg mx-auto">
<div class="py-4">01</div>
<div class="py-12">02</div>
</div>
<!-- 仅一个元素 -->
<div class="flex justify-between items-center w-full max-w-screen-lg">
<div class="py-4 mx-auto">01</div>
</div>
<!-- 多个元素 -->
<div class="flex justify-between items-center w-full max-w-screen-lg">
<div class="py-4 mx-auto">01</div>
<div class="py-12 mx-auto">02</div>
<div class="py-12 mx-auto">02</div>
</div>
输出:
英文:
Use mx-auto
in children elements to automatically align, see Playground:
<!-- original -->
<div class="flex justify-between items-center w-full max-w-screen-lg mx-auto">
<div class="py-4">01</div>
<div class="py-12">02</div>
</div>
<!-- Just one element -->
<div class="flex justify-between items-center w-full max-w-screen-lg">
<div class="py-4 mx-auto">01</div>
</div>
<!-- multiple elements -->
<div class="flex justify-between items-center w-full max-w-screen-lg">
<div class="py-4 mx-auto">01</div>
<div class="py-12 mx-auto">02</div>
<div class="py-12 mx-auto">02</div>
</div>
Output:
答案2
得分: 1
你可以考虑使用mx-auto
,它会自动设置你的x轴边距并将内容居中。如@Jishan的答案中所提到的,但在第三种情况下,它不是justify-between
之间的项目。
所以你可以考虑使用条件运算符,像这样:
<div class="flex justify-between items-center w-full max-w-screen-lg">
{items.length === 1 ? (
<div class="py-4 text-center">01</div>
) : (
<>
<div class="py-4">01</div>
<div class="py-12">02</div>
</>
)}
</div>
如果你需要纯粹的CSS,你可以使用以下CSS代码:
.container > .item:first-child:last-child {
margin-left: auto;
margin-right: auto;
}
输出:
-
<div class="container flex w-screen items-center justify-between bg-red-100"> <div class="item">01</div> <div class="item">02</div> </div>
-
<div class="container flex w-screen items-center justify-between bg-red-100"> <div class="item">01</div> <div class="item">02</div> </div>
- 3个项目
[![enter image description here][3]][3]
```html
<div class="container flex w-screen items-center justify-between bg-red-100">
<div class="item">01</div>
<div class="item">02</div>
<div class="item">03</div>
</div>
随意在 tailwind-play 中尝试。
英文:
You can consider using mx-auto
which will magically set your x-axis
margin and sets the content to center. As mentioned in @Jishan's answer, but in the third case it is not justify-between
between the items.
So you can consider using conditional operator like so:
<div class="flex justify-between items-center w-full max-w-screen-lg">
{items.length === 1 ? (
<div class="py-4 text-center">01</div>
) : (
<>
<div class="py-4">01</div>
<div class="py-12">02</div>
</>
)}
</div>
In case you need plain css
, You can use the following css
code:
.container > .item:first-child:last-child {
margin-left: auto;
margin-right: auto;
}
Output:
-
<div class="container flex w-screen items-center justify-between bg-red-100"> <div class="item">01</div> <div class="item">02</div> </div>
-
<div class="container flex w-screen items-center justify-between bg-red-100"> <div class="item">01</div> <div class="item">02</div> </div>
-
<div class="container flex w-screen items-center justify-between bg-red-100"> <div class="item">01</div> <div class="item">02</div> <div class="item">03</div> </div>
Feel free to play in tailwind-play
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论