94

Hay I have an element like this

<span class='a.b'>

Unfortunately this class name comes from an eCommerce application and cannot be changed.

Can I style a class name with a dot in it?

like

.a.b { }
7
  • 43
    What insane system will generate such a classname?
    – SLaks
    Commented Aug 10, 2010 at 8:55
  • Partial dupe, but probably answers the question: stackoverflow.com/questions/448981/… Commented Aug 10, 2010 at 8:58
  • insane system: class names are keys from properties file. Different properties files for different languages allow for dynamic formatting based on the language-independent but semantically same content.
    – mut1na
    Commented Aug 24, 2011 at 12:54
  • 3
    "What insane system will generate such a classname?" Shopify does..ugh.
    – jwinn
    Commented Jan 24, 2013 at 0:13
  • 2
    @SLaks - google, facebook..youtube..gmail..basically any large web system
    – vsync
    Commented Feb 20, 2014 at 16:35

4 Answers 4

137
.a\.b { }

However there could be browsers around that don't support this.

12
  • like? Would firefox 1.5 +, Safari 3+ and IE 6+ support it?
    – dotty
    Commented Aug 10, 2010 at 8:55
  • 2
    I'm not sure (thus the "could"). However IE6 surprisingly does.
    – RoToRa
    Commented Aug 10, 2010 at 9:01
  • 1
    It was IE5.x, and early versions of Opera, that didn't support this.
    – bobince
    Commented Aug 10, 2010 at 9:11
  • 2
    You need to escape the dot that is part of the class name with a backslash, thus in this case: span.a\.b. Example: jsfiddle.net/Mrafq/1
    – RoToRa
    Commented Aug 11, 2010 at 12:12
  • 1
    In Google Chrome I get it to work with double backslashes... => .a\\.b {}
    – algorhythm
    Commented May 17, 2013 at 9:31
51

Coming very late to this party, but you can use attribute selectors.

In your case, to target the class='a.b' element, you could use:

[class~="a.b"] {...}
// or
span[class~="a.b"] {...}

Additionally, here is the full list of attribute selectors.

Attribute Present Selector

// Selects an element if the given attribute is present

// HTML
<a target="_blank">...</a>

// CSS
a[target] {...}

Attribute Equals Selector

// Selects an element if the given attribute value
// exactly matches the value stated

// HTML
<a href="http://google.com/">...</a>

// CSS
a[href="http://google.com/"] {...}

Attribute Contains Selector

// Selects an element if the given attribute value
// contains at least once instance of the value stated

// HTML
<a href="/login.php">...</a>

// CSS
a[href*="login"] {...}

Attribute Begins With Selector

// Selects an element if the given attribute value
// begins with the value stated

// HTML
<a href="https://chase.com/">...</a>

// CSS
a[href^="https://"] {...}

Attribute Ends With Selector

// Selects an element if the given attribute value
// ends with the value stated

// HTML
<a href="/docs/menu.pdf">...</a>

// CSS
a[href$=".pdf"] {...}

Attribute Spaced Selector

// Selects an element if the given attribute value
// is whitespace-separated with one word being exactly as stated

// HTML
<a href="#" rel="tag nofollow">...</a>

// CSS
a[rel~="tag"] {...}

Attribute Hyphenated Selector

// Selects an element if the given attribute value is
// hyphen-separated and begins with the word stated

// HTML
<a href="#" lang="en-US">...</a>

// CSS
a[lang|="en"] {...}

Source: learn.shayhowe.com

2
  • 3
    The attribute spaced selector is probably more appropriate for the general case, as there may be other classed on the element that you don't want to select by.
    – thelem
    Commented Aug 4, 2016 at 15:14
  • What would a compound be like? img[data~="120"].recognition_template Where the data="120" and class ="recognition_template"?
    – RedSands
    Commented May 3, 2021 at 1:45
1

Perhaps you could scan the elements for these classes and add a class that you could style.

For instance, scan all elements with the “a.b” class and then add a new “style-ab” class or some such.

I haven’t posted any example code for this as people may want to use vanilla Javascript or jQuery and it’s a simple enough thing to do.

To clarify, my gaming framework does exactly as the OP described so translations could be applied to certain divs and spans. It’s not a nasty way to decide class names, it’s just useful for people creating markup when using a dictionary that has keys for phrases

1
  • Please add vanilla JS for those of us that need a starting point. Otherwise your answer is limited to those who understand how to 'scan all elements'
    – RedSands
    Commented May 3, 2021 at 1:37
-9

Yes you can. The meaning of CSS class name like '.a.b' is targeting elements that have CSS name with 'a' which also has class name 'b',that's to say you have both of these class in the same element. Just as div.cssname targeting div elements with cssname.

1
  • 6
    This has been down voted because you haven't understood the question. The OP doesn't have any elements with class "a" or class "b", he is trying to style an element with the class "a.b".
    – thelem
    Commented Aug 4, 2016 at 15:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.