使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

huangapple go评论67阅读模式
英文:

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:

&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg&quot;&gt;
  &lt;div class=&quot;py-4&quot;&gt;01&lt;/div&gt;
  &lt;div class=&quot;py-12&quot;&gt;02&lt;/div&gt;
&lt;/div&gt;

How I can make this working?

答案1

得分: 1

Use mx-auto 在子元素中以自动对齐,参见示例:

&lt;!-- 原始 --&gt;
&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg mx-auto&quot;&gt;
  &lt;div class=&quot;py-4&quot;&gt;01&lt;/div&gt;
  &lt;div class=&quot;py-12&quot;&gt;02&lt;/div&gt;
&lt;/div&gt;

&lt;!-- 仅一个元素 --&gt;
&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg&quot;&gt;
  &lt;div class=&quot;py-4 mx-auto&quot;&gt;01&lt;/div&gt;
&lt;/div&gt;

&lt;!-- 多个元素 --&gt;
&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg&quot;&gt;
  &lt;div class=&quot;py-4 mx-auto&quot;&gt;01&lt;/div&gt;
  &lt;div class=&quot;py-12 mx-auto&quot;&gt;02&lt;/div&gt;
  &lt;div class=&quot;py-12 mx-auto&quot;&gt;02&lt;/div&gt;
&lt;/div&gt;

输出:

使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

英文:

Use mx-auto in children elements to automatically align, see Playground:

&lt;!-- original --&gt;
&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg mx-auto&quot;&gt;
  &lt;div class=&quot;py-4&quot;&gt;01&lt;/div&gt;
  &lt;div class=&quot;py-12&quot;&gt;02&lt;/div&gt;
&lt;/div&gt;

&lt;!-- Just one element --&gt;
&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg&quot;&gt;
  &lt;div class=&quot;py-4 mx-auto&quot;&gt;01&lt;/div&gt;
&lt;/div&gt;

&lt;!-- multiple elements --&gt;
&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg&quot;&gt;
  &lt;div class=&quot;py-4 mx-auto&quot;&gt;01&lt;/div&gt;
  &lt;div class=&quot;py-12 mx-auto&quot;&gt;02&lt;/div&gt;
  &lt;div class=&quot;py-12 mx-auto&quot;&gt;02&lt;/div&gt;
&lt;/div&gt;

Output:

使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

答案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;
}

输出:

  • 1个项目
    使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

    <div class="container flex w-screen items-center justify-between bg-red-100">
     <div class="item">01</div>
     <div class="item">02</div>
    </div> 
    
    
  • 2个项目
    使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

    <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:

&lt;div class=&quot;flex justify-between items-center w-full max-w-screen-lg&quot;&gt;
{items.length === 1 ? (
&lt;div class=&quot;py-4 text-center&quot;&gt;01&lt;/div&gt;
) : (
&lt;&gt;
&lt;div class=&quot;py-4&quot;&gt;01&lt;/div&gt;
&lt;div class=&quot;py-12&quot;&gt;02&lt;/div&gt;
&lt;/&gt;
)}
&lt;/div&gt;

In case you need plain css, You can use the following css code:

.container &gt; .item:first-child:last-child {
margin-left: auto;
margin-right: auto;
}

Output:

  • 1 item
    使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

    &lt;div class=&quot;container flex w-screen items-center justify-between bg-red-100&quot;&gt;
    &lt;div class=&quot;item&quot;&gt;01&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;02&lt;/div&gt;
    &lt;/div&gt; 
    
  • 2 items
    使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

    &lt;div class=&quot;container flex w-screen items-center justify-between bg-red-100&quot;&gt;
    &lt;div class=&quot;item&quot;&gt;01&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;02&lt;/div&gt;
    &lt;/div&gt;
    
  • 3 items
    使用Tailwind CSS将单个项目居中于容器,同时对多个项目使用`justify-between`。

     &lt;div class=&quot;container flex w-screen items-center justify-between bg-red-100&quot;&gt;
    &lt;div class=&quot;item&quot;&gt;01&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;02&lt;/div&gt;
    &lt;div class=&quot;item&quot;&gt;03&lt;/div&gt;
    &lt;/div&gt;
    

Feel free to play in tailwind-play

huangapple
  • 本文由 发表于 2023年5月7日 14:41:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76192533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定