How to overwrite a standard HTML tag?

Viewed 114

I am writing a web site in Hugo and Tailwind CSS (and learning as I go) and one point is not clear after reading both Hugo and tailwind documentation: how to override standard tags?

As an example: h1. Imagine I would like it to apply to it classes .text-bold and .text-xl (these are Tailwind classes). What is the correct approach:

  • using SCSS to @extend these classes?
h1 {
    @extend .text-xl, .font-bold ;
}

→ does not work, the classes are not recognized:

Error: Error building site: TOCSS: failed to transform "styleScssSource.scss" (text/x-scss): SCSS processing failed: file "stdin", line 2, col 23: The target selector was not found.
Use "@extend .font-bold !optional" to avoid this error.
  • using some Hugo mechanism to replace on the fly <h1> with <h1 class="text-xl font-bold">? (I do not know if this can be done)

  • or something else?

2 Answers

Tailwind CSS provides @apply that allows exactly this.

h2 {
  @apply font-extrabold text-3xl my-5;
}

This can be done in Hugo by defining partials.

You need to create a file in layouts/partials (for instance layouts/partials/tag.html) which content is what you want to ultimately have in the HTML

<a href="/tags/{{ . }}" class="bg-gray-300 text-sm rounded-full p-1 px-2">{{ . }}</a>

This can be then used as {{ partial "tag.html" . }} in your code.

Related