FOLLOW US ON TWITTER



FOLLOW US



img/blogimg/html5.jpg


@harish 1043

Classes and ID


2021-11-17 13:41 · 11 min read

Classes and ID

Classes and IDs make referencing HTML elements from scripts and stylesheets easier. The class attribute can be used on one or more tags and is used by CSS for styling. IDs however are intended to refer to a single element, meaning the same ID should never be used twice. IDs are generally used with JavaScript and internal document links, and are discouraged in CSS. This topic contains helpful explanations and examples regarding proper usage of class and ID attributes in HTML.

ParameterDetails
classIndicates the Class of the element (non-unique)
IDIndicates the ID of the element (unique in the same context)


 

Giving an element a class

Classes are identifiers for the elements that they are assigned to. Use the class attribute to assign a class to an element.

<div class="example-class"></div>

To assign multiple classes to an element, separate the class names with spaces.

<div class="class1 class2"></div>

Copy

Using classes in CSS

Classes can be used for styling certain elements without changing all elements of that kind. For example, these two span elements can have completely different stylings:


 

<span></span>
<span class="special"></span>

Classes of the same name can be given to any number of elements on a page and they will all receive the styling associated with that class. This will always be true unless you specify the element within the CSS.

For example, we have two elements, both with the class highlight:

<div class="highlight">Lorem ipsum</div>
<span class="highlight">Lorem ipsum</span>

 

If our CSS is as below, then the color green will be applied to the text within both elements:

.highlight { color: green; }
/* However, if we only want to target div's with the class highlight then we can add specificity like below: */
div.highlight { color: green; }

Info:- Nevertheless, when styling with CSS, it is generally recommended that only classes (e.g. .highlight) be used rather than elements with classes (e.g. div. highlight).
As with any other selector, classes can be nested:

.main .highlight { color: red; } /* Descendant combinator */
.footer > .highlight { color: blue; } /* Child combinator */

 

You can also chain the class selector to only select elements that have a combination of several classes. For example, if this is our HTML:

<div class="special left menu">This text will be pink</div>


 

/*And we want to colour this specific piece of text pink, we can do the following in our CSS:*/
.special.left.menu { color: pink; }

 

Example:-

<style>
.highlight { color: green;background:gold;margin:5px; }
</style>
<div class="highlight">Lorem ipsum</div>
<span class="highlight">Lorem ipsum</span>

Click here to learn more about CSS

Giving an element an ID:-

The ID attribute of an element is an identifier that must be unique in the whole document. Its purpose is to uniquely identify the element when linking (using an anchor), scripting, or styling (with CSS).

<div id="example-id"></div>

Copy

You should not have two elements with the same ID in the same document, even if the attributes are attached to two different kinds of elements. For example, the following code is incorrect:

Example code:

<div id="example-id"></div>
<span id="example-id"></span>

 

Browsers will do their best to render this code, but unexpected behavior may occur when styling with CSS or adding functionality with JavaScript.

To reference elements by their ID in CSS, prefix the ID with #.

#example-id { color: green; }

 

Do you know? :- To jump to an element with an ID on a given page, append # with the element name in the URL.

http://example.com/about#example-id

This feature is supported in most browsers and does not require additional JavaScript or CSS to work.

Example:-

<style>
#example_ID { color: yellow;background:orange;padding:5px; }
</style>
<div id="example_ID">Lorem ipsum</div>

Acceptable Values for Id and Classes:-

For an ID:-
version > 5

The only restrictions on the value of an id are:

  1. it must be unique in the document
  2. it must not contain any space characters
  3. it must contain at least one character

So the value can be all digits, just one digit, just punctuation characters, include special characters, whatever. Just no whitespace.

Example:-

So these are valid:

<div id="container"> ... </div>
<div id="999"> ... </div>
<div id="#%LV-||"> ... </div>
<div id="____V"> ... </div>
<div id="??"> ... </div>
<div id="?"> ... </div>
<div id="{}"> ... </div>
<div id="©"> ... </div>
<div id="??¤?€~¥"> ... </div>

 

This is invalid:

<div id=" "> ... </div>
<!-- This is also invalid, when included in the same document: -->
<div id="results"> ... </div>
<div id="results"> ... </div>

Copy

version < 4

An id value must begin with a letter, which can then be followed only by:

  • letters (A-Z/a-z)
  • digits (0-9)
  • hyphens ("-")
  • underscores ("_")
  • colons (":")
  • periods (".")

Referring to the first group of examples in the HTML5 section above, only one is valid:

<div id="container"> ... </div>

Copy

For an Classes:-

The rules for classes are essentially the same as for an id. The difference is that class values do not need to be unique in the document.

Referring to the examples above, although this is not valid in the same document:

<!--Id must be unique in the document -->
<div id="results"> ... </div>
<div id="results"> ... </div>
<!--Class values do not need to be unique in the document:-->
<div class="results"> ... </div>
<div class="results"> ... </div>

 

Important Note: How ID and Class values are treated outside of HTML :-

Keep in mind that the rules and examples above apply within the context of HTML

Using numbers, punctuation or special characters in the value of an id or a class may cause trouble in other contexts, such as CSS, JavaScript and regular expressions.

For example, although the following id is valid in HTML5:

<div id="9lions"> ... </div>

 

... it is invalid in CSS:

4.1.3 characters and case

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. (emphasis added)

In most cases you may be able to escape characters in contexts where they have restrictions or special meaning.

Problems related to duplicated IDs:-

Having more than one element with the same ID is a hard to troubleshoot problem. The HTML parser will usually try to render the page in any case. Usually no error occurs. But the pace could end up in a mis-behaving web page.

In this example:

<div id="aDiv">a</div>
<div id="aDiv">b</div>


 

/*CSS selectors still work*/
#aDiv {
color: red;
}

 

But JavaScript fails to handle both elements:

TAGS:






POST COMMENTS
No Comment

Please login to post comment




POPULAR POSTS

best free web hosting services...
what is new in miui 11?...
free website ssl security using cloudfla...
do you want to make money online?...
CSS3 @IMPORT RULES...
What is HTML Anchor Tag and Hyperlinks?...
CSS3 RESPONSIVE UI...
How to host a web page in 10 minutes....
Tables HTML...
CSS3 FUNCTIONS...
How to use CSS3 Overflow Property?...
CSS3 FILTERS...


RECENT COMMENTS

2022-03-05 11:05
By coder on XAMPP - MySQL shutdown unexpectedly | How to fix MySQL crash unexpectedly
2021-10-12 12:34
By abnongoke on do you want to make money online?
2021-10-12 12:34
By abnongoke on how to get a free website domain name?
2021-10-12 12:34
By solar panel for shed on what is new in miui 11?
2021-10-12 12:34
By solar panel for shed on best free web hosting services
2021-10-12 12:34
By amos tanui on free website ssl security using cloudflare


SOCIAL SHARE



FOLLOW US ON TWITTER



FOLLOW US ON FACEBOOK



FOLLOW US







Recent Blogs



Contact Us

Subscribe Our News-Letter



Kashipur, 244713
Uttarakhand, India



© blog.theprodevelopers.com 2024. All Rights Reserved. Powered by Pro Developer