简体   繁体   English

在CSS中设置div样式

[英]styling a div in css

IF there is a div say 如果有一个div说

 <div class="ref"><contents goes here</div>
  1. How to specify in the css as body of the div .Meaning 如何在css中指定div的主体

      <style> .ref body { /* ref body contents goes here*/ } </style> 

    Can anything like the above be done? 可以做以上的事情吗?

  2. If any one can explain the below also 如果有人可以解释以下内容

      <style> .a > .bb { } </style> 

What does the above mean? 以上是什么意思?

Thanks.. 谢谢..

body is a specific HTML element. body是特定的HTML元素。 In order to style the contents of a div with a class of ref , your CSS selector is simply .ref , not .ref body . 为了使用ref类来样式化div的内容,您的CSS选择器就是.ref而不是.ref body

The > in CSS is called the child selector . CSS中的>称为子选择器 It means that the element on the right must be a child element of the one on the left. 这意味着右侧的元素必须是左侧元素的子元素。 So: 所以:

.a > .bb would match the bb div here: .a > .bb与此处的bb div匹配:

<div class="a"><div class="bb"></div></div>

but not here: 但不在这里:

<div class="bb"></div>

For the first one 对于第一个

CSS is only for styling you elements and not for showing HTML. CSS仅用于样式化元素,而不用于显示HTML。

For the second on 对于第二个

styles all elements with class name 'bb' which are children[not descendants] of elements with class 'a'. 设置所有类名称为“ bb”的元素的样式,这些元素是类“ a”的元素的子元素(不是子元素)。 See child selector 查看子选择器

Ex 1 例1

<div class="a">
    <div class="bb"></div>
</div>

Ex 2 例2

<div class="a">
    <div class="bb"></div>
    <div>
        <div class="bb"></div>
    </div>  
</div>

Child selector will only select the first div with class 'bb' and not the inner div with class 'bb'. 子选择器将仅选择类别为“ bb”的第一个div,而不选择类别为“ bb”的内部div。

For the first one, it's not totally clear what you mean. 对于第一个,您的意思还不清楚。 Just using .ref { /* CSS here */ } will target that div element. 仅使用.ref { /* CSS here */ }定位该div元素。 If you mean adding content via CSS, it's only possible using the :before and :after pseudo slectors: 如果您要通过CSS添加内容,则只能使用:before:after伪选择子:

.ref:after {
  content: "Some text";
}

Note: you can't put HTML in there, only text. 注意:您不能在其中放置HTML,只能在文本中放置HTML。


For the second one, it looks for all elements with a class of bb that are direct children of class a . 对于第二个,它看起来与一类的所有元素bb是类的直接孩子a For example, it will match the p in this code 例如,它将匹配此代码中的p

<div class="a">
  <div>something</div>
  <p class="bb">class bb</p>
</div>

But it won't match this: 但这不符合以下条件:

<div class="a">
  <div>
      <p class="bb">class bb</p>
  </div>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM