标签“div.img img”是什么意思?

时间:2011-06-20 00:17:51

标签: css css-selectors

我理解创建类标记的一般想法,例如.center或p.center,但是做“p.center p”之类的操作是什么意思?我在用css创建图库时看到了这样的例子。 div.img img是什么意思?谢谢你!

<html>
<head>
<style type="text/css">
div.img
{
  margin: 2px;
}   
div.img img
{
  display: inline;
  margin: 3px;
  border: 1px solid #ffffff;
}
div.img a:hover img {border: 1px solid #0000ff;}

</style>
</head>
<body>
<div class="img">
  <a target="_blank" href="klematis4_big.htm"><img src="klematis4_small.jpg" alt="Klematis" width="110" height="90" /></a>
</div>
</body>
</html>

3 个答案:

答案 0 :(得分:14)

到目前为止给出的所有答案都是正确的,但也许也解释了它不会有所帮助。

虽然div.img img { }会影响img元素中包含的<div class="img">元素,但以下是一些不会影响的元素:

<body>

  <img src="klematis4_small.jpg" alt="Klematis"><br>
  This image is not contained in a div, therefore it cannot possibly be
  contained in a div of the class "img" and is therefore unaffected.

  <div>
     <img src="klematis4_small.jpg" alt="Klematis"><br>
     This img element is in a div, but the div is not of the class "img",
     so the img element is unaffected.
  </div>

  <div id="img">
     <img src="klematis4_small.jpg" alt="Klematis"><br>
     This img is contained within a div that has an ID of "img", but it is
     also not of the class "img" and is therefore unaffected.
  </div>

</body>

答案 1 :(得分:9)

这意味着

  

选择任何img元素
  它是具有类div的{​​{1}}元素的后代。

img类没有特殊含义。在这个例子中,它看起来有点令人困惑。

答案 2 :(得分:3)

div.img img选择器将使用类“img”在div中设置img标签的样式。例如:

<div class="img">
    <img src="foo.jpg" alt="" />
</div>

简而言之,只有具有类img的div标签中包含的img标签才会被该选择器设置样式。