In your example, you are trying to select the second child element of h1 , but this element has no children, and therefore it fails. You must select the second child of the parent element h1
section article :nth-child(2):before
This has the advantage that you donβt put any tag name there, so it will work even if one day you change h1 to h2 , for example. This last selector can also be rewritten to
section article :first-child:after
This is not the same , but you can also add the generated content after the element (and in your case it will work fine and work the same way).
Or, if you want to match something with h1 , you need to target your next brother using the sibling selector
section article h1 + *:before
This selector will select the first element (regardless of its type), which will appear immediately after h1 .
Or by inserting generated content after an element, you can use this
section article h1:after { background-color: white !important; content: ''; height: 10px; display: block; }
Which, in my opinion, is the easiest way to do
Alessandro Vendruscolo
source share