CSS

:lang

:lang

该:langCSS伪类基于所选定的语言匹配元素。在HTML中,语言是由lang属性,<meta>元素的组合来确定的,有时也可能由来自协议的信息(如HTTP首部)确定。对于其他文档类型,可能有其他文档方法来确定语言。

/* Matches paragraphs, but only if they are specified as being en (English language) */ p:lang(en) { quotes: '\201C' '\201D' '\2018' '\2019'; }

在HTML 4.0规范中指定了可接受的language-code字符串。

语法

形式语法

:lang( <language-code> )

参数

语言编码language-code,即要为其选择的字符串。

实例

在这个例子中,:lang伪类用于使用子选择器来匹配一个quote元素的父元素。但请注意,这并不是唯一的方法,甚至有时也不是正确的方法(这取决于文档的类型)。请注意,Unicode值用于指定特殊的引号字符。

HTML

:lang(en) > q { quotes: '\201C' '\201D' '\2018' '\2019'; } :lang(fr) > q { quotes: '« ' ' »'; } :lang(de) > q { quotes: '»' '«' '\2039' '\203A'; }

CSS

<div lang="en"><q>This English quote has a <q>nested</q> quote inside.</q></div> <div lang="fr"><q>This French quote has a <q>nested</q> quote inside.</q></div> <div lang="de"><q>This German quote has a <q>nested</q> quote inside.</q></div>

结果

规范

SpecificationStatusComment
Selectors Level 4The definition of ':lang()' in that specification.Working DraftNo change.
Selectors Level 3The definition of ':lang()' in that specification.RecommendationNo significant change.
CSS Level 2 (Revision 1)The definition of ':lang()' in that specification.RecommendationInitial definition.

浏览器兼容性

FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support1.0(Yes)1.0 (1.7 or earlier)8.08.03.1

FeatureAndroidEdgeFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support1.0(Yes)1.0 (1)8.08.03.1