父元素具有特定类的子元素的CSS选择器 [英] CSS selector for a child element whose parent element has a certain class

查看:498
本文介绍了父元素具有特定类的子元素的CSS选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在样式表文件中为div内的strong元素指定CSS,如下面的代码所示.该strong元素位于其父类div中,该父类的类为commandBar.

I need to specify CSS in a style sheet file for the strong element inside a div as in code below. This strong element is inside a parent div whose class is commandBar.

<strong>Outside Div</strong>
<div class='commandBar'>
    <button class='dBtn'>Delete</button>
    <strong>My Name</strong>
    <button class='aBtn'>Add</button>
</div>

推荐答案

要选择作为commandBar类元素的后代的strong元素,请使用

To select strong elements that are descendants of an element with class commandBar, use the descendant combinator along with a class selector:

.commandBar strong

为了仅选择直接子级strong元素,请使用子级组合器> :

In order to only select direct children strong elements, use the child combinator, >:

.commandBar > strong

根据您的标记,您可能还需要指定具有类.commandBar的元素类型(在本例中为div):

Depending on your markup, you may also want to specify the element type that has the class .commandBar (in this case, div):

div.commandBar strong

这篇关于父元素具有特定类的子元素的CSS选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆