Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML class attribute with spaces, it is a W3C valid class?

Tags:

html

css

About HTML class attribute, that assigns CSS class (or classes?) to a tag. The use of spaces, like in

<tag class="a b">....</tag>

is valid?

This syntax is used by some web-designers and occurs into exported HTML of Adobe InDesign (tested with versions 5 and 6), and another HTML generation softwares...

It (class="a b") is a valid W3C syntax? What versions of CSS and HTML? (starting from which version became valid?)


EDIT: a natural subquestion "W3C say how to interpret it?" (it is an "override" or another renderization behaviour?) was posted here.

like image 323
Peter Krauss Avatar asked Dec 10 '12 20:12

Peter Krauss


People also ask

Can HTML classes have spaces?

A class name can't have spaces. When you have a space-separated string in your class attribute, you're always giving your element several classes.

What does a space in a class mean HTML?

this attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.

Can CSS class have spaces?

The class name can't contain a space, but it can contain hyphens or underscores. Any tag can have multiple space-separated class names.

What is the class attribute in HTML?

The HTML class attribute specifies one or more class names for an element. Classes are used by CSS and JavaScript to select and access specific elements. The class attribute can be used on any HTML element. The class name is case sensitive. Different HTML elements can point to the same class name.


Video Answer


2 Answers

these are two different classes a & b separated by space. see w3c DOCS

class = cdata-list [CS]

this attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.


If you have two class

.a { font-weight: bold; }
.b { font-weight: normal; }

and assign in class="a b" or class="b a", then later class will overwrite the prior class having same property, so font weight will be normal.

If you change the CSS definition order,

.b { font-weight: normal; }
.a { font-weight: bold; }

now the later class is bold, so "overwrite the prior class having same property" results font weight bold.

like image 134
diEcho Avatar answered Oct 07 '22 14:10

diEcho


This is supported in IE 7 and up, including all modern, non-IE browsers. As other commenters have pointed out, it is actually a list of classes, not a single class with spaces.

A better way to understand this is to give your example a few more options:

<tag class="a b">....</tag>
<tag class="a">....</tag>
<tag class="b">....</tag>

.a.b {} in your css will target the first tag.
.a {} will target the first and second tags.
.b {} will target the first and third tags.

This is why using multiple classes on a single element can be very helpful.

For questions of CSS selectors and pseudo selectors, I like to use this (slightly outdated) table http://kimblim.dk/css-tests/selectors/

like image 4
Jason Avatar answered Oct 07 '22 16:10

Jason