11

Is there a CSS selector where i can select all anchors with a class containing icon-*

<a class="icon-a icon-large scrollTo" href="#a"></a>
<a class="icon-large icon-b scrollTo" href="#b"></a>
<a class="icon-large scrollTo icon-c" href="#c"></a>

I just jumbled up the icon- since i want to check if the css selector can handle all cases.

I want to be able to change the style of all anchors that contains the class icon-*. This code doesn't seem to work.

a [class^="icon-"], a [class*=" icon-"] {
    text-decoration: none;
    color: #1BA1E2;
}

Is Javascript my only option?

4
  • color: #PINK; the # is too much? Commented Jun 5, 2013 at 15:07
  • 1
    You can't have a class that contains space, so the second selector doesn't make sense. Commented Jun 5, 2013 at 15:09
  • 2
    @Barmar: But you can have a class attribute with a value that contains a space. It's right there in the markup - spaces are used to separate class names. Commented Jun 5, 2013 at 15:09
  • D'oh, right, the attribute selector doesn't parse it like a class selector does. Commented Jun 5, 2013 at 15:10

2 Answers 2

22

You were using an incorrect selector - a [class] is all anchors with a class as a descendant.

Basically, any element descending from an <a>, which starts with or contains the class icon- will be targeted.

What you want is to select all anchors starting with or containing that class themselves - a[class]:

a[class^="icon-"], a[class*=" icon-"] {
   text-decoration: none;
   color: #1BA1E2;
}

jsFiddle example here.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm a complete css noob. Thanks! Is the 2nd selector actually required? Not exactly sure what it does since i coped this code from font-awesome in an attempt to override something.
@user1133619: The first selector is the more appropriate one, and yes they both do the same thing in this context.
5

Yes - but you first need to remove the space between the type selector and the attribute selector and the space in the attribute value:

a[class^="icon-"], a[class*="icon-"] {
text-decoration: none;
color: pink; /* get rid of the # */
}

http://jsfiddle.net/c8YdD/1/

1 Comment

And now the question has changed it to #1BA1E2 which is decidedly not pink...?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.