[英]How to wrap a whole group of elements on the next line when screen gets smaller using CSS?
On big screens, like desktops, it would look like this:在大屏幕上,比如台式机,它看起来像这样:
But in smaller screens or when zoomed in, I want my multiple lines to wrap like this:但在较小的屏幕或放大时,我希望我的多行像这样换行:
I tried this:我试过这个:
.tab__set { display: block; font-size: 1.2em; font-family: monospace; overflow: hidden; border: 1px solid; resize: horizontal; }.tab { min-height: 1.2rem; display: block; white-space: wrap; }
<span class="tab__set"> <span class="tab">e|------------------------|----------------------------|----------------------|-------------</span> <span class="tab">B|------------------------|-------2h3p2----5-----5--2--|------2-----2-----2---|---3-----3---</span> <span class="tab">G|------------------------|----2--------2--4-----4--2--|------2-----2-----2---|---2-----2---</span> <span class="tab">D|------------------------|----------------------------|----------------------|-------------</span> <span class="tab">A|------------------------|-0--------------------------|----------------------|-------------</span> <span class="tab">E|--------------------0h2-|----------------4--4-----2--|---2-----2--0--0-----0|---2--2-----2</span> </span>
Above, I tried using white-space: wrap;
上面,我尝试使用white-space: wrap;
but the result is not what I desire.但结果不是我想要的。 It doesn't wrap as a whole set of tabs like what it did on the second image above.它不像上面第二张图片那样包装成一整套标签。 Instead, it just wraps as individual elements.相反,它只是包装为单个元素。
Then I tried using white-space: nowrap;
然后我尝试使用white-space: nowrap;
and of course that doesn't work because it will not show all the tabs unless I scroll horizontally.当然这不起作用,因为除非我水平滚动,否则它不会显示所有选项卡。 I don't know how to make multiple elements go on a new line as a whole group.我不知道如何将多个元素 go 作为一个整体放在一个新行上。 Please help.请帮忙。
From my old answer and in case the number of lines is known you can approximate like below:根据我的旧答案,如果行数已知,您可以近似如下:
.tab__set { --s:1.2em; --n:6; font-size: var(--s); line-height: calc((var(--n) + 1)*var(--s)); font-family: monospace; position:relative; /* to illustrate */ overflow: hidden; border: 1px solid; resize: horizontal; word-break:break-all; }.tab__set.tab { display:block; transform:translateY(calc(-1*var(--n)*var(--s)/2)) }.tab__set.tab:not(:first-child) { position: absolute; top: 0; left: 0; right: 0; }.tab__set.tab:nth-child(2) {top:calc(1*var(--s));}.tab__set.tab:nth-child(3) {top:calc(2*var(--s));}.tab__set.tab:nth-child(4) {top:calc(3*var(--s));}.tab__set.tab:nth-child(5) {top:calc(4*var(--s));}.tab__set.tab:nth-child(6) {top:calc(5*var(--s));} /*.tab__set.tab:nth-child(N) {top:calc((N - 1)*var(--s));}
<div class="tab__set"> <span class="tab">e|------------------------|----------------------------|----------------------|------------</span> <span class="tab">B|------------------------|-------2h3p2----5-----5--2--|------2-----2-----2---|---3-----3---</span> <span class="tab">G|------------------------|----2--------2--4-----4--2--|------2-----2-----2---|---2-----2---</span> <span class="tab">D|------------------------|----------------------------|----------------------|-------------</span> <span class="tab">A|------------------------|-0--------------------------|----------------------|-------------</span> <span class="tab">E|--------------------0h2-|----------------4--4-----2--|---2-----2--0--0-----0|---2--2-----2</span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.