CSS:双重声明有什么作用?

发布于 2024-12-15 14:25:48 字数 333 浏览 4 评论 0原文

我正在为 WordPress 构建一个自定义主题,并在默认的 2010 style.css 文件中看到了这一点:

#wrapper {
  margin: 0 auto;
  width: 940px;
}
#wrapper {
  background: pink;
  margin-top: 20px;
  padding: 0 20px;
}

现在这是默认代码(pink 除外)。当我尝试折叠它时,这似乎合乎逻辑,但它却产生了很大的不同。

我不明白的是为什么你要像这样声明同一个元素两次?我以前从未见过...

WR!

i'm building a custom theme for wordpress and saw this in the default 2010 style.css file:

#wrapper {
  margin: 0 auto;
  width: 940px;
}
#wrapper {
  background: pink;
  margin-top: 20px;
  padding: 0 20px;
}

now this is the default code (except the pink). when i try and collapse it, which seems logical, it makes quite a difference.

what i can't figure out is WHY you'd declare the same element twice like that? i've never seen that before...

WR!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[旋木] 2024-12-22 14:25:48

当您想要在多个元素上应用共享属性时,它被证明是有用的。另一个有用的应用程序是添加来自多个源的样式表
示例:

#head, #foot {
    height: 100px;
}
#foot { /*Another foot*/
    color: red;
}

第二个示例:来自多个源的 CSS:

/* External stylesheet: common.css */
body {
    background: yellow;
}
/* Inline stylesheet, overrides external stylehseet */
body {
    background: pink;
}

当两个属性具有相同的特异性时,将应用最后声明的属性。

It proves useful when you want to apply shared properties at multiple elements. Another useful application is adding stylesheets from multiple sources
Example:

#head, #foot {
    height: 100px;
}
#foot { /*Another foot*/
    color: red;
}

Second example: CSS from multiple sources:

/* External stylesheet: common.css */
body {
    background: yellow;
}
/* Inline stylesheet, overrides external stylehseet */
body {
    background: pink;
}

When two properties have the same specificity, the lastly declared property will be applied.

但可醉心 2024-12-22 14:25:48

它只是覆盖之前声明的属性。

wrapper 现在将具有 margin:20px auto 0 auto (右上左下)。

It just overrides previously declared properties.

wrapper will now have margin:20px auto 0 auto (Top Right Bottom Left).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文