Lining up 2 wrapped elements inside a li (to perfection)

I often make long lists that have two elements in them: date & title.
Both elements are already wrapped and could look like this:

<li><span>12.05.15</span><a href="">Article Title</a></li>
<li><span>01.07.16</span><a href="">Blog Title</a></li>

If I use a monospaced font for the dates, I can give the span some padding-right, use margin-left and text-indent just right and everything seems to always line up pixel-perfect across devices.

However, I’m wondering if this is possible when not using a monospaced font for the dates? As you can see in the image, my usual technique then fails (more or less, depending on the spacing of the dates).

Is there a way I can perfectly line up the text titles (even when breaking) without using a table? Since both are already wrapped anyway, maybe there’s an obvious positioning technique I’m unfamiliar with. Or if I find one, I might spend 2 days and discover gotchas later… :frowning:

Thanks for looking! :slight_smile:

Here’s one easy way.

<meta charset="utf-8">
<title>Untitled Document</title>
ul {
	margin:0 auto;
li {
li span, li a {
	padding:0 10px 5px 0;
li span{width:1%}/* shrink cell to content width  */

  <li><span>12.05.15</span><a href="">Article Title</a></li>
  <li><span>01.07.16</span><a href="">Blog Title</a></li>

Wow, looks credible :slight_smile: TIA Paul!

I’ll get into it and will get back for more thanks & update after I try this today :sunglasses:

Another possibility - couldn’t this kind of list also be considered an example of tabular data. If so you could put it into an html table.

1 Like

Yes that’s very close to tabular data so semantically a table wouldn’t be that bad (which is effectively what I turned the list into as regards formatting).

The data could probably fit more semantically into a dl element with the date as the data term and the title as the data description but would be awkward to style in the required way.

1 Like

Thanks again, Paul. It’s working great! :slight_smile: :slight_smile:

For user feedback, I sometimes give the title a background color. This now highlights the whole cell, which is not always what I prefer. Is it possible to only go as far as the text in the cell?

You’d have to wrap the text in a span and give a background colour to the span as display:table-cell will always fit the parent’s width. Even if we removed the width from the table the right cells would always be as long as the longest content in that structure.

Thanks. That’s not a huge problem, cos it’s usually from a click and jQuery initiated, so I don’t have to put it on all text beforehand. I’m gonna try that :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.