{"id":14164,"date":"2011-09-06T19:53:07","date_gmt":"2011-09-07T02:53:07","guid":{"rendered":"http:\/\/css-tricks.com\/?page_id=14164"},"modified":"2021-06-29T09:23:08","modified_gmt":"2021-06-29T16:23:08","slug":"class","status":"publish","type":"page","link":"https:\/\/css-tricks.com\/almanac\/selectors\/c\/class\/","title":{"rendered":"Class"},"content":{"rendered":"\n

A class selector in CSS starts with a dot (.), like this:<\/p>\n\n\n\n

.class {\n\n}<\/code><\/pre>\n\n\n\n

A class selector selects all elements with a matching class attribute.<\/p>\n\n\n\n

For example, this element:<\/p>\n\n\n\n

<button class=\"big-button\">Push Me<\/button><\/code><\/pre>\n\n\n\n

is selected and styled like this:<\/p>\n\n\n\n

.big-button {\n  font-size: 60px;\n}<\/code><\/pre>\n\n\n\n

You can give a class any name that starts with a letter, hyphen (-), or underscore (_). You can use numbers in class names, but a number can’t be the first character or the second character after a hyphen. Unless you get crazy and start escaping selectors<\/a>, which can get weird:<\/p>\n\n\n\n

.\\3A \\`\\( { \n  \/* matches elements with class=\":`(\" *\/\n} <\/code><\/pre>\n\n\n\n

An element can have more than one class:<\/p>\n\n\n\n

<p class=\"intro blue\">\n  This paragraph will get styles from .intro and .blue selectors.\n<\/p><\/code><\/pre>\n\n\n\n

An element with multiple classes is styled with the CSS rules for each class. You can also combine multiple classes to select elements:<\/p>\n\n\n\n

\/* only selects elements with BOTH of these classes *\/\n.intro.blue {\n    font-size: 1.3em;\n }<\/code><\/pre>\n\n\n\n

This demo shows how single-class selectors are different from combined selectors:<\/p>\n\n\n\n