简体   繁体   English

如何使用flexbox使所有标签的高度相同?

[英]How to make all labels the same height with flexbox?

I have created two columns of labels that flex and wrap. 我创建了两列可伸缩的标签。 I have two issues that I cannot figure out how to solve using CSS. 我有两个问题,我无法弄清楚如何使用CSS解决。

  1. I would like the height of all the labels to be equal to the tallest label. 我希望所有标签的高度都等于最高标签。
  2. The last element isn't half the width of the div, but expands to the entire width of the div. 最后一个元素不是div宽度的一半,而是扩展到div的整个宽度。

Here is my demo: http://jsfiddle.net/pdExf/869/ 这是我的演示: http : //jsfiddle.net/pdExf/869/

HTML: HTML:

<div>
  <label>
    <input type="checkbox"/>
    <span > 1-thisisaverrrrrrrrrrrrryyyyyy_long_word_with_no_spaces </span>
  </label>
  <label>
    <input type="checkbox"/>
    <span> 2-thisisaverrrrrrrrrrrrryyyyyy_long_word_with_no_spaces </span>
  </label>
  <label>
    <input type="checkbox"/>
    <span> 3-thisisaverrrrrrrrrrrrryyyyyy_long_word_with_no_spaces </span>
  </label>
  <label>
    <input type="checkbox"/>
    <span> 4-very_short_word </span>
  </label>
  <label>
    <input type="checkbox"/>
    <span> 5-medium_length_word </span>
  </label>
  <label>
    <input type="checkbox"/>
    <span> 6-still_no_spaces </span>
  </label>
  <label>
    <input type="checkbox"/>
    <span> 7-thisisaverrrrrrrrrrrrryyyyyy_long_word_with_no_spaces   </span>
  </label>
</div>

CSS: CSS:

span {
  margin-left: 14px;
  word-break: break-all;
  white-space: pre-wrap; /* css-3 */
  white-space: -moz-pre-wrap; /* Mozilla, since 1999 */
  white-space: -pre-wrap; /* Opera 4-6 */
  white-space: -o-pre-wrap; /* Opera 7 */
  word-wrap: break-word; /* Internet Explorer 5.5+ */
  background-color: yellow;
}

label {
  box-sizing: border-box;
  background: white;
  color: black;
  padding: 10px;
  margin: 10px 10px 20px 10px;
  border: 1px solid black;
  flex-grow: 1;
  flex-shrink: 1;
  width: calc(100% / 3); // n_rows + 1;
  max-width: calc(50% - 20px); // 50% minus the pixels
}

div {
  height: 200px;
  background-color: lightblue;
  overflow: scroll;
  display: flex;
  flex-wrap: wrap;
}

UPDATE: I've figured how the second requirement. 更新:我想出了第二个要求。 However, the first one remains at large. 但是,第一个仍然很大。 How could I make the labels equal to the largest one using js if CSS isn't possible? 如果无法使用CSS,如何使用js使标签等于最大标签?

Set the max width on the label to be max-width: calc(100%/3); 将标签上的最大宽度设置为最大宽度max-width: calc(100%/3); for the issues with the last element taking up the whole screen. 最后一个元素占用整个屏幕的问题。

Unfortunately the only way I can think helping the situation in css is setting a minimum height but there I always a chance to go over that height. 不幸的是,我认为可以帮助解决CSS问题的唯一方法是设置最小高度,但是我总是有机会超过该高度。 The only way I can truly think of is using Javascript. 我真正想到的唯一方法是使用Javascript。

What exactly are you going for? 你到底要干什么 You set the width of each label to 100%/3 and then you've given them flex-grow:1; 您将每个标签的宽度设置为100%/ 3,然后为其赋予flex-grow:1; which is making them all larger, anyway. 无论如何,这都使它们变大了。 Remove flex-grow and that problem will be solved, no? 删除flex-grow即可解决问题,不是吗? Or give the last-child a smaller grow-value? 还是给最后一个孩子一个较小的成长价值? I'm not sure what it's supposed to look like. 我不确定应该是什么样子。

Making them all the same height as the tallest isn't possible in CSS. 在CSS中无法使它们与最高高度相同。 With JavaScript you could make a function that runs whenever the window is resized. 使用JavaScript,您可以使函数在窗口调整大小时运行。 It would have to do the following: 它必须执行以下操作:

1) Set all heights to auto.

2) Iterate through all heights to find the largest value.

3) Set all heights (or set min-heights) to the newfound value.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM