What is the difference between pipe (|) and caret (^) attribute selectors?

后端 未结 4 825
广开言路
广开言路 2020-11-28 06:19

At W3Schools they declare both | and ^ to mean: Select an element with an attribute starting with a specified value.

So what\'s the differe

4条回答
  •  温柔的废话
    2020-11-28 07:00

    Caret (^): selects an element (

    ) where the value of the specified attribute (rel) starts with a certain value (val):

    h1[rel^="val"] { /** formatting */ }
    

    h1[rel^="friend"] { color: blue; }

    I'm Blue.

    I'm Blue.

    I'm Black.

    Pipe (|): selects an element (

    ) where the value of the specified attribute (rel) is either exactly the value (val) or starts with the value immediately followed by - (val-):

    h1[rel|="val"] { /**formatting */ }
    

    h1[rel|="friend"] { color: red; }

    I'm Red.

    I'm Black.

    More information about this selectors you can find here: https://css-tricks.com/attribute-selectors/ or on the official CSS specification: https://www.w3.org/TR/css3-selectors/#attribute-selectors

提交回复
热议问题