在css文件中,使用eclipse ide,添加头:

@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");

应该让eclipse检查元素是否有错误吗?(因为它没有这么做)。
如果没有,加不加标题有什么区别?

最佳答案

css中的@namespace模块用于创建仅应用于特定命名空间的样式。它对于将css样式应用于xml文档特别有用。您还可以将其与xhtml和html5一起使用,以便仅将样式应用于具有特定xml名称空间的文档和元素(由xmlns属性定义,通常在html标记中)。
例如,查看以下XHTML文档:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>@namespace examples</title>

    <!-- This stylesheet defines an namespace prefix called "xhtml", and uses it to style all p elements in that namespace -->
    <style type="text/css">
        @namespace xhtml "http://www.w3.org/1999/xhtml";
        xhtml|p
        {
            color: Red;
        }
    </style>

    <!-- This stylesheet defines style that apply to an imaginary "superhtml" namespace. It shouldn't work for xhtml elements -->
    <style type="text/css">
        @namespace "http://www.w3.org/20X6/superxhtml";
        p
        {
            font-style: italic;
        }
    </style>

    <!-- This stylesheet uses a namespace URL with no namespace prefix, so all its styles apply to that namespace. -->
    <style type="text/css">
        @namespace xhtml "http://www.w3.org/1999/xhtml";
        p
        {
            text-decoration: underline;
        }
    </style>

    <!-- This stylesheet uses no namespace declaration, it applies to any document that includes it. -->
    <style type="text/css">
        p
        {
            font-size: 20pt;
        }
    </style>

</head>
<body>

    <p>If this text is red, underlined, and 20pt, then you're using the http://www.w3.org/1999/xhtml namespace.</p>

</body>
</html>

在firefox 4中加载,如下所示:
注意打开的html标记:<html xmlns="http://www.w3.org/1999/xhtml" >。它有一个xmlns属性。因此,与该命名空间匹配的css规则在此文档中起作用。文本为红色、下划线和20磅。但是请注意,文本不是斜体。为什么?应用于不同命名空间的斜体段落的样式规则:
<!-- This stylesheet defines style that apply to an imaginary "superhtml" namespace. It shouldn't work for xhtml elements -->
<style type="text/css">
    @namespace "http://www.w3.org/20X6/superxhtml";
    p
    {
        font-style: italic;
    }
</style>

由于html标记没有指向http://www.w3.org/20X6/superxhtml处的组合命名空间的xmlns属性,因此忽略了此样式规则。
现在,您可能认为将html标记中的xmlns更改为值“http://www.w3.org/20X6/superxhtml”将使该段落变为黑色和斜体。然而,似乎所有支持@namespacecss声明的浏览器目前都假设所有xhtml/html文档都在http://www.w3.org/1999/xhtml命名空间中,并相应地设置它们的样式,即使您试图更改它。
因此,@namespace可能看起来不太有用,但如果您要在多个xml文档之间或xhtml文档和xml文档之间共享样式表,并且希望每个文档都有不同的样式,则它很有用。
为了演示,我将创建3个文件:
首先,namespacecss.css:
@namespace xhtml "http://www.w3.org/1999/xhtml";
@namespace article "http://www.example.com/namespaces/article";

xhtml|p
{
    color: Red;
}

article|p
{
    color: Blue;
}

p
{
    font-size: 20pt;
}

接下来,namespacetest.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>@namespace examples</title>
    <link type="text/css" href="namespacecss.css" rel="Stylesheet" />
</head>
<body>

    <p>If this text is red, then you're using the http://www.w3.org/1999/xhtml namespace.</p>

</body>
</html>

最后,一个XML文件namespacetest.xml:
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="namespacecss.css"?>
<document xmlns="http://www.example.com/namespaces/article">
  <p>This should be blue</p>
</document>

现在,将最后两个文件加载到firefox 4中。namespacetest.html如下所示:
http://i56.tinypic.com/2zeca44.png
namespacetest.xml如下所示:
css.css中的第一个样式规则只适用于xhtml,因此xhtml段落是红色的。第二个样式规则只适用于我们的自定义名称空间“article”,因此xml文件中的段落是蓝色的。第三条规则适用于所有名称空间,因此两个示例中的文本都是20pt。
进一步阅读:
CSS3 Namespaces Module - W3C Candidate Recommendation
@namespace reference at sitepoint.com
Blog post about CSS namespaces by Anne van Kesteren
谢谢你问这个问题!我在回答的时候学到了很多。

07-24 09:37
查看更多