如何分层特异性
在 http://www.smashingmagazine.com 的帖子中/2009/08/17/taming-advanced-css-selectors/
在定义“特异性”规则的上下文中指出:
例如,如果您想更改 所有div的背景颜色 您博客上的帖子元素, 您可以使用属性选择器 其目标是每个类的 div 属性以“post-”开头:
div[class*="post"] { 背景颜色:#333; }
这将匹配其 class 属性在任何位置包含单词“posts”的所有 div 元素。
...
我的问题是用以下内容扩展上面的示例:
** ...更改博客上帖子的所有 div 元素的背景颜色除了由匿名用户发布的帖子看起来像... **
[看起来像什么?]
谢谢
Within the posting at http://www.smashingmagazine.com/2009/08/17/taming-advanced-css-selectors/
in the context of defining the rules of 'specificity' is stated:
For example, if you want to change the
background color of all the div
elements that are posts on your blog,
you can use the an attribute selector
that targets every div whose class
attribute starts with “post-”:div[class*="post"] { background-color: #333; }
This will match all the div elements whose class attribute contains the words “posts”, in any position.
...
My question goes to extending the above example with:
** ...change the background color of all the div elements that are posts on your blog except posts by anon users looks like...
**
[whatzitlooklike?]
thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,这不是在 CSS 中选择具有特定类的元素的正确方法。这就是
.
的用途:我只是简单地浏览了那篇文章,但我不知道为什么它会推荐其他语法。除了需要更多的输入之外,它在 Internet Explorer 6 中不起作用,并且会让任何有经验的 CSS 作者感到完全困惑。
至于您的实际问题,您如何设置匿名用户的帖子样式完全取决于它们的标记方式。例如,如果这样的帖子在 HTML 中看起来像这样:
那么您可以简单地添加另一个样式规则,如下所示:
重要的问题是:这些样式规则以什么顺序出现在样式表中?如果
.anonymous
最后出现,那么无论它适用于何处,它都会优先。因此,即使.post
和.anonymous
规则均适用于该,
.anonymous
> 颜色优先,因为它出现得较晚。 (当然,如果您将这些规则按其他顺序排列,则相反。)First, that's not the correct way to select elements with a particular class in CSS. That's what the
.
is for:I've glanced through that article only briefly, but I have no idea why it would recommend the other syntax. Besides the fact that it requires more typing, it won't work in Internet Explorer 6, and it will confuse entirely any experienced CSS author.
As to your actual question, how you'd style posts by anonymous users depends entirely on how they're marked up. For example, if such a post looks like this in your HTML:
Then you could simply add another style rule like so:
The important question is: in what order do those style rules appear within your stylesheet? If
.anonymous
appears last, then it will take precedence wherever it applies. So, even though both the.post
and.anonymous
rules apply to that<div>
, the.anonymous
color takes precedence because it comes later. (Of course, the reverse will be true if you put those rules in the other order.)