CSS xml:lang 的选择器

CSS Selector for xml:lang

我想为每种语言使用不同的 CSS 选择器。我有一个 HTML 文档,例如:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es">
     ...
</html>

我有不同的语言,我想根据语言更改样式。

我已经试过了html:lang(es)但是没用。

只有当 XHTML 被解释为 XML 时,xml:lang 属性才会有效。当它被解析为 HTML 时,您需要使用 lang 属性。

您可以通过使用 XML 文件扩展名在本地保存您的 XHTML 文档然后在浏览器中加载来测试它,当前的 CSS 选择器应该可以工作。

如果您可以控制 XHTML 的生成,最简单的做法是同时使用这两种语言属性,以确保在读取时正确评估它XML 或 HTML.

这在 W3C 国际 Declaring language in HTML 页面中有描述:

When serving XHTML 1.x or polyglot pages as text/html, use both the lang attribute and the xml:lang attribute together every time you want to set the language. The xml:lang attribute is the standard way to identify language information in XML. Ensure that the values for both attributes are identical.

<html lang="fr" xml:lang="fr" xmlns="http://www.w3.org/1999/xhtml">

The xml:lang attribute is not actually useful for handling the file as HTML, but takes over from the lang attribute any time you process or serve the document as XML. The lang attribute is allowed by the syntax of XHTML, and may also be recognized by browsers. When using other XML parsers, however (such as the lang() function in XSLT) you can't rely on the lang attribute being recognized.

我建议同时更新 html 中的语言属性。您也可以使用 CSS select 这个元素。 xml 是一个可以 select 和 CSS 的属性。

html[xml:lang="es"]{ //your style }

您还可以将 css 更改为 lang 属性

html[lang="es"]{ //your style } 

html[xml\:lang="es"] body{ background: red; } 
html[xml\:lang="en"] body{ background: green; } 
<html xml:lang="es" lang="es">
<head>
</head>
<body>
</body>