简体   繁体   English

设置折叠优先级而不对CSS项目应用flex?

[英]Set collapsing priority without applying flex to CSS items?

I have a <ul> element that is styled to list its inner <li> elements horizontally: 我有一个<ul>元素,其样式是水平列出其内部<li>元素:

one is the 1st number | 一个是第一个数字| two is the 2nd number | 二是第二个数字| three is the 3rd number | 三是第三个数字| four is the 4th number 四是第四个数字

This works fine for shorter content, but these "cells" need to collapse with varying priorities if there's not enough space to display them without wrapping . 这适用于较短的内容,但如果没有足够的空间来显示它们而没有包装 ,这些“单元”需要以不同的优先级崩溃。

...Something like this: ......像这样:

one is the first number | 一个是第一个数字| two is the... | 二是... | three is the... | 三是... | f... F...

So, the first cell should always display 100% of its content, the second two cells should display ~40-50% (fluidly or as a hard-coded width/percentage), and the 4th cell can take up whatever's left (if anything)... without overflowing the parent container's bounds. 因此,第一个单元格应始终显示其内容的100%,后两个单元格应显示~40-50%(流畅地或作为硬编码的宽度/百分比),并且第四个单元格可以占用剩余的任何内容(如果有的话) )...没有溢出父容器的边界。

Oh, and these items all need to be equally vertically-aligned for aesthetics. 哦,这些物品都需要同样垂直对齐才能美观。

Possible to do cleanly without flex/JS? 没有flex / JS可以干净利落地做什么? I'm not sure. 我不确定。 The idea is to create something visually appealing that can display as much information from the listed sections as possible within that single line, with the first one always displaying all of its content without ellipsis/wrapping/overflow and taking the highest priority in that line. 我们的想法是创建一些视觉上吸引人的东西,它可以在单行中尽可能多地显示列出的部分中的信息,第一个信息总是显示其所有内容而没有省略号/包装/溢出并在该行中占据最高优先级。


EDIT: I just want to clarify that setting min-width won't work here because the content could render in less than the min-width . 编辑:我只想澄清设置min-width在这里不起作用,因为内容可以在小于min-width情况下呈现。 (consider "abc" instead of "two is the..." ... That should only take the space of "abc" for <li> . (考虑“abc”而不是“two is the ......”......这应该只为<li>占用“abc”的空间。

So this what I can conjure up with flexbox - I guess this will get you started: 所以这就是我能flexbox - 我想这会让你开始:

  1. A flexbox of ul with white-space:nowrap to all the flex children to keep the text content in a single line. flexboxulflexbox white-space:nowrap到所有flex子flexbox ,以将文本内容保持在一行中。

  2. Added ellipsis to the flex children except the first one as per your requirement. 根据您的要求,向第一个孩子添加ellipsis ,除了第一个孩子。

  3. The magic here is done by: 这里的魔力是由:

     ul > li:first-child { flex: 0 1 auto; } ul > li:nth-child(2) { flex: 0 1 auto; } ul > li:nth-child(3) { flex: 0 2 auto; } ul > li:nth-child(4) { flex: 0 3 auto; } 

    a. 一个。 For the first flex child, we disable flex-grow 对于第一个flex子,我们禁用flex-grow

    b. For the other flex children, we use relative flex ing. 对于其他弹性儿童,我们使用相对flex

Let me know your feedback on this. 请告诉我您对此的反馈。 Thanks! 谢谢!

 ul { list-style-type: none; display: flex; padding: 0; margin: 0; } ul > li { white-space: nowrap; padding: 5px; } ul > li:not(:last-child) { border-right: 1px solid; } ul > li:not(:first-child) { overflow: hidden; text-overflow: ellipsis; } ul > li:first-child { flex: 0 1 auto; } ul > li:nth-child(2) { flex: 0 1 auto; } ul > li:nth-child(3) { flex: 0 2 auto; } ul > li:nth-child(4) { flex: 0 3 auto; } 
 <ul> <li>one is the 1st number</li> <li>two is the 2nd number</li> <li>three is the 3rd number</li> <li>four is the 4th number</li> </ul> 

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

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