charset declaration in .css file

6

Sometimes I come across some examples of file .css with the declaration of charset logo in the first line:

@charset "utf-8";

Until my code editor (Dreamweaver CC 2015), when I create a new CSS file, it already places this line by default at the beginning of the document:

But a CSS file usually only contains styles and properties (except in cases of pseudo-elements ::before and ::after where you can place texts in the content property.)

I had a question about this: is this statement really necessary? If yes or no , what is the reason?

    
asked by anonymous 21.11.2018 / 00:30

1 answer

2

W3C Words

  

You should always use UTF-8 as the character encoding of your style sheets and your HTML pages, and declare that encoding in your HTML. If you do that, there is no need to declare the encoding of your style sheet .

PORTUGUESE: "You should always use UTF-8 as the character encoding of your style sheets and your HTML pages and declare this encoding in your HTML. If you do, there is no need to declare the encoding of your style sheet. "

Source: link

So briefly we can say that if you have already declared Charset in HTML that will index this style sheet you do not necessarily have to declare Charset within .CSS

To declare Charset in HTML you can use:

<meta charset="utf-8" />

Or

<meta http-equiv="Content-Type" content="text/html; charset=IANAcharset">

Take these points into consideration as well:

  • This <meta> element is only part of the algorithm to determine the character set of a page that browsers apply. The HTTP Content-Type header and any elements of BOM take precedence over this element.
  • It is a good practice, and highly recommended, to define the character set using this attribute.
  • A number of cross-scripting techniques could be detrimental to the page user, such as the cross-scripting fallback UTF-7 technique. Always setting this goal will protect against these risks.

So setting Charset in HTML is a security issue and is considered good practice and highly recommended.

Source: link

See what Mozilla says:

  

The @charset rule specifies the character encoding used in the style sheet.

Source: link

Note!

It's not enough simply to put @charset "utf-8"; at the top of the style sheet - you also need to save your style sheet in UTF-8 character encoding.

Important: Because the HTTP header has a higher precedence than the in-document @charset statement, you should always consider whether character encoding is already declared in the HTTP header. If you are, @charset must be set to declare the same encoding and will only take effect when the style sheet is read in a context where there are no HTTP headers (for example, a local drive).

    
21.11.2018 / 13:00