简体   繁体   English

:first-child伪不适用

[英]:first-child pseudo is not applying

I want to apply some styles to first-child of <dl> . 我想对<dl>第一个孩子应用某些样式。 I meant to say that should be applied to first <dd> . 我的意思是说应该将其应用于first <dd> But it works if i remove <dt></dt> in the <dl> . 但是,如果我删除了<dl>中的<dl> <dt></dt> ,它将起作用。

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

CSS: CSS:

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

Use first-of-type instead: 改用first-of-type

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

Demo Fiddle 演示小提琴


The reasoning... 推理...

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-of-type CSS伪类表示其父元素的子代列表中其类型第一个同级

-vs- 航班吗

first-child

The :first-child CSS pseudo-class represents any element that is the first child element of its parent. :first-child CSS伪类表示任何作为其父级的第一个子元素的元素

In your case, first-child wont work, because the first dd is not the first child of dl 在您的情况下, first-child不会工作,因为第一个dd不是dl第一个孩子

<div id="text_translate"><p>我在 MDN 页面上通过了使用选择器的测试技能,我遇到了 CSS 规则编写的情况:带有 class.container 的 div 元素的第一个子元素应用于层次结构中的表元素,但不是其第一个子元素div 元素。 为什么会这样? 我是否缺少表格元素的一些一般规则? 正如我所料,只有第一个 p 元素应该受此规则影响。</p><p> 我附上了 MDN 上使用的 html 代码和导致此问题的 CSS 解决方案。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .container:first-child {font-size: 150%}.container:first-child::first-line {color: red}</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="container"&gt; &lt;p&gt;Veggies es &lt;a href="http://example.com"&gt;bonus vobis&lt;/a&gt;, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo melon azuki bean garlic.&lt;/p&gt; &lt;p&gt;Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.&lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Fruits&lt;/th&gt; &lt;th&gt;Vegetables&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Apple&lt;/td&gt; &lt;td&gt;Potato&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Orange&lt;/td&gt; &lt;td&gt;Carrot&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Tomato&lt;/td&gt; &lt;td&gt;Parsnip&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Kiwi&lt;/td&gt; &lt;td&gt;Onion&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Banana&lt;/td&gt; &lt;td&gt;Beet&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt;</pre></div></div><p></p></div>受:first-child 伪类影响,尽管它不是规则设置的元素的第一个孩子<table> </table> - <table> is affected by :first-child pseudo-class although it is not first-child of element the rule is set up for

暂无
暂无

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

相关问题 CSS:第一个孩子的伪元素 - CSS :first-child pseudo-element 第一子级伪类不适用于<a>标签</a> - first-child pseudo class not working for <a> tag CSS3的第一个和最后一个伪属性 - First-child and last-child pseudo property with CSS3 <div id="text_translate"><p>我在 MDN 页面上通过了使用选择器的测试技能,我遇到了 CSS 规则编写的情况:带有 class.container 的 div 元素的第一个子元素应用于层次结构中的表元素,但不是其第一个子元素div 元素。 为什么会这样? 我是否缺少表格元素的一些一般规则? 正如我所料,只有第一个 p 元素应该受此规则影响。</p><p> 我附上了 MDN 上使用的 html 代码和导致此问题的 CSS 解决方案。 </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .container:first-child {font-size: 150%}.container:first-child::first-line {color: red}</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;div class="container"&gt; &lt;p&gt;Veggies es &lt;a href="http://example.com"&gt;bonus vobis&lt;/a&gt;, proinde vos postulo essum magis kohlrabi welsh onion daikon amaranth tatsoi tomatillo melon azuki bean garlic.&lt;/p&gt; &lt;p&gt;Gumbo beet greens corn soko endive gumbo gourd. Parsley shallot courgette tatsoi pea sprouts fava bean collard greens dandelion okra wakame tomato. Dandelion cucumber earthnut pea peanut soko zucchini.&lt;/p&gt; &lt;table&gt; &lt;tr&gt; &lt;th&gt;Fruits&lt;/th&gt; &lt;th&gt;Vegetables&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Apple&lt;/td&gt; &lt;td&gt;Potato&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Orange&lt;/td&gt; &lt;td&gt;Carrot&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Tomato&lt;/td&gt; &lt;td&gt;Parsnip&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Kiwi&lt;/td&gt; &lt;td&gt;Onion&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Banana&lt;/td&gt; &lt;td&gt;Beet&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt;</pre></div></div><p></p></div>受:first-child 伪类影响,尽管它不是规则设置的元素的第一个孩子<table> </table> - <table> is affected by :first-child pseudo-class although it is not first-child of element the rule is set up for CSS 2.1中的第一个孩子伪类 - First-child pseudo-class in css 2.1 在一节内的几篇文章中使用伪元素第一个孩子 - Use pseudo element first-child in several articles inside a section 使用CSS在:first-child伪选择器“内部”选择一个元素 - select an element “inside” the :first-child pseudo selector using CSS 如何使用CSS:first-child伪元素 - How to use CSS :first-child pseudo-element 具有无序列表的CSS第一子级伪类(Bootstrap) - CSS first-child pseudo class with unordered list (Bootstrap) 尝试使用第一个子级伪类选择器 - Trying to use first-child pseudo class selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM