父元素具有特定类的子元素的CSS选择器 [英] CSS selector for a child element whose parent element has a certain class
问题描述
我需要在样式表文件中为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屋!