Skip to main content

HTML Guide

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

The W3C Validator is indicating that the charset attribute on the link element is obsolete. According to modern HTML standards, the charset attribute should not be used on the <link> element, and instead, the character encoding should be specified via an HTTP Content-Type header on the server response of the resource.

Here’s how you can address and fix the issue:

1. Remove the charset attribute from the <link> element:

You should simply remove the charset attribute from the <link> element in your HTML file.

Before:

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

After:

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

2. Set the charset using HTTP Headers:

To ensure that the correct character encoding is used, you should configure your web server to send the appropriate Content-Type header with the CSS file.

  • For Apache: You can modify the .htaccess file or the server configuration file.

    <FilesMatch "\.css$">
        AddCharset UTF-8 .css
    </FilesMatch>
  • For Nginx: You can add the following directive to your server block or location block:

    location ~* \.css$ {
        charset utf-8;
    }
  • For Express.js (Node.js): You can set headers in your response:

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

Learn more:

Related W3C validator issues