将两个文本区域列并排对齐。

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

Align two textarea column side by side

问题

我需要将两个<textarea>元素并排对齐,以便每个文本区域应占窗口的整个高度和一半的宽度。

以下部分有效,但...

body {
  display: flex;
}

textarea {
  width: 50%;
}

但我不明白为什么使用display: flex时50%是合适的,而在没有它的情况下则太大了。

这引发了另一个问题:这真的是一种“适当”的方式来完成这个任务吗?看起来像是一种“快速而肮脏”的解决方案。有没有更“适当”的方法来对齐它们?

英文:

I need to align two &lt;textarea&gt; elements side by side, so that each text area should take the whole height of the window and the half of its width.

The following works fine, but ...

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

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

body {
  display: flex;
}

textarea {
  width: 50%;
}

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

&lt;textarea&gt;&lt;/textarea&gt;
&lt;textarea readonly&gt;&lt;/textarea&gt;

<!-- end snippet -->

but I don't understand why 50% is fine if I use display: flex, but is too much without it.

And this rises another question: is it really a "proper" way to achieve this task? Looks as a "quick-and-dirty" solution. Is there a more "proper" way to align them?

答案1

得分: 1

默认的 flex 值是 0 1 auto,等同于:

flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;

所以当你将你的文本区域设为 flex 容器,但没有应用任何具体的 flex 规则时,它们会自动应用 flex-shrink: 1;。然后,当你给它们应用 width: 50%; 时,它们都会尝试扩展到其父元素宽度的 50%,但也会明白它们需要根据需要缩小。

由于没有一个文本区域需要比另一个更宽,它们最终会具有相等的宽度。但是,如果你给它们一个更大的宽度,比如 100%;,你会得到相同的行为,就像在这个示例中所看到的:

body {
  display: flex;
}

textarea {
  width: 100%;
}

我个人喜欢的一种方法,使 flex 容器中的多个项目都占据相等的宽度,不考虑它们的内容,是使用以下组合规则:

.flex {
  display: flex;
}

.flex-child {
  outline: 2px solid red;
  
  width: 0;
  flex: 1 1 auto;
}

将元素的宽度设置为 0 基本上告诉 flexbox 在考虑相对于它的兄弟元素时不要考虑其内容,这有助于确保列具有相等的宽度。然后,flex: 1 1 auto; 告诉每个项目根据需要扩展和收缩,它们将尝试填充沿 flex 轴的相同空间量。

你可能会发现这篇 CSS Tricks 上的文章有帮助,以理解在这些情况下 flexbox 的工作原理:Equal Columns With Flexbox: It’s More Complicated Than You Might Think


如果你想了解可以实现类似结果的其他方法,你可能会对阅读更多关于如何使用 CSS Grid 来构建布局的内容感兴趣,其中可以使用 fr("分数")单位来定义具有相等宽度和间隙的列。CSS Tricks 提供了一个有关使用网格的有用速查表:Complete Guide to Grid

以下是使用这种方法的示例:

.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 1rem;
}

.grid textarea {
  /* 禁止水平调整大小 */
  resize: vertical;
  /* 这对于垂直调整大小的影响不理想,但它确保两个文本区域保持相同的高度 */
  min-height: 100%;
}
英文:

The default flex value is 0 1 auto, which is equivalent to:

flex-grow: 0;
flex-shrink: 1;
flex-basis: auto;

So when you give your textareas a flex parent, but don't apply any specific flex rules on them, they will automatically have flex-shrink: 1; applied. When you then give them width: 50%;, they will both try to expand to 50% of their parent's width but also will understand that they should shrink as necessary in order to fit.

Since there's no reason for one textarea to be wider than the other, they end up being equal widths. But you could actually end up with the same behaviour if you gave them an even larger width like 100%;, as you can see in this example:

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

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

body {
  display: flex;
}

textarea {
  width: 100%;
}

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

&lt;textarea&gt;&lt;/textarea&gt;
&lt;textarea readonly&gt;&lt;/textarea&gt;

<!-- end snippet -->

My personal favourite way to have a number of items in a flex container all take up equal widths, regardless of their content, is to use this combination of rules:

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

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

.flex {
  display: flex;
}

.flex-child {
  outline: 2px solid red;
  
  width: 0;
  flex: 1 1 auto;
}

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

&lt;div class=&quot;flex&quot;&gt;
  &lt;div class=&quot;flex-child&quot;&gt;One&lt;/div&gt;
  &lt;div class=&quot;flex-child&quot;&gt;Two&lt;/div&gt;
  &lt;div class=&quot;flex-child&quot;&gt;Three&lt;/div&gt;
  &lt;div class=&quot;flex-child&quot;&gt;Four&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

Setting an element's width to 0 basically tells flexbox not to consider its content when considering how wide it should be relative to its siblings, which helps ensure equal column widths. Then, flex: 1 1 auto; tells each item to expand and collapse as necessary, and they will try to fill the same amount of space along the flex axis.

You might find this article on CSS Tricks helpful when it comes to understanding how flexbox is working in cases like these: Equal Columns With Flexbox: It’s More Complicated Than You Might Think


For alternative approaches that can achieve similar results, you might be interested in reading more about how the fr ("fraction") unit can be used in constructing layouts using CSS grid, which can be used to define columns with equal widths and a gap in between them. CSS Tricks has a useful cheat sheet for working with grid.

Here's an example using this approach:

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

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

.grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 1rem;
}

.grid textarea {
  /* Disallow horizontal resizing */
  resize: vertical;
  /* This isn&#39;t ideal for how it affects vertical resizing,
  but it does ensure both textareas stay the same height */
  min-height: 100%;
}

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

&lt;div class=&quot;grid&quot;&gt;
  &lt;textarea&gt;&lt;/textarea&gt;
  &lt;textarea readonly&gt;&lt;/textarea&gt;
&lt;/div&gt;

<!-- end snippet -->

答案2

得分: -1

更合适的方式是:尝试使用 display: inline-block;,同时确保它们之间没有边距或空白。为了确认,请尝试在每个元素上添加 width: 45%,以确保它们可以紧邻在一起,没有发生意外情况。

英文:

A more appropriate way is: try display: inline-block; while making sure there's no margin or space between them. To confirm, try width: 45% on each to confirm they can get next to one another without anything unexpected happening.

huangapple
  • 本文由 发表于 2023年7月7日 08:22:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76633230.html
匿名

发表评论

匿名网友

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

确定