Evan Miller
Evan Miller

Reputation: 389

HTML align text left and right on same line

I am hoping to find a better way to do this, but right now, for necessary reasons, I have a perl script that prints out html code. One of the subs uses a for loop to iterate through a directory and print out the name of the file with the last modified date:

  foreach my $result (@files) {
    if ($result =~ /\.txt/ ) {
      chomp $result;
      my $filename = basename($result);
      my $time = (stat("$result"))->[9] or die "$!";
      my $date = localtime $time;
      my $filestat = '<div style="text-align:left;">' . "$filename" . '</div><div style="text-align:right;">' . "$date" . '</div>';
      push(@final_result,$filestat);
    }
  }
  my $final_result_stat = join('<br>',@final_result);
  my $header = '<p style="text-align:left;">File Name<span style="float:right;">Last Modified Date</span></p>';
  return "$header" . "$final_result_stat";
}

The resulting html output is put in between tags and placed in a table in the body. The issue I am having is the text output (the $filename and $date) are not aligned on the same line. They seem to be one line beneath each other but $filename is aligned left and $date is aligned right. Also, the next resulting output is quite far spaced and I would like them to be more condensed.

Current:

Filename
                         Date

Filename
                         Date

I want:

Filename            Date
Filename            Date

Also, I know this is still in its rough stages, so any suggestions on how to make this cleaner and better are highly welcomed!

Upvotes: 4

Views: 2590

Answers (1)

emmanuel
emmanuel

Reputation: 9615

You have to convert this line:

my $filestat = '<div style="text-align:left;">' . "$filename" . '</div><div style="text-align:right;">' . "$date" . '</div>';

To:

my $filestat = '<div class="filename">' . "$filename" . '</div><div class="date">' . "$date" . '</div>';

And add the following css:

.filename {
  display: inline-block;
  width: 30%;
}
.date {
  display: inline-block;
  width: 70%;
}

Upvotes: 3

Related Questions