-1

In one table, I have fixed content for each class.

    <td class="matched">Matched</td>
    ...
    <td class="not-matched">Not Matched</td>

Is there way to avoid typing content every time?

    <td class="matched"></td>
    ...
    <td class="not-matched"></td>
5
  • are you using some kind of template engine or pure HTML / CSS?
    – Martin
    Commented Dec 26, 2019 at 18:20
  • :after or :before come to mind.
    – Sirko
    Commented Dec 26, 2019 at 18:21
  • @Martin, I prefer just HTML/CSS.
    – user180574
    Commented Dec 26, 2019 at 18:21
  • @HereticMonkey, looks like it is, thank you!
    – user180574
    Commented Dec 26, 2019 at 18:26
  • 1
    In my opinion its preferable to use javascript for this type of feature. In this case the text will be directly injected in the balise and not as a ::after element. Commented Dec 26, 2019 at 18:34

1 Answer 1

0

You can use the ::before or ::after selectors to create a pseudo-element, and apply a content to it.

.matched::before {
  content: "Matched";
}

.not-matched::before {
  content: "Not Matched";
}
<table>
  <tr>
    <td class="matched"></td>
    <td class="not-matched"></td>
  </tr>
  <tr>
    <td class="matched"></td>
    <td class="not-matched"></td>
  </tr>
  <tr>
    <td class="matched"></td>
    <td class="not-matched"></td>
  </tr>
</table>

The downside of this is that those texts are not selectable.


You can also optimize the code like below:

[class]::before {
  content: attr(class);
}
<table>
  <tr>
    <td class="matched"></td>
    <td class="not-matched"></td>
  </tr>
  <tr>
    <td class="matched"></td>
    <td class="not-matched"></td>
  </tr>
  <tr>
    <td class="matched"></td>
    <td class="not-matched"></td>
  </tr>
</table>

Not the answer you're looking for? Browse other questions tagged or ask your own question.