垂直定位的项目给予相同的宽度

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

Give same width to items vertically positioned

问题

我有两个垂直排列的项目,并且我希望较窄的一个与较宽的一个一样宽。

我的代码如下:

<div class="flex flex-col items-end">
  <div>This should take all the space</div>
  <div class="flex flex-row gap-x-4">
    <div> this is the first element</div>
    <div> this is the second element</div>
  </div>
</div>

并且生成

垂直定位的项目给予相同的宽度

然而,我希望结果是

垂直定位的项目给予相同的宽度

items-end 是必需的,因为项目显示在页面的右侧。

我已经尝试过调整位置,但无法实现我寻找的最终结果。

有人可以帮我吗?

英文:

I have two items positioned vertically, and I'd like the narrower one is as wide as the wider one.

My code looks like

&lt;div class=&quot;flex flex-col items-end&quot;&gt;
  &lt;div&gt;This should take all the space&lt;/div&gt;
  &lt;div class=&quot;flex flex-row gap-x-4&quot;&gt;
    &lt;div&gt; this is the first element&lt;/div&gt;
    &lt;div&gt; this is the second element&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

and produce

垂直定位的项目给予相同的宽度

However, I would like the result to be

垂直定位的项目给予相同的宽度

items-end is needed because the items are displayed on the right side of the page.

I have tried to mess with positioning, but I cannot achieve the final result I'm looking for.

Can anyone give me a hand on this?

答案1

得分: 1

你可以对容器进行收缩包装,然后将其右对齐:

<!-- 开始代码段: js 隐藏: false 控制台: false Babel: false -->

<!-- 语言: lang-html -->
    <script src="https://cdn.tailwindcss.com"></script>
    <div class="flex flex-col w-max ml-auto">
      <div>This should take all the space</div>
      <div class="flex flex-row gap-x-4">
        <div> this is the first element</div>
        <div> this is the second element</div>
      </div>
    </div>

<!-- 结束代码段 -->

你还可以使用一个带有一个max-content大小的网格列轨道的网格布局,然后将网格列轨道对齐到右侧:

<!-- 开始代码段: js 隐藏: false 控制台: false Babel: false -->

<!-- 语言: lang-html -->
    <script src="https://cdn.tailwindcss.com"></script>
    <div class="grid grid-cols-[max-content] justify-end">
      <div>This should take all the space</div>
      <div class="flex flex-row gap-x-4">
        <div> this is the first element</div>
        <div> this is the second element</div>
      </div>
    </div>

<!-- 结束代码段 -->
英文:

You could shrink-wrap the container and then right align it:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;flex flex-col w-max ml-auto&quot;&gt;
  &lt;div&gt;This should take all the space&lt;/div&gt;
  &lt;div class=&quot;flex flex-row gap-x-4&quot;&gt;
    &lt;div&gt; this is the first element&lt;/div&gt;
    &lt;div&gt; this is the second element&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

You could also use a grid layout with one grid column track sized to max-content and then align the grid column track to the right:

<!-- begin snippet: js hide: false console: false babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://cdn.tailwindcss.com&quot;&gt;&lt;/script&gt;

&lt;div class=&quot;grid grid-cols-[max-content] justify-end&quot;&gt;
  &lt;div&gt;This should take all the space&lt;/div&gt;
  &lt;div class=&quot;flex flex-row gap-x-4&quot;&gt;
    &lt;div&gt; this is the first element&lt;/div&gt;
    &lt;div&gt; this is the second element&lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月1日 18:01:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380747.html
匿名

发表评论

匿名网友

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

确定