-1

I am facing very difficulty to understand these two different attribute in CSS [att|=val] and [att~=val]. Can anyone simply give me explanation for it

thanks

1 Answers1

3

[attr|=val] matches a word in val in any form, so [class=div] would match .my-div, .div, but not .mydiv.

[attr~=val] matches a complete word in val, so [class~=div] would match .div, but not .mydiv or .my-div.

Example:

HTML

<div id="myDiv"></div>
<div id="myDiv2"></div>
<div id="new-div"></div>

CSS

div[id|=myDiv] {
  /* Matches the first div */
}
div[id|=my]{
  matches first two divs
}
div[id|=new]{
  /* Matches second div - the hyphen counts as a word separator */
}
div[id~=Div]{
  /* Matches nothing - "Div" is not a separate word */
}
yaakov
  • 4,568
  • 4
  • 27
  • 51
  • 1) It's [attr|=val] with a pipe, not [attr=val] 2) "contains" is a little vague, it helps to be very specific here since other attribute selectors exist that match values containing substrings. – BoltClock Feb 09 '17 at 05:47
  • 1
    This answer is not correct according to [MDN](https://developer.mozilla.org/nl/docs/Web/CSS/Attribute_selectors). You are confusing `[attr~=val]` with `[attr*=val]`, I think? – Just a student Feb 09 '17 at 15:12