在网格中整齐对齐项目。

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

Align items neatly in grid

问题

Sure, here's the translated content you requested:

我有一些带有`grid-template-columns: auto auto auto;`的`div`,因为我希望列宽像弹性效果一样,取决于元素的宽度,还有`justify-content: start;`来将列对齐到左边,这也是为什么我使用`auto`而不是`1fr`的原因,因为我不希望列被拉伸到可能的最大宽度。

问题是,我不知道如何像表格一样整齐地对齐列,正如您可以从下面的示例中看到的那样,第二个`div`中的`p .target`要比第一个长,我试图实现的是让所有其他列的宽度都跟随最长的列。

附注:我不想使用表格。我也可以使用JavaScript找到最长的列并将所有其他列的宽度设置为相同。我只是想看看是否有仅使用CSS的简单解决方案。

例如,在表格中看起来像这样:
请注意,所有第二列都从第二行开始,即使第一列的“hellooo”在第二行更长。

如果需要更多帮助,请告诉我。

英文:

I have some div .test with grid-template-columns: auto auto auto; because I want the column width to be like flex effect which depends on the element width, and also justify-content: start; to align the column to the left and this is also why I use auto and not 1fr because I don't want the columns being stretched to the longer width possible.

The problem is that I don't know how to align the column neatly like a table does, s you can see from the example below, the p .target from the second div .test is longer than the first one, what I try to achieve is to make all other column width follow the longest one.

P.S.: I don't want to use table. I also can use JavaScript to find the longest one and set all others' width to this. I just want to see if there is an easy solution with CSS only.

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

<!-- language: lang-css -->

p {
  margin: 0;
}

.test {
	column-gap: 30px;
	display: grid;
    grid-template-columns: auto auto auto;
    justify-content: start;
}

p:nth-child(2) {
	position: relative;
    width: 120px;
}

.flex {
    white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
    position: absolute;
    left: 0;
    right: 0;
}

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

&lt;div class=&quot;test&quot;&gt;
  &lt;p class=&quot;target&quot;&gt;Hello&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;test&quot;&gt;
	&lt;p class=&quot;target&quot;&gt;Hellooo&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

For example, it looks like this in a table:

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

<!-- language: lang-css -->

td:nth-child(2) {
	position: relative;
  width: 120px;
}

.flex {
  white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
}

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

&lt;table&gt;
&lt;tbody&gt;
  &lt;tr&gt;
    &lt;td&gt;Hello&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;Hello&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td&gt;Hellooo&lt;/td&gt;
    &lt;td&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/td&gt;
    &lt;td&gt;Hello&lt;/td&gt;
  &lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;

<!-- end snippet -->

Note that the all the second columns start together even the first column "hellooo" is longer from the second row.

答案1

得分: 0

重构你的HTML以将所有项目放入单个网格中,并使用inline-grid

<div class="test">
  <p class="target">Hello</p>
  <p><span class="flex">Helloooooooooooooo</span></p>
  <p>Hello</p>
  <p class="target">Hellooo</p>
  <p><span class="flex">Helloooooooooooooo</span></p>
  <p>Hello</p>
</div>
.test {
  display: inline-grid;
}

请注意,您需要使用更复杂的nth-child来选择每行的第二个元素:

p:nth-child(3n + 2) {}

试试看:

<!-- 开始片段: js 隐藏: true -->

<!-- 语言: lang-css -->
.test {
  display: inline-grid;
}

p:nth-child(3n + 2) {
  position: relative;
  width: 120px;
}

/* 仅演示用 */
p {
  margin: 0;
  background: #ddd;
}

.test {
  column-gap: 30px;
  grid-template-columns: auto auto auto;
  justify-content: start;
}

.flex {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: absolute;
  left: 0;
  right: 0;
}

<!-- 语言: lang-html -->
<div class="test">
  <p class="target">Hello</p>
  <p><span class="flex">Helloooooooooooooo</span></p>
  <p>Hello</p>
  <p class="target">Hellooo</p>
  <p><span class="flex">Helloooooooooooooo</span></p>
  <p>Hello</p>
</div>

<!-- 结束片段 -->
英文:

Refactor your HTML so as to add all items inside a single grid and use inline-grid:

&lt;div class=&quot;test&quot;&gt;
  &lt;p class=&quot;target&quot;&gt;Hello&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
  &lt;p class=&quot;target&quot;&gt;Hellooo&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;
.test {
  display: inline-grid;
}

Note that you will have to target the second element of each row with a more complex nth-child:

p:nth-child(3n + 2) {}

Try it:

<!-- begin snippet: js hide: true -->

<!-- language: lang-css -->

.test {
  display: inline-grid;
}

p:nth-child(3n + 2) {
  position: relative;
  width: 120px;
}


/* Demo only */

p {
  margin: 0;
  background: #ddd;
}

.test {
  column-gap: 30px;
  grid-template-columns: auto auto auto;
  justify-content: start;
}

.flex {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  position: absolute;
  left: 0;
  right: 0;
}

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

&lt;div class=&quot;test&quot;&gt;
  &lt;p class=&quot;target&quot;&gt;Hello&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
  &lt;p class=&quot;target&quot;&gt;Hellooo&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: 0

以下是翻译好的内容:

如果您希望网格列的行为类似于表格,您需要将项目放置在单个网格容器中,而不是两个分开的容器:

<div class="test">
    <p class="target">Hello</p>
    <p><span class="flex">Helloooooooooooooo</span></p>
    <p>Hello</p>
    <p class="target">Hellooo</p>
    <p><span class="flex">Helloooooooooooooo</span></p>
    <p>Hello</p>
</div>

要设置每行的第二个元素的宽度,您可以在网格布局中简单地指定宽度:

grid-template-columns: auto 120px auto;

但是如果现在去掉 p:nth-child(2) 选择器,.flex 元素会绝对定位到网格上,而不是 p 元素,这会破坏布局。

但是您实际上可以完全去掉绝对定位,而不会破坏布局,并将溢出属性应用于所有网格项,如下所示:

.test > * {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

这是生成的更清晰的代码:

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

<!-- language: lang-css -->
p {
  margin: 0;
}

.test {
  column-gap: 30px;
  display: grid;
  grid-template-columns: auto 120px auto;
  justify-content: start;
}

.test > * {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

<!-- language: lang-html -->
<div class="test">
  <p class="target">Hello</p>
  <p><span class="flex">Helloooooooooooooo</span></p>
  <p>Hello</p>
  <p class="target">Hellooo</p>
  <p><span class="flex">Helloooooooooooooo</span></p>
  <p>Hello</p>
</div>

<!-- end snippet -->
英文:

If you want the grid columns to behave like a table you need to place your items in a single grid container, not 2 separate ones:

&lt;div class=&quot;test&quot;&gt;
  &lt;p class=&quot;target&quot;&gt;Hello&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
  &lt;p class=&quot;target&quot;&gt;Hellooo&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

To set the width of the second element of each row, you can simply specify the width in your grid layout:

grid-template-columns: auto 120px auto;

But if we get rid of the p:nth-child(2) selector now, we'd have the .flex element positioned absolutely against the grid, and not the p element, which messes up the layout.

But you can actually get rid of the absolute positioning altogether without messing up your layout and apply your overflow properties to all the grid items like this:

.test &gt; * {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

And here is the resulting, cleaner code:

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

<!-- language: lang-css -->

p {
  margin: 0;
}

.test {
  column-gap: 30px;
  display: grid;
  grid-template-columns: auto 120px auto;
  justify-content: start;
}

.test &gt; * {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

&lt;div class=&quot;test&quot;&gt;
  &lt;p class=&quot;target&quot;&gt;Hello&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
  &lt;p class=&quot;target&quot;&gt;Hellooo&lt;/p&gt;
  &lt;p&gt;&lt;span class=&quot;flex&quot;&gt;Helloooooooooooooo&lt;/span&gt;&lt;/p&gt;
  &lt;p&gt;Hello&lt;/p&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月6日 09:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410875.html
匿名

发表评论

匿名网友

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

确定