如何让具有五个元素的一行,使第二个和第四个元素均匀填充剩余的宽度?

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

For a row with five elements, how to let second and forth element fill remain width evenly?

问题

举例来说,我有5个元素:

  <span style="width:100%;">
    <div>1111111111</div>
    <div style="width:100%;background-color:red;">2222222222</div>
    <div>3333333333</div>
    <div style="width:100%;background-color:red;">4444444444</div>
    <div>5555555555</div>
  </span>

我想将它们封装到一行中,然后第二个和第四个元素扩展以填充剩余的宽度,使第二个和第四个元素具有相同的宽度。我该如何做?

我尝试过:

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
    <table style="width:100%;">
      <tr>
        <td style="white-space:nowrap;">1111111111</td>
        <td style="width:100%;background-color:red;">2222222222</td>
        <td style="white-space:nowrap;">3333333333</td>
        <td style="width:100%;background-color:red;">4444444444</td>
        <td style="white-space:nowrap;">5555555555</td>
      </tr>
    </table>
<!-- end snippet -->

但第二个元素会扩展以"挤占"第四个元素,这并不起作用。(注意:不一定要使用表格,只是我认为可以实现这个结果,但仍然不起作用)

英文:

For example, I have 5 elements:

  &lt;span style=&quot;width:100%;&quot;&gt;
    &lt;div&gt;1111111111&lt;/div&gt;
    &lt;div style=&quot;width:100%;background-color:red;&quot;&gt;2222222222&lt;/div&gt;
    &lt;div&gt;3333333333&lt;/div&gt;
    &lt;div style=&quot;width:100%;background-color:red;&quot;&gt;4444444444&lt;/div&gt;
    &lt;div&gt;5555555555&lt;/div&gt;
  &lt;/span&gt;

Which I want them to be enclosed into a row, and then the second and forth element expand to fill the remain width, which the second and forth element would have same width. How can I do it?

I tried:

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

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

&lt;table style=&quot;width:100%;&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;white-space:nowrap;&quot;&gt;1111111111&lt;/td&gt;
    &lt;td style=&quot;width:100%;background-color:red;&quot;&gt;2222222222&lt;/td&gt;
    &lt;td style=&quot;white-space:nowrap;&quot;&gt;3333333333&lt;/td&gt;
    &lt;td style=&quot;width:100%;background-color:red;&quot;&gt;4444444444&lt;/td&gt;
    &lt;td style=&quot;white-space:nowrap;&quot;&gt;5555555555&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

Which the second element expands to "bully" the forth element, which is not working. (Note: it is not necessary to use table, just I think I can achieve the result and then use it, but stills not working)

答案1

得分: 0

将其他三个单元格设置为相同的宽度,并将2和4保持未定义。结果将是2和4均等地填充剩余的空间。

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

<!-- language: lang-html -->
    <table style="width:100%;">
      <tr>
        <td style="width:10%;">1111111111</td>
        <td style="background-color:red;">2222222222</td>
        <td style="width:10%;">3333333333</td>
        <td style="background-color:red;">4444444444</td>
        <td style="width:10%;">5555555555</td>
      </tr>
    </table>

<!-- end snippet -->

请注意,代码部分没有翻译,仅返回翻译好的内容。

英文:

Set the other three cells to a common width and leave 2 and 4 undefined. The result will be that both 2 and 4 will fill the remaining space equally.

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

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

&lt;table style=&quot;width:100%;&quot;&gt;
  &lt;tr&gt;
    &lt;td style=&quot;width:10%;&quot;&gt;1111111111&lt;/td&gt;
    &lt;td style=&quot;background-color:red;&quot;&gt;2222222222&lt;/td&gt;
    &lt;td style=&quot;width:10%;&quot;&gt;3333333333&lt;/td&gt;
    &lt;td style=&quot;background-color:red;&quot;&gt;4444444444&lt;/td&gt;
    &lt;td style=&quot;width:10%;&quot;&gt;5555555555&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;

<!-- end snippet -->

答案2

得分: 0

你可以使用 flex-growflex-basis 的组合来实现这个效果。请注意,下面的第二个和第四个 <div> 元素具有相同的宽度,即使父容器中的剩余空间不同。

<div style="display: flex; width: 200px;">
  <div style="background: red;">1</div>
  <div style="background: orange; flex-basis: 1; flex-grow: 1;">2</div>
  <div style="background: yellow;">3</div>
  <div style="background: green; flex-basis: 1; flex-grow: 1;">4</div>
  <div style="background: blue;">5</div>
</div>
<br/>
<div style="display: flex; width: 200px;">
  <div style="background: red;">1111</div>
  <div style="background: orange; flex-basis: 1; flex-grow: 1;">2</div>
  <div style="background: yellow;">3333</div>
  <div style="background: green; flex-basis: 1; flex-grow: 1;">4</div>
  <div style="background: blue;">5555</div>
</div>
英文:

You can use a combination of flex-grow and flex-basis to achieve this. Note that the second and fourth divs below are the same width as each other, even when the remaining space in the parent is different.

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

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

&lt;div style=&quot;display: flex; width: 200px;&quot;&gt;
  &lt;div style=&quot;background: red;&quot;&gt;1&lt;/div&gt;
  &lt;div style=&quot;background: orange; flex-basis: 1; flex-grow: 1;&quot;&gt;2&lt;/div&gt;
  &lt;div style=&quot;background: yellow;&quot;&gt;3&lt;/div&gt;
  &lt;div style=&quot;background: green; flex-basis: 1; flex-grow: 1;&quot;&gt;4&lt;/div&gt;
  &lt;div style=&quot;background: blue;&quot;&gt;5&lt;/div&gt;
&lt;/div&gt;
&lt;br/&gt;
&lt;div style=&quot;display: flex; width: 200px;&quot;&gt;
  &lt;div style=&quot;background: red;&quot;&gt;1111&lt;/div&gt;
  &lt;div style=&quot;background: orange; flex-basis: 1; flex-grow: 1;&quot;&gt;2&lt;/div&gt;
  &lt;div style=&quot;background: yellow;&quot;&gt;3333&lt;/div&gt;
  &lt;div style=&quot;background: green; flex-basis: 1; flex-grow: 1;&quot;&gt;4&lt;/div&gt;
  &lt;div style=&quot;background: blue;&quot;&gt;5555&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定