css - 自己+同款是什么意思?

标签 css css-selectors

我知道,这没有意义。看看这里,你就会明白我的意思了

http://jsfiddle.net/thirtydot/q6Hj8

它所在的部分

.yourDivClass + .yourDivClass {....

好像是消除了Div结尾的封闭样式。这是实现该效果的常用方法吗?

最佳答案

每节5.7: Adjacent sibling selectors W3C CSS 规范:(为清楚起见,格式化)

Adjacent sibling selectors have the following syntax: E1 + E2, where E2 is the subject of the selector. The selector matches if

  1. E1 and E2 share the same parent in the document tree and
  2. E1 immediately precedes E2, ignoring non-element nodes (such as text nodes and comments).

Thus, the following rule states that when a P element immediately follows a MATH element, it should not be indented:

math + p { text-indent: 0 }

它基本上意味着当 second 元素在相同的上下文中并且直接跟随代码中的 first 元素(中间没有任何其他元素)时,以下属性将被添加到第二个元素!

在您的 jsfiddle 示例中,这意味着“margin-left”将应用于整个/两个类/es(因为两个选择器在您的示例中相同)仅当 该元素在您的代码中跟随其自身,它确实如此。尝试

<div class="Yourclass">
<div class="cloneofyourclass">
<div class="Yourclass"> 

并且不会应用您的示例中的其他 margin-left

请注意,Internet Explorer 6 不会接受任何这些选择器!不过 IE7 和更高版本就可以了!

关于css - 自己+同款是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8316583/

相关文章:

CSS 选择器 - 表中的第一个 h2

css - 作为 CSS 选择器,.class-name 是什么意思?

html - 如何让我的关键帧动画顺利开始?

html - 基于 nth-child(n) 选择器的替代颜色

javascript - Ajax 在没有用户交互的情况下以 1 秒的间隔显示文件

jquery - 改变滚动的 BG 图像

python - 如何在 Glassdoor 上使用 Selenium 和 Python 访问 iframe

php - 简单的 html dom : how get a tag without certain attribute

javascript - 如何使用 jQuery 将多个图像的切片与另一个图像切换

html - 如何在 asp.net 中正确地在图像上实现白框