html - :first-child pseudo is not applying

标签 html css

我想对 <dl> 的第一个 child 应用一些样式.我的意思是说应该先应用于 <dd> .但如果我删除 <dt></dt> 它会起作用在<dl> .

<dl>
    <dt>Test</dt>
    <dd>One</dd>
    <dd>Two</dd>
    <dd>Three</dd>
    <dd>Four</dd>
    <dd>Five</dd>
</dl>

CSS:

dl dd:first-child{border:1px solid red}

最佳答案

使用 first-of-type相反:

dl dd:first-of-type{border:1px solid red}

Demo Fiddle


推理...

first-of-type

The :first-of-type CSS pseudo-class represents the first sibling of its type in the list of children of its parent element.

-对比-

first-child

The :first-child CSS pseudo-class represents any element that is the first child element of its parent.

在你的例子中,first-child不会工作,因为第一个 dd不是 dl第一个 child

关于html - :first-child pseudo is not applying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23804354/

相关文章:

html - 悬停时将图像更改为文本

html - 为什么我的按钮在页面上向下移动?

javascript - jQuery 图像交换 if else

html - CSS 布局 101 - 在结构化布局中感到困惑

css - 如何使用 CSS3 制作这种类型的边框?

html - 确保遵守表格单元格宽度

html - 如何使用CSS垂直对齐内联 block div而没有垂直空格?

css - ionic 如何将 ionic 内容对齐到页面底部

jquery - 将 jQuery 事件处理程序应用于 AJAX 内容

css - 如何删除汉堡菜单右侧的空白区域