Dumb Noob Dumb Noob - 13 days ago 5
CSS Question

Targeting a class value that begins with a space

I want to make a CSS selector for a class which starts with a whitespace, but I don't know how. For example:

<table class=" example">…</table>
.

Answer

A class value that begins with a space is invalid HTML.

HTML 4

6.2 SGML basic types

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

HTML 5

3.2.5.7 The class attribute

The attribute, if specified, must have a value that is a set of space-separated tokens representing the various classes that the element belongs to.

Spaces are only used to separate class values. If you start the value with a space, expect the unexpected, or the browser may simply ignore the space. But it's invalid code, so avoid doing that.

Comments