# The “charset” attribute on the “link” element is obsolete. Use an HTTP Content-Type header on the linked resource instead.

> Canonical HTML version: https://rocketvalidator.com/html-validation/the-charset-attribute-on-the-link-element-is-obsolete-use-an-http-content-type-header-on-the-linked-resource-instead
> Attribution: Rocket Validator (https://rocketvalidator.com)
> License: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/)

In earlier versions of HTML, the `charset` attribute on `<link>` was used to indicate the character encoding of the linked resource (such as a stylesheet or other external file). The HTML Living Standard has since made this attribute obsolete. Modern browsers determine the character encoding of linked resources through the `Content-Type` HTTP response header — for example, `Content-Type: text/css; charset=UTF-8` — or by using default encoding rules defined in the relevant specification (CSS files, for instance, default to UTF-8).

Using the obsolete `charset` attribute creates several issues. It gives a false sense of control over encoding, since browsers don't actually use it. It also clutters your markup with unnecessary attributes, and it may signal to other developers that this is the mechanism being relied upon for encoding, when in reality it has no effect. For standards compliance and clean, future-proof markup, it should be removed.

The fix involves two steps: first, remove the `charset` attribute from your HTML; second, ensure your web server sends the correct `Content-Type` header for the linked resources.

## Examples

### Incorrect: using `charset` on a `<link>` element

```html
<link rel="stylesheet" href="styles.css" charset="UTF-8">
<link rel="stylesheet" href="print.css" charset="iso-8859-1">
```

Both of these will trigger the validation warning because the `charset` attribute is obsolete on `<link>`.

### Correct: `charset` attribute removed

```html
<link rel="stylesheet" href="styles.css">
<link rel="stylesheet" href="print.css">
```

Simply removing the attribute resolves the validation error. The server should handle encoding declaration instead.

### Full document example

```html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Example Page</title>
  <link rel="stylesheet" href="styles.css">
</head>
<body>
  <h1>Hello, World!</h1>
</body>
</html>
```

## Configuring the server to declare character encoding

Once you've removed the `charset` attribute, make sure your web server sends the proper `Content-Type` header with the linked resources. Most modern servers already default to UTF-8 for CSS and JavaScript files, but you can configure this explicitly.

### Apache

Add the following to your `.htaccess` file or server configuration:

```
<FilesMatch "\.css$">
    AddCharset UTF-8 .css
</FilesMatch>
```

This causes Apache to serve CSS files with the header `Content-Type: text/css; charset=UTF-8`.

### Nginx

Add a `charset` directive to the relevant location block in your server configuration:

```
location ~* \.css$ {
    charset utf-8;
}
```

### Node.js (Express)

Set the `Content-Type` header explicitly when serving the file:

```javascript
app.get('/styles.css', function(req, res) {
  res.setHeader('Content-Type', 'text/css; charset=UTF-8');
  res.sendFile(__dirname + '/styles.css');
});
```

If you're using Express's built-in static file middleware (`express.static`), it typically sets correct content types automatically based on file extensions.

### Verifying the fix

You can confirm that the server is sending the correct header by opening your browser's developer tools, navigating to the **Network** tab, clicking on the linked resource, and inspecting the `Content-Type` response header. It should include `charset=UTF-8` (or whichever encoding your file uses). Once the attribute is removed from your HTML and the server is configured correctly, the validation warning will be resolved.
