Ted
Ted

Reputation: 3885

Using ID with CLASS in a tag

I would like to mark a tag with an id in HTML like this:

<span id="id_errortag" 
    style="font-family:Helvetica;Font-size:14px;" class="red"></span>

I want to find out whether this is ok by HTML standards to use an id in conjunction with a class?

Upvotes: 0

Views: 135

Answers (8)

Daan
Daan

Reputation: 3348

Certainly, see the specs for HTML 4.01 and the draft of the HTML5 specs. And see also this article on the difference between id and class.

Upvotes: 0

Jim Thomas
Jim Thomas

Reputation: 1658

IDs and Classes are meant for different uses. Elements only have one ID and that should be unique across the content. Elements can have multiple classes (space-separated) and the same classes can be applied to multiple elements. It's fine to use them both, but depending on the context it is often better to step back and look at your intent before deciding which to use.

Upvotes: 1

Bibin Velayudhan
Bibin Velayudhan

Reputation: 3103

Yes. But ID must be only one since it is unique. Classes can be many and can be separated with comas.

Upvotes: 1

jamesmortensen
jamesmortensen

Reputation: 34078

It is perfectly acceptable to use both an id and a class on an HTML element, so long as you remember that id's must be unique on the page. Classes can be used to group elements, but id must be unique.

Aside from classes, you can use other attribute selectors to identify elements. You aren't limited to just class and id. For instance, you could use a CSS selector to change the color of an input with a certain value.

Keep in mind that one of the uses of class/id selectors is to help you keep your content, presentation, and behavior separate from each other, which will make it easier for Web designers to work with you. With CSS selectors, your JavaScript can be kept completely in external JavaScript files, your CSS can all be kept in an external CSS file, and your content can be kept inside the HTML.

If someone needs to change the presentation or behavior, they can do so without needing to modify the HTML, and if someone needs to change the style, they don't need to modify the HTML. This not only helps reduce bugs, but makes the code easier to maintain.

Upvotes: 3

Ryan
Ryan

Reputation: 5682

Yes, that is perfectly valid. id's have to be unique but classes don't. the whole reason for a class would be to style several things that need different id's the same while allowing you to use the id's to differentiate function

Upvotes: 1

Marc
Marc

Reputation: 11633

There is nothing wrong with using both an ID and a class for an HTML element. It's very common, in fact.

Upvotes: 0

anuj_io
anuj_io

Reputation: 2173

It's 100% ok but writing inline styles isn't.

Upvotes: 1

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

Yes, most definitely. You can specify as many classes as you want and separate them by spaces, but only one id. They do not conflict each other even if they have the same name, so you don't need to namespace them with that id_ prefix.

Upvotes: 0

Related Questions