::before 和 ::after 伪元素与具有多个 <p> 元素选择器的伪类一起使用

::before and ::After pseudo elements used with pseudo classes with several <p> elements selectors

提问人:Yves Bourdeau 提问时间:10/23/2023 最后编辑:Yves Bourdeau 更新时间:10/23/2023 访问量:20

问:

在此示例中,如您所见,“p:first-child::before”不起作用。有没有办法在不使用 .classes 的情况下拥有两个 p 元素?我只想在第一个子元素、最后一个子元素或第 n 个子元素之前和之后使用伪元素,这样我就可以在第一个 p 元素之前添加内容,并在第二个元素之后添加内容

元素。这是我正在使用的代码和我想要的结果。

我无法让 ::before 伪元素在第二个 p 元素上使用 ::after 的同时显示“Hi”内容。是我做错了,还是不可能将这些伪元素和类元素放在一起? 先谢谢你。

<html>
    <head>
        <style>
        p:first-child::before {
            content: "Hi, ";
        }

        p:last-child::after {
            content: " and I like it";
        }
        </style>
    </head>
    <body>

        <h1>Demo of the ::before selector</h1>

        <p>My name is Donald</p>
        <p>I live in Ducksburg</p>

    </body>
</html>

我想要的结果是这样的:

嗨,我叫唐纳德
,我住在达克堡,我喜欢它

我得到的结果是这样的:

我的名字是唐纳德
,我住在达克堡,我喜欢它

css-selectors 元素伪

评论

1赞 Daniel A. White 10/23/2023
用途:first-of-type

答: 暂无答案