Select first occurring element after another element

后端 未结 5 1432
时光取名叫无心
时光取名叫无心 2020-12-01 03:33

I\'ve got the following HTML code on a page:

Some text

Some more text!

In my .css I\

相关标签:
5条回答
  • 2020-12-01 03:47

    Just hit on this when trying to solve this type of thing my self.

    I did a selector that deals with the element after being something other than a p.

    .here .is.the #selector h4 + * {...}
    

    Hope this helps anyone who finds it :)

    0 讨论(0)
  • 2020-12-01 03:58

    Simplyfing for the begginers:

    If you want to select an element immediatly after another element you use the + selector.

    For example:

    div + p The "+" element selects all <p> elements that are placed immediately after <div> elements


    If you want to learn more about selectors use this table

    0 讨论(0)
  • 2020-12-01 03:59
    #many .more.selectors h4 + p { ... }
    

    This is called the adjacent sibling selector.

    0 讨论(0)
  • 2020-12-01 03:59

    I use latest CSS and "+" didn't work for me so I end up with

    :first-child

    0 讨论(0)
  • 2020-12-01 04:05

    For your literal example you'd want to use the adjacent selector (+).

    h4 + p {color:red}//any <p> that is immediately preceded by an <h4>
    
    <h4>Some text</h4>
    <p>I'm red</p>
    <p>I'm not</p>
    

    However, if you wanted to select all successive paragraphs, you'd need to use the general sibling selector (~).

    h4 ~ p {color:red}//any <p> that has the same parent as, and comes after an <h4>
    
    <h4>Some text</h4>
    <p>I'm red</p>
    <p>I am too</p>
    

    It's known to be buggy in IE 7+ unfortunately.

    0 讨论(0)
提交回复
热议问题