简体   繁体   English

具有无序列表的CSS第一子级伪类(Bootstrap)

[英]CSS first-child pseudo class with unordered list (Bootstrap)

I am trying to render a background color on the list item that is the first child of the unordered list. 我正在尝试在作为无序列表的第一个孩子的列表项上呈现背景色。

HTML structure is as follows HTML结构如下

<div class="nav-collapse">
    <ul class="nav">
      <li><a href="#">test 1</a></li>
      <li><a href="#">test 2</a></li>
      <li><a href="#">test 3</a></li>
    </ul>
</div> 

and to apply the background color on the first child element I did 并在我做的第一个子元素上应用背景色

.nav-collapse > .nav:first-child {
    background-color: orange;
}

It renders orange background to all list items. 它将橙色背景呈现给所有列表项。 I've played with slight variations but it doesn't make difference. 我玩的时候有一些细微的变化,但这并没有什么不同。

.nav-collapse > ul.nav:first-child
.nav-collapse > ul:first-child

Here is the Demo 这是演示

Use the following: 使用以下内容:

.nav > li:first-child {
    background-color: orange;
}

Working jsFiddle here 在这里工作jsFiddle

You were trying to style the first .nav item - which there is only one of. 您试图设置第一个.nav项目的样式-只有一个。 Simply change it to style the first li that is a direct child of .nav . 只需将其更改为.nav的直接子.nav li样式即可。

If you want to be more specific use: 如果您想更具体地使用:

.nav-collapse > .nav > li:first-child {
    background-color: orange;
}

You can do it in many ways, try this too 您可以通过多种方式进行操作,也可以尝试

ul.nav > li:first-child {
    background-color: orange;
}

<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 2.1中的第一个孩子伪类 - First-child pseudo-class in css 2.1 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 :first-child伪不适用 - :first-child pseudo is not applying 使用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 <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删除或重命名第一个孩子的课程 - CSS Remove or rename a class on first-child 尝试使用第一个子级伪类选择器 - Trying to use first-child pseudo class selector
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM