gpt4 book ai didi

html - 在溢出前将不同长度的元素包裹 X 次

转载 作者:行者123 更新时间:2023-12-04 14:43:37 24 4
gpt4 key购买 nike

我正在尝试显示一个简单的标签列表,其中包含不同长度的标签,这些标签将跨越 3 行,然后水平溢出,同时使用 React 可滚动。

标签包含文本、边框,除此之外别无其他:

tag flex

这里有一张宏伟的图来说明我试图用不同数量的标签渲染的内容:

finest art

这是我目前使用的代码,它的标签在错误的方向垂直溢出:

:root {
--scpu-bg: #1b1b1b;
--scpu-border: #3d3d3d;

--scpu-fg: #ffffff;

color: var(--scpu-fg);

font-size: 14px;
}

body {
background-color: var(--scpu-bg);
}

.tag-chip {
padding: 1rem;
border: 1px var(--scpu-border) solid;
}

.tag-cloud {
overflow-x: scroll;
}

.tag-list {
/*display: grid;

grid: 1fr 1fr 1fr / ;*/

max-height: 11rem;
display: flex;
gap: 0.7rem;
flex-wrap: wrap;
}
<div class="tag-cloud">
<div class="tag-list nav-scroll">
<span class="tag-chip">6000</span><span class="tag-chip">alagadda</span
><span class="tag-chip">artifact</span><span class="tag-chip">audio</span
><span class="tag-chip">biological</span
><span class="tag-chip">blackwood</span><span class="tag-chip">_cc</span
><span class="tag-chip">co-authored</span
><span class="tag-chip">container</span><span class="tag-chip">d-11424</span
><span class="tag-chip">daevite</span
><span class="tag-chip">deer-college</span
><span class="tag-chip">doctor-light</span
><span class="tag-chip">doctor-mcdoctorate</span
><span class="tag-chip">entropic</span
><span class="tag-chip">esoteric-class</span
><span class="tag-chip">ethics-committee</span
><span class="tag-chip">faeowynn-wilson</span
><span class="tag-chip">global-occult-coalition</span
><span class="tag-chip">grand-karcist-ion</span
><span class="tag-chip">hanged-king</span
><span class="tag-chip">hy-brasil</span
><span class="tag-chip">interactive</span
><span class="tag-chip">la-rue-macabre</span
><span class="tag-chip">_licensebox</span><span class="tag-chip">meta</span
><span class="tag-chip">metallic</span
><span class="tag-chip">narrative</span
><span class="tag-chip">on-guard-43</span
><span class="tag-chip">portal</span><span class="tag-chip">religious</span
><span class="tag-chip">ritual</span
><span class="tag-chip">s&amp;amp;c-plastics</span
><span class="tag-chip">scp</span><span class="tag-chip">serpents-hand</span
><span class="tag-chip">spacetime</span
><span class="tag-chip">temporal</span
><span class="tag-chip">three-moons-initiative</span
><span class="tag-chip">three-portlands</span
><span class="tag-chip">wanderers-library</span
><span class="tag-chip">weapon</span
><span class="tag-chip">wilsons-wildlife</span>
</div>
</div>

我目前正在使用 display: flexflex-wrap: wrap 来尽可能接近我正在尝试做的事情。

简单地说,我希望标签最多放在 3 行上,无论宽度有多大,同时让标签保持自己的宽度。

不幸的是,我只知道组件应该具有的高度,并且环绕效果不适用于溢出 AFAIK。

If you already know the height of your component, but not the width, why not use flex-direction: column ?

因为每个元素都被赋予了相同的宽度,形成了一个尴尬的组合:

enter image description here

If you have a set amount of rows, why aren't you using display: grid ?

列不会遇到与上述相同的问题吗?

除了将我的标签数组分成 3 个部分并独立呈现它们之外,还有其他解决方案吗?

最佳答案

仅通过 CSS,您可以尝试 writing-mode 和 column CSS,最后通过 min-content/max-content 和 max-height:100% 重置大小;


记住一次 Masonry Layout将被广泛实现(在该答案时仅 FF),该答案将过时并且 this answer就是您要找的那个。

Warning: This feature is only implemented in Firefox, and can be enabled by setting the flag layout.css.grid-template-masonry-value.enabled to true in about:config, in order to allow testing and providing of feedback.


这是实际的想法(欢迎提供有关浏览器故障的反馈)

:root {
--scpu-bg: #1b1b1b;
--scpu-border: #3d3d3d;
--scpu-fg: #ffffff;
color: var(--scpu-fg);
font-size: 14px;
}

body {
background-color: var(--scpu-bg);
}

.tag-chip {
padding: 1rem;
border: 1px var(--scpu-border) solid;
}

.tag-cloud {
overflow-x: scroll;
max-height: 11rem;
padding-bottom: 1.2rem;
display: flex;
}

.tag-list {
column-count: 3;
column-rule-width:0;
writing-mode: vertical-lr;
width: max-content;
height: min-content;
max-height: 100%;
padding-right:2px;
}

.tag-chip {
writing-mode: horizontal-tb;
margin: 2px;
}
<div class="tag-cloud">
<div class="tag-list nav-scroll">
<span class="tag-chip">6000</span><span class="tag-chip">alagadda</span
><span class="tag-chip">artifact</span><span class="tag-chip">audio</span
><span class="tag-chip">biological</span
><span class="tag-chip">blackwood</span><span class="tag-chip">_cc</span
><span class="tag-chip">co-authored</span
><span class="tag-chip">container</span><span class="tag-chip">d-11424</span
><span class="tag-chip">daevite</span
><span class="tag-chip">deer-college</span
><span class="tag-chip">doctor-light</span
><span class="tag-chip">doctor-mcdoctorate</span
><span class="tag-chip">entropic</span
><span class="tag-chip">esoteric-class</span
><span class="tag-chip">ethics-committee</span
><span class="tag-chip">faeowynn-wilson</span
><span class="tag-chip">global-occult-coalition</span
><span class="tag-chip">grand-karcist-ion</span
><span class="tag-chip">hanged-king</span
><span class="tag-chip">hy-brasil</span
><span class="tag-chip">interactive</span
><span class="tag-chip">la-rue-macabre</span
><span class="tag-chip">_licensebox</span><span class="tag-chip">meta</span
><span class="tag-chip">metallic</span
><span class="tag-chip">narrative</span
><span class="tag-chip">on-guard-43</span
><span class="tag-chip">portal</span><span class="tag-chip">religious</span
><span class="tag-chip">ritual</span
><span class="tag-chip">s&amp;amp;c-plastics</span
><span class="tag-chip">scp</span><span class="tag-chip">serpents-hand</span
><span class="tag-chip">spacetime</span
><span class="tag-chip">temporal</span
><span class="tag-chip">three-moons-initiative</span
><span class="tag-chip">three-portlands</span
><span class="tag-chip">wanderers-library</span
><span class="tag-chip">weapon</span
><span class="tag-chip">wilsons-wildlife</span>
</div>
</div>

关于html - 在溢出前将不同长度的元素包裹 X 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69194293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com