Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4601754
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T23:52:30+00:00 2026-05-21T23:52:30+00:00

I’m reading the spec on attribute selectors , but I can’t find anything that

  • 0

I’m reading the spec on attribute selectors, but I can’t find anything that says if whitespace is allowed. I’m guessing it’s allowed at the beginning, before and after the operator, and at the end. Is this correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T23:52:31+00:00Added an answer on May 21, 2026 at 11:52 pm

    The rules on whitespace in attribute selectors are stated in the grammar. Here’s the Selectors 3 production for attribute selectors (some tokens substituted with their string equivalents for illustration; S* represents 0 or more whitespace characters):

    attrib
      : '[' S* [ namespace_prefix ]? IDENT S*
            [ [ '^=' |
                '$=' |
                '*=' |
                '=' |
                '~=' |
                '|=' ] S* [ IDENT | STRING ] S*
            ]? ']'
      ;
    

    Of course, the grammar isn’t terribly useful to someone looking to understand how to write attribute selectors, as it’s intended for someone who’s implementing a selector engine.

    Here’s a plain-English explanation:

    Whitespace before the attribute selector

    This isn’t covered in the above production, but the first obvious rule is that if you’re attaching an attribute selector to another simple selector or a pseudo-element, don’t use a space:

    a[href]::after
    

    If you do, the space is treated as a descendant combinator instead, with the universal selector implied on the attribute selector and anything that may follow it. In other words, these selectors are equivalent to each other, but different from the above:

    a [href] ::after
    a *[href] *::after
    

    Whitespace inside the attribute selector

    Whether you have any whitespace within the brackets and around the comparison operator doesn’t matter; I find that browsers seem to treat them as if they weren’t there (but I haven’t tested extensively). These are all valid according to the grammar and, as far as I’ve seen, work in all modern browsers:

    a[href]
    a[ href ]
    a[ href="http://stackoverflow.com" ]
    a[href ^= "http://"]
    a[ href ^= "http://" ]
    

    Whitespace is not allowed between the ^ (or other symbol) and = as these are treated as a single token, and tokens cannot be broken apart.

    If IE7 and IE8 implement the grammar correctly, they should be able to handle them all as well.

    If a namespace prefix is used, whitespace is not allowed between the prefix and the attribute name.

    These are incorrect:

    unit[sh| quantity]
    unit[ sh| quantity="200" ]
    unit[sh| quantity = "200"]
    

    These are correct:

    unit[sh|quantity]
    unit[ sh|quantity="200" ]
    unit[sh|quantity = "200"]
    

    Whitespace within the attribute value

    But notice the quotes around the attribute values above; if you leave them out, and you try to select something whose attribute has spaces in its value you have a syntax error.

    This is incorrect:

    div[class=one two]
    

    This is correct:

    div[class="one two"]
    

    This is because an unquoted attribute value is treated as an identifier, which doesn’t include whitespace (for obvious reasons), whereas a quoted value is treated as a string. See this spec for more details.

    To prevent such errors, I strongly recommend always quoting attribute values, whether in HTML, XHTML (required), XML (required), CSS or jQuery (once required).

    Whitespace after the attribute value

    As of Selectors 4 (following the original publication of this answer), attribute selectors can accept flags in the form of an identifier appearing after the attribute value. Two flags have been defined pertaining to character case, one for case-insensitive matching:

    div[data-foo="bar" i]
    

    And one for case-sensitive matching (whose addition I had a part in, albeit by proxy of the WHATWG):

    ol[type="A" s]
    ol[type="a" s]
    

    The grammar has been updated thus:

    attrib
      : '[' S* attrib_name ']'
        | '[' S* attrib_name attrib_match [ IDENT | STRING ] S* attrib_flags? ']'
      ;
    
    attrib_name
      : wqname_prefix? IDENT S*
    
    attrib_match
      : [ '=' |
          PREFIX-MATCH |
          SUFFIX-MATCH |
          SUBSTRING-MATCH |
          INCLUDE-MATCH |
          DASH-MATCH
        ] S*
    
    attrib_flags
      : IDENT S*
    

    In plain English: if the attribute value is not quoted (i.e. it is an identifier), whitespace between it and attrib_flags is required; otherwise, if the attribute value is quoted then whitespace is optional, but strongly recommended for the sake of readability. Whitespace between attrib_flags and the closing bracket is optional as always.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.