简体   繁体   English

CSS:第一个孩子的伪元素

[英]CSS :first-child pseudo-element

Why is my code not working as intended? 为什么我的代码无法按预期工作? background-color of the first article must be green. 第一篇文章的background-color必须为绿色。 How I can do it without classes or id? 没有类或ID的情况下该怎么办? PS I'm sorry but my post needs more text. PS很抱歉,我的帖子需要更多文字。 I'm sorry but my post needs more text. 抱歉,我的帖子需要更多文字。

    <!DOCTYPE html>
<html>
    <head>  
        <meta charset="utf-8">
        <title>stack</title>

        <link rel="stylesheet" href="css/style_new.css">
    </head>

    <body> 
        <section>
            <h1>This is H1 header</h1>

            <article>
                <p>
                    Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet
                    Lorem ipsum dolor sit amet Lorem ipsum dolor sit 

                </p>
            </article>

            <article>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
                    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 

                </p>
            </article>

            <article>
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
                    tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 

                </p>
            </article>
        </section>
    </body> 
</html> 

CSS code: CSS代码:

     section article:first-child {
    background-color: green;
}

    section article:last-child {
        background-color: red;
    }

Actually first child of section tag is h1 not article. 实际上,区段标记的第一个子代不是h1文章。 So use below code instead, 因此,请改用以下代码,

section article:first-of-type {
  background-color: green;
}

article is not the first-child of section - its h1 文章不是本节的第一个孩子-它的h1

the first/last child selector does only target if article is the first or last child of its container. 仅当article是其容器的第一个或最后一个孩子时,第一个/最后一个孩子选择器才定位。

possible css: 可能的CSS:

section article{
  background-color:green;
}
section article + article{
  background-color:transparent;
}
section article:last-child {
  background-color: red;
}

see http://jsfiddle.net/rn4wgho2/ 参见http://jsfiddle.net/rn4wgho2/

 ![enter image description here][1]

h1 required to remove the plug out of the section 将插头从该部分拔出所需的h1

This is H1 header 这是H1标头

        <article>
            <p>
                Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet
                Lorem ipsum dolor sit amet Lorem ipsum dolor sit 

            </p>
        </article>

        <article>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 

            </p>
        </article>

        <article>
            <p>
                Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod 
                tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, 

            </p>
        </article>
    </section>

<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:first-child伪元素 - How to use CSS :first-child pseudo-element :first-child和:first-letter令人费解的伪元素语法 - Baffling pseudo-element syntax with :first-child and :first-letter 使用CSS在:first-child伪选择器“内部”选择一个元素 - select an element “inside” the :first-child pseudo selector using CSS CSS3的第一个和最后一个伪属性 - First-child and last-child pseudo property with CSS3 :first-child伪不适用 - :first-child pseudo is not applying <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` - CSS `:first-child` on specific element 具有无序列表的CSS第一子级伪类(Bootstrap) - CSS first-child pseudo class with unordered list (Bootstrap)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM