Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Select all classes containing a word?

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #42681
    Blackhawkso
    Member

    Hey everyone,

    I’ve got a feeling i’ve seen this done but i’m getting a complete mental block. I’m using the icomoon icon font and classes.

    I am attaching various icons to different li in an ul. I am doing this using the class method so for example

    <li class="icon-map">
    

    what I want to do is select all versions of .icon without having to specify all of them. I’ve got a niggling feeling that i’ve seen this done but I might be imagining it or just quite simply forgot it.

    Can anyone help??

    Cheers.
    Phil

    #124270
    tomrogers123
    Member

    You can use an attribute selector where the attribute to be selected is class but must start with “icon” eg.

    `[class^=”icon”] {

    }`

    This will select all elements with a class beginning with icon. If you want to learn more, this could help: https://css-tricks.com/attribute-selectors/

    #124275
    Paulie_D
    Member

    I think

    [class*=”keyword”]
    would select al classes containing ‘keyword’ whether it’s first, last or part of a name….right?

    http://codepen.io/anon/pen/rpGob

    http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/

    #124291
    CrocoDillon
    Participant

    [class^="icon"] sadly fails if there is another class in specified before icon-whatever like &lt;li class="fancy icon-map"&gt;, go with [class*="icon"] as Paulie mentioned :)

    #124294
    Blackhawkso
    Member

    Right got it thank you everyone for your help.

    I’m very glad that i didn’t just imagine it lol

    #241921
    nicholemaureen
    Participant

    FYI for those copy/pasting this in the future – replace the quotations! Mine failed initially because they were curly quotes.

    #247701
    StevenWho
    Participant

    Thanks Nichole!

    #270503
    Deven
    Participant

    How could you do this with Javascript?

    #270543
    Beverleyh
    Participant

    Look in to querySelectorAll() with the class/asterisk attribute selector advised above.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘CSS’ is closed to new topics and replies.