同宽的子元素在内联弹性盒中

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

same-width childen in inline-flex

问题

我正在尝试将一组按钮显示为一个统一的组件,可以内联在段落中使用,例如。到目前为止,我发现的最佳方法是将它们包装在 <span> 中,并使用 display: inline-flex 进行样式设置。

它运行正常,但现在我还想使每个按钮具有相同的宽度,而不占用比必要更多的空间。换句话说,我希望每个按钮的宽度都与具有最长内容的按钮需要的最小宽度相同。

有没有办法只使用 CSS 来实现这一点?

以下是我目前的一些示例代码:

  1. button {
  2. border: 1px gray solid;
  3. border-radius: 5px;
  4. }
  5. span.button-group {
  6. display: inline-flex;
  7. }
  8. span.button-group button:not(:last-child){
  9. border-top-right-radius: 0;
  10. border-bottom-right-radius: 0;
  11. border-right-width: 0;
  12. }
  13. span.button-group button:not(:first-child){
  14. border-top-left-radius: 0;
  15. border-bottom-left-radius: 0;
  16. }
  1. <p>
  2. Here is a paragraph with a set of buttons:
  3. <span class="button-group">
  4. <button>A</button>
  5. <button>Longer</button>
  6. <button>B</button>
  7. <button>C</button>
  8. </span>
  9. </p>
英文:

I'm trying to make a group of buttons appear as a unified component that can be used inline in a paragraph for example. The best way I've found so far is to wrap them in a &lt;span&gt; and style it with display: inline-flex.

It works fine, but now I would also like to make every button the same width, without taking up any more space than necessary. In other words, I want each button to be as wide as the one with the longest content needs to be minimally.

Is there a way to achieve that using only css?

Here's some example code of of what I have so far:

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

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

  1. button {
  2. border: 1px gray solid;
  3. border-radius: 5px;
  4. }
  5. span.button-group {
  6. display: inline-flex;
  7. }
  8. span.button-group button:not(:last-child){
  9. border-top-right-radius: 0;
  10. border-bottom-right-radius: 0;
  11. border-right-width: 0;
  12. }
  13. span.button-group button:not(:first-child){
  14. border-top-left-radius: 0;
  15. border-bottom-left-radius: 0;
  16. }

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

  1. &lt;p&gt;
  2. Here is a paragraph with a set of buttons:
  3. &lt;span class=&quot;button-group&quot;&gt;
  4. &lt;button&gt;A&lt;/button&gt;
  5. &lt;button&gt;Longer&lt;/button&gt;
  6. &lt;button&gt;B&lt;/button&gt;
  7. &lt;button&gt;C&lt;/button&gt;
  8. &lt;/span&gt;
  9. &lt;/p&gt;

<!-- end snippet -->

答案1

得分: 4

据我所知,如果按钮的数量不同,使用 flexbox 确实不太可能,但您可以使用 CSS 网格布局来实现:

  1. button {
  2. border: 1px gray solid;
  3. border-radius: 5px;
  4. }
  5. span.button-group {
  6. display: inline-grid;
  7. grid-auto-flow: column;
  8. grid-auto-columns: 1fr;
  9. }
  10. span.button-group button:not(:last-child) {
  11. border-top-right-radius: 0;
  12. border-bottom-right-radius: 0;
  13. border-right-width: 0;
  14. }
  15. span.button-group button:not(:first-child) {
  16. border-top-left-radius: 0;
  17. border-bottom-left-radius: 0;
  18. }

grid-auto-flow: column; 会自动创建足够的列以将所有项目放在一行中,grid-auto-columns: 1fr; 使所有列的宽度相同(因此所有列的宽度将与最宽的列相同)。

英文:

As far as I know it's not really possible using flexbox if the amount of buttons can differ, but you can use css grid for that:

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

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

  1. button {
  2. border: 1px gray solid;
  3. border-radius: 5px;
  4. }
  5. span.button-group {
  6. display: inline-grid; grid-auto-flow: column; grid-auto-columns: 1fr;
  7. }
  8. span.button-group button:not(:last-child){
  9. border-top-right-radius: 0;
  10. border-bottom-right-radius: 0;
  11. border-right-width: 0;
  12. }
  13. span.button-group button:not(:first-child){
  14. border-top-left-radius: 0;
  15. border-bottom-left-radius: 0;
  16. }

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

  1. &lt;p&gt;
  2. Here is a paragraph with a set of buttons:
  3. &lt;span class=&quot;button-group&quot;&gt;
  4. &lt;button&gt;A&lt;/button&gt;
  5. &lt;button&gt;Longer&lt;/button&gt;
  6. &lt;button&gt;B&lt;/button&gt;
  7. &lt;button&gt;C&lt;/button&gt;
  8. &lt;/span&gt;
  9. &lt;/p&gt;

<!-- end snippet -->

grid-auto-flow: column; makes automatically enough columns to put all items in one row, grid-auto-columns: 1fr; makes all columns the same width (so all will be the same as the largest one).

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

发表评论

匿名网友

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

确定