Can Xhtml and HTML Class Attributes Value Start with a Number

HTML & XHTML id attribute question

For HTML, no - http://www.w3.org/TR/html401/types.html#type-name

Theoretically, the collection of legal values in XHTML could be much larger however I doubt it would be compatible with many browsers. See http://www.w3.org/TR/xhtml1/#C_8

What are valid values for the id attribute in HTML?

For HTML 4, the answer is technically:

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 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

The id attribute is case sensitive in XHTML.

As a purely practical matter, you may want to avoid certain characters. Periods, colons and '#' have special meaning in CSS selectors, so you will have to escape those characters using a backslash in CSS or a double backslash in a selector string passed to jQuery. Think about how often you will have to escape a character in your stylesheets or code before you go crazy with periods and colons in ids.

For example, the HTML declaration <div id="first.name"></div> is valid. You can select that element in CSS as #first\.name and in jQuery like so: $('#first\\.name'). But if you forget the backslash, $('#first.name'), you will have a perfectly valid selector looking for an element with id first and also having class name. This is a bug that is easy to overlook. You might be happier in the long run choosing the id first-name (a hyphen rather than a period), instead.

You can simplify your development tasks by strictly sticking to a naming convention. For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern. You will never wonder "was it firstName or FirstName?" because you will always know that you should type first_name. Prefer camel case? Then limit yourself to that, no hyphens or underscores, and always, consistently use either upper-case or lower-case for the first character, don't mix them.


A now very obscure problem was that at least one browser, Netscape 6, incorrectly treated id attribute values as case-sensitive. That meant that if you had typed id="firstName" in your HTML (lower-case 'f') and #FirstName { color: red } in your CSS (upper-case 'F'), that buggy browser would have failed to set the element's color to red. At the time of this edit, April 2015, I hope you aren't being asked to support Netscape 6. Consider this a historical footnote.

Custom data in XHTML 1.0 Strict

You can use the jQuery MetaData plugin which allows you to write data in the class attribute in JSON format:

<li class="someclass {some: 'data'} anotherclass">...</li>

Then in your jQuery, to get at the data:

var data = $('li.someclass').metadata();
if ( data.some && data.some == 'data' )
alert('It Worked!');

This should meet your requirements of being xhtml 1.0 strict, and also allows you to use a plug-and-play solution :)

html id/class first symbol digit, is valid?

HTML5:

As of HTML5, it is indeed valid to start an ID with a digit.

In HTML5, even this is valid:

<p id="#">Foo.
<p id="##">Bar.
<p id="♥">Baz.
<p id="©">Inga.
<p id="{}">Lorem.

As is your example:

<div id="4bla"></div>

Note: It may be valid in HTML5, however it is not valid in CSS.

That means <div id="4bla"></div> is valid, but #4bla { background-color:red; } isn't.

Start ID's with characters instead for maximum compatibility.

HTML4:

It is invalid if you're still using HTML4:

"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 (".")."

Is it possible to specify a starting number for an ordered list?

If you need the functionality to start an ordered list (OL) at a specific point, you'll have to specify your doctype as HTML 5; which is:

<!doctype html>

With that doctype, it is valid to set a start attribute on an ordered list. Such as:

<ol start="6">

<li>Lorem</li>

<li>Ipsum</li>

<li>Dolor</li>

</ol>

Can an HTML element have multiple ids?

No. From the XHTML 1.0 Spec

In XML, fragment identifiers are of
type ID, and there can only be a
single attribute of type ID per
element. Therefore, in XHTML 1.0 the
id attribute is defined to be of type
ID. In order to ensure that XHTML 1.0
documents are well-structured XML
documents, XHTML 1.0 documents MUST
use the id attribute when defining
fragment identifiers on the elements
listed above. See the HTML
Compatibility Guidelines for
information on ensuring such anchors
are backward compatible when serving
XHTML documents as media type
text/html.

Is it possible to display an enumerated list in HTML starting from given position?

There is a start value that can be defined on your list item alongside a type.

<ol type="A" start="3">

This is the passage taken from http://www.w3.org/TR/html4/struct/lists.html.

start = number
Deprecated. For OL only. This attribute specifies the starting number of the first item in an ordered list. The default starting number is "1". Note that while the value of this attribute is an integer, the corresponding label may be non-numeric. Thus, when the list item style is uppercase latin letters (A, B, C, ...), start=3 means "C". When the style is lowercase roman numerals, start=3 means "iii", etc.

And you can experiment yourself using
http://www.w3schools.com/TAGS/tryit.asp?filename=tryhtml_ol_start

CSS selectors cannot match numerical attribute values? why?

Wrap the string to match in quotes...

[abc="1"] {
...
}

jsFiddle.

Attribute values must be CSS identifiers or strings.

Source.

When you wrap it in quotes, you are telling it to match a string.

When you don't quote it, it is looking for an identifier.

In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.



Related Topics



Leave a reply



Submit