我想从嵌套数组创建网格模板. 如果元素自然具有相同的高度,则此方法有效,但当值太长时,某些元素具有不同的高度

<div id="container" [style.grid-template-columns]="'repeat(outerArray.length, 1fr)'" [style.columnGap.rem]="1" *ngIf="outerArray.length != 0">
  <div class="outerArray" *ngFor="let inner of outerArray">
    <div *ngFor="let item of inner">
      {{item}}
    </div>
  </div>
</div>

结果项目应该这样放置:

内在1价值1 内在2值1 内在3价值1

内在1价值2 内在2值2 内在3值2

内在1价值3

With grid look like this: (array with 8 inner arrays) Layout using grid solution, blurred strings

我想让列中的每个元素保持相同的宽度(但列可以有不同的宽度),并且行中保持相同的高度.

表工作得很好,但来自一个内部数组的值填充行而不是列,而且,在我看来,表的语义不适合我的需要.

<table *ngIf="outerArray.length != 0">
  <tbody>
    <tr *ngFor="let inner of outerArray">
      <td *ngFor="let item of inner;">
        {item}}
      </td>
    </tr>
  </tbody>
</table>

数组的大小不同(开始时更长,然后其余的则少一项)

示例数组可能看起来像这样:

let outerArray = [
  [
    "short",
    "bit longer",
    "example value"
  ],
  [
    "not so long, but longer string",
    "short words",
    "bitLonger twoWords"
  ],
  [
    "shorter array",
    "different lengths of strings"
  ],
  [
    "another shorter array",
    "string"
  ]
]

数组的数量和长度可能不同,有时一个数组有8个元素,但也可以有8个数组有2个元素. 字符串有一两个单词,但单词有时最多有15个字符,因此当内部数组很少时,字符串需要两行才能适应字符串宽度

我的完美解决方案是创建不嵌套的Divs并使用CSS样式正确放置它们

推荐答案

我们只需要有一套内部分区.您有一个嵌套的迪瓦 struct ,所以它会扰乱布局.我使用ng-container作为内部for循环,ng-container的特点是它不会在HTML中创建元素,所以我们只需使用它来运行for循环并创建一组divs.

此外,当您创建属性[style.grid-template-columns]="'repeat('+outerArray.length+', 1fr)'" 时,请确保字符串和JavaScript变量之间有明确的区分.

之前的代码只将repeat(outerArray.length, 1fr)呈现为字符串,而不是插入outerArray长度,因此我将代码更改为 'repeat('(字符串)+ outerArray.length(数字)+',1 fr '(字符串)

我们还需要[style.grid-template-rows],其配置与列相同.

如果您希望所有行的高度相同,请使用提供的注释CSS,它会向元素添加省略号并使所有单元格高度相同!

完整代码:完整代码:

import { CommonModule } from '@angular/common';
import { Component } from '@angular/core';
import { bootstrapApplication } from '@angular/platform-browser';
import 'zone.js';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule],
  styles: [`
  #container {
    display: grid;
  }
  /* .same-height {
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  } */
  `],
  template: `
    <div id="container" [style.grid-template-columns]="'repeat('+outerArray.length+', 1fr)'" [style.grid-template-rows]="'repeat('+outerArray.length+', 1fr)'" [style.columnGap.rem]="1" *ngIf="outerArray.length != 0">
      <ng-container class="outerArray" *ngFor="let inner of outerArray">
        <div *ngFor="let item of inner" style="background-color: red;color: white; margin-bottom:2px;" class="same-height">
          {{item}}
        </div>
      </ng-container>
    </div>
  `,
})
export class App {
  outerArray = [
    ['short', 'bit longer', 'example value'],
    ['not so long, but longer string', 'short words', 'bitLonger twoWords'],
    ['shorter array', 'different lengths of strings'],
    ['another shorter array', 'string'],
  ];
}

bootstrapApplication(App);

Stackblitz Demo

Css相关问答推荐

CSS动画与不正确的时间比率

JavaFX如何在Tableview中使用默认和自定义的列标题CSS样式?

Next.js中的TailWind CSS类名称疑难解答

使用SVG以竖排书写模式创建渐变文本时,文本显示异常

如何将 div 和 img 包装在 display:flex 容器中

如何向 Highcharts 迷你图添加具有淡入淡出效果的线性渐变?

为什么 :focus 会覆盖 :focus-visible ?

如何在使用 bslib 5 的shiny 应用程序中使用样式参数排列 ui 元素

在 CSS 中,如何用破折号填充段落中每一行的空白区域?

为什么同一个 CSS 类连续出现两次

CSS 在具有多个背景图像的元素上过滤一个背景图像

zoom 时闪烁的背景滤镜模糊

如何为柔和的配色方案提供易于使用的高对比度替代方案?

nth-child 在一个页面上工作,但不在另一个页面上

AngularJS Graphs & Charts - 实线和虚线的混合

CSS 常量()用于什么?

如何将 .btn-group 从 twitter-bootstrap 居中?

如何创建一个与 Bootstrap 3 配合得很好的粘性页脚

从样式设置为 % 的元素获取宽度(以像素为单位)?

可见性:隐藏 vs 显示:无 vs 不透明度:0