0

I want to make an ul that breakes into a few lines with flex-wrap. So far so good but, if there are multiple lines, I want all lines exept the last one to take the rest of the width of the line. So I used flex-grow: 1; on the li's. However as the last line shouldn't have flex-grow I just put an empty li after all the others an gave it flex-grow: 9999;, so that they don't take the whole width. (See Example for better understanding)

ul {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}

li {
  flex-grow: 1;
}

li:last-of-type {
  flex-grow: 9999;
}

/* Unimportant */

ul {
  list-style-type: none;
  text-align: center;
  width: 400px;
}

li {
  padding: 0.5em;
  background: blue;
}

li:hover {
  background: red;
}
<ul>
  <li>abcdefghijklm</li>
  <li>abcdefghijklm</li>
  <li>abcdefghijklm</li>
  <li>abcdefghijklm</li>
  <li>abcdefghijklm</li>
  <li></li>
</ul>

Now, I have the question: Does HTML5 allow to just put an li at the end only for styling reasons?

(If there exists another CSS function for using flex-grow on these lines, I would be very happy, but I don't think so.)


I already searched for this, but I didn't realy know how to search, so I couldn't find something.

Luka Theisen
  • 181
  • 1
  • 6
  • So long as the elements are placed within a permitted parent (`
  • ` can only appear wrapped within a `
      ` or `
      ` for example) then html has nothing to say about whether you use empty elements for styling purposes. Semantically it feels wrong to me, but it’s opinion based, so it only matters whether you’re happy to have empty elements as hooks for presentational styling.
  • – David Thomas Jul 06 '18 at 21:59
  • but why 9999 and not simply 1 like the other? – Temani Afif Jul 06 '18 at 22:02
  • 9999 is simply to prevent the li's in the last row to grow. Normal li's with flex-grow: 1; will only grow very little. (https://joren.co/flex-grow-9999-hack/) – Luka Theisen Jul 06 '18 at 22:06
  • Okay, thank you David Thomas. Think the answer below is perfect: Using the pseudo elements. – Luka Theisen Jul 06 '18 at 22:08