devcoder
devcoder

Reputation: 37

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. 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. 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.

The elements are in spans with a class of jobdate-item-date, jobdate-item-file-total and 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 -

      <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 -

.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;
}

Upvotes: 0

Views: 111

Answers (1)

elDrimm
elDrimm

Reputation: 106

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. 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>

Upvotes: 2

Related Questions