I would like to create a list in HTML that looks like
2024: Short announcement
2025: Long announcement that goes to over to the next line, it has
and may have multiple sentences.
My naïve HTML attempt:
<ul>
<li> 2023: Short announcement </li>
<li> 2025: Long announcement that goes to over to the next line, it has <br> and may have multiple sentences. <br> More text and some more text and so on. </li>
</ul>
My problem is that "More" appears directly under 2025 while I want it to make it appear under "Long". One workaround is to use multiple nbsp; but that seems wrong. Any suggestions?
If it can’t be done with pure HTML I can use CSS (so I am adding the tag) but I cannot use JS.
Edit: Dates are actually not consecutive so tricks based on that will not work.
7
Answers
You can achieve the desired list layout with pure HTML by using the
<dl>
element, which is suitable for pairing terms with their descriptions.You can use CSS to style the list items. You can use a combination of
display: flex;
andflex-wrap: wrap;
to allow the items to flow to the next line while aligning properly.You can achieve this by adding some CSS to your list.
Here’s how:
Use
display: grid
withgrid-template-columns: subgrid
:You can accomplish this with a custom start value and a custom counter style. Your requirements aren’t clear, though, as I’m not sure if you’ll have multiple items with the same integer value, etc.
If you need specific integers, I recommend the approach by Petros.
Note that this approach has accessibility and SEO concerns, considering the content that’s not represented in the DOM.
Just use flexbox on the
li
with the date in aspan
.Apart from how you achieve the visual appearance, it also depends on the semantics you want to convey.
If you see it more as tabular data you would use
table
.If you think it is a list then you can use
dl
and set a grid layout:If you want to make it look like a list with the bullet you could add it back with
::before
.Depending on the content of the first column, you might need to prevent line breaks using CSS if that it not desired.
I would suggest using
<dl>
tags to separate label and content; and use CSS grid to align items nicely.