---
title: "Writing future-proof UnoCSS | Unai Mengual"
description: "UnoCSS is flexible enough to let you write HTML that isn't valid. Here is how to keep attributes and tags within the spec."
canonical_url: "https://owlnai.com/blog/writing-future-proof-unocss"
meta:
  "og:description": "UnoCSS is flexible enough to let you write HTML that isn't valid. Here is how to keep attributes and tags within the spec."
  "og:image": "https://owlnai.com/_og/s/o_60f531627056d811.png"
  "og:image:height": "600"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "Writing future-proof UnoCSS | Unai Mengual"
  "twitter:image": "https://owlnai.com/_og/s/o_60f531627056d811.png"
---

> Index of every page on this site: [llms.txt](https://owlnai.com/llms.txt)

# Writing future-proof UnoCSS

11 May 2023

[All posts](https://owlnai.com/blog)

I've been using [UnoCSS](https://unocss.dev) since its early days, and I'm quite amazed by its limitless flexibility. However, flexibility comes at a price and can lead you to misuse.

Note that this post focuses on UnoCSS using the default presets: Uno, Attributify and Tagify.

## Ways to use UnoCSS

Before we start, let's review the ways we can use UnoCSS with the main presets:

- **Classes** `<div class="dark:bg-gray-900">`
- **Attributes** `<div dark="bg-gray-900">`
- **Tags** `<dark-bg-gray-900>`

## Attributes

The [attributify preset](https://unocss.dev/presets/attributify) allows you to use HTML attributes instead of CSS classes as long as they are within the Unicode ranges supported by the specification.

However, that doesn't mean it strictly adheres to the HTML standard. For example, the following code:

```html
<div dark="bg-gray-950">
```

It *technically* works in modern browsers, but if you run a validator you'll notice that "dark" is not a valid attribute in `div` tags.

Why is this the case? Custom attributes can conflict with future HTML attributes, causing unexpected behavior or rendering issues.

### Using data-\* attributes

So what do we do? `data-*` attributes allow us to extend HTML tags with any non-standard attribute we can think of. Of course, this also applies to the utilities generated by UnoCSS.

```html
<div data-dark="bg-gray-950">
```

By default, the attributify preset doesn't support them, but you can enforce it by using this configuration:

```js
presetAttributify({
  prefix: 'data-', // or 'data-un-'
  prefixedOnly: true,
})
```

Problem solved!

## Tags

[Tags](https://unocss.dev/presets/tagify) have a similar problem. Custom HTML elements [*should*](https://www.ietf.org/rfc/rfc2119.html#section-3) follow [these rules](https://html.spec.whatwg.org/multipage/custom-elements.html#prod-potentialcustomelementname):

- The name of a custom element must contain a hyphen ("-").
- The name must start with a letter (a-z or A-Z).
- The name can contain lowercase letters (a-z), uppercase letters (A-Z), digits (0-9), hyphens ("-"), and must not contain any whitespace or special characters.

### Hyphens

Most utilities like `<text-red-300>` work fine, but prefixed tags like `dark:` or `print:` may be problematic, as colons are not supported. In fact, tagify won't work at all!

Let's say we have this example:

```html
<dark:text-red-300>
```

We should replace the colon with a hyphen:

```html
<dark-text-red-300>
```

### Prefixes

Many utilities, such as `<flex>` or `<hidden>`, do not contain dashes because they consist of single words. Splitting them, for example into `<fl-ex>`, wouldn't make sense.

Instead, we can use a [namespace prefix](https://www.webcomponents.org/community/articles/web-components-best-practices):

```js
presetTagify({
  prefix: 'un-'
})
```

This means that you can write utilities like `<un-flex>` or `<un-dark-flex>` and be at ease since no new elements containing hyphens will be added to the HTML specification.

First published on [DEV](https://dev.to/owlnai/writing-future-proof-unocss-5b68).

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://owlnai.com/#website","@type":"WebSite","description":"Building for the web from Spain. Nuxt, Vue and open source.","name":"Unai Mengual","url":"https://owlnai.com/"},{"@id":"https://owlnai.com/blog/writing-future-proof-unocss#webpage","@type":"WebPage","description":"UnoCSS is flexible enough to let you write HTML that isn't valid. Here is how to keep attributes and tags within the spec.","name":"Writing future-proof UnoCSS","url":"https://dev.to/owlnai/writing-future-proof-unocss-5b68","isPartOf":{"@id":"https://owlnai.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://dev.to/owlnai/writing-future-proof-unocss-5b68"]}]}]}
```

## Sitemap

See the full [sitemap](https://owlnai.com/sitemap.md) for all pages.
