简体   繁体   English

如何垂直对齐文本列表中的所有元素?

[英]How can I vertically align all elements in a list of text?

I am trying to align these elements in my Angular application - date, file total and file size.我试图在我的 Angular 应用程序中对齐这些元素 - 日期、文件总数和文件大小。 If there is a larger number with more digits, then it is pushing the other elements away.如果有一个更大的数字和更多的数字,那么它会将其他元素推开。 I have tried changing the padding margins and have tried display: Flex, inline and inline-block.我尝试更改填充边距并尝试显示:Flex、内联和内联块。 I want the start of each element to line up even if the numbers have more digits or less, so say we have - Apr 1, 2019 1 file 3445 G the start of each element will line up with Mar 28, 2019 34 files 29282 G.我希望每个元素的开头对齐,即使数字有更多或更少,所以假设我们有 - 2019 年 4 月 1 日 1 个文件 3445 G 每个元素的开头将与 2019 年 3 月 28 日对齐 34 个文件 29282 G .

The elements are in spans with a class of jobdate-item-date, jobdate-item-file-total and jobdate-item-file-length.这些元素的跨度为 class 的 jobdate-item-date、jobdate-item-file-total 和 jobdate-item-file-length。 When you click on these they open up showing lists of job data.当您单击这些时,它们会打开显示作业数据列表。 Here is a picture and my current code -这是一张图片和我当前的代码 -

Html - Html -

      <div *ngFor="let date of selectedJob.dates" class="card-date-file">
        <div class="detail-item" (click)="toggle()">
          <span class="jobdate-item-date">{{ date.date | date: 'MMM dd, y' }}</span>
          <span class="jobdate-item-file-total">{{ date.files.length }} files</span>
          <span class="jobdate-item-file-length">{{ jobFileCalc(date.files) }} GB</span>
        </div>
        <ng-container *ngIf="show">
        <div *ngFor="let file of date.files" class="list" >
          <span class="file-item-filename">{{ file.filename }}</span>
          <span class="file-item-incr">{{ file.incr? 'INCR':'FULL' }}</span>
          <span class="file-item-time">{{ file.dttm | date:'h:mm' }}{{ file.dttm | date:'a' | lowercase }}</span>
          <span class="file-item-size">{{ file.sizegb.toFixed(1)| number : fractionSize }} GB</span>
        </div>
        </ng-container>
      </div>
    </div>

CSS - CSS -

.jobdate-item-date {
  padding: 0.1em 1.1em 0.3em 0.8em;
}

.jobdate-item-file-total {
  padding: 0.3em 1.1em 0.3em 1.1em;
}

.jobdate-item-file-length {
  padding: 0.3em 1.1em 0.3em 1.1em;
}

You could give every element a certain width (I used flexbox in my example), and if it is too long, you could truncate the text with some ellipses if you'd like.你可以给每个元素一个特定的宽度(我在我的例子中使用了 flexbox),如果它太长,你可以用一些省略号截断文本。 You can of-course play with the widths, or use percentages to achieve your wanted results.您当然可以使用宽度,或使用百分比来实现您想要的结果。

 .detail-item { display: flex; padding: 8px; }.jobdate-item-date, .jobdate-item-file-total, .jobdate-item-file-length { flex: 0 0 100px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
 <div class="detail-item" (click)="toggle()"> <span class="jobdate-item-date">Mar 29, 2019</span> <span class="jobdate-item-file-total">12 files</span> <span class="jobdate-item-file-length">2280.2 GB</span> </div> <div class="detail-item" (click)="toggle()"> <span class="jobdate-item-date">Apr 3, 2019</span> <span class="jobdate-item-file-total">2 files</span> <span class="jobdate-item-file-length">99.2 GB</span> </div> <div class="detail-item" (click)="toggle()"> <span class="jobdate-item-date">Apr 3, 2019</span> <span class="jobdate-item-file-total">2 files</span> <span class="jobdate-item-file-length">324324234234234232423 GB</span> </div>

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

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