Html css 所有 div 与直接子 div

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4529063/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 05:50:45  来源:igfitidea点击:

css all divs vs direct child divs

htmlcsscss-selectorschildren

提问by Naor

I have this structure:

我有这个结构:

<div class="Root">
    <div>ddddddd</div>
    <div>
        <div>pppppppppp</div>
        <div>pppppppppp</div>
    </div>
    <div>ddddddd</div>
<div>

I want to put borders on the divs that contain ddddddd, and I want to set the text color on alldivs to green.

我想在div包含 s 的 s上放置边框ddddddd,并且我想将所有divs上的文本颜色设置为绿色。

There are two rules:

有两个规则:

  1. I can't add classattributes.
  2. I have to write selectors that start with .Root.
  1. 我无法添加class属性。
  2. 我必须编写以.Root.

Any ideas?

有任何想法吗?

回答by Naor

Actually I was searching this:

其实我是在找这个:

Selects the divs that are direct children of Root:

选择作为 Root 的直接子级的 div:

.Root > div {
    border: 1px solid red;
}

Selects all the divs under Root:

选择 Root 下的所有 div:

.Root div {
    color:green;
}

回答by karim79

Something like this?

像这样的东西?

.Root > :first-child, .Root > :last-child { border: 1px solid red }
.Root { color: green; }

Demo: http://jsfiddle.net/karim79/N5qFu/1/

演示:http: //jsfiddle.net/karim79/N5qFu/1/

I would advise you to go through this: http://www.w3.org/TR/css3-selectors/

我建议你通过这个:http: //www.w3.org/TR/css3-selectors/

回答by 422

.root {
border: 1px solid green;
}

Why are you not declaring class /id for other divs?

为什么不为其他 div 声明类 /id?