Magento Expert Forum - Improve your Magento experience
-
Difference between id and class in css with example?
Difference between id and class in css with example?
View more threads in the same category:
-
-
CSS obviously needs these so that we may build selectors and do our styling, but other web languages like Javascript depend on them too. Main diffrencce between ID's and Classes are:
ID's are unique.Each element can have only one ID. Each page can have only one element with that ID
Example:
#element {
background: blue;
}
body div.element {
background: green;
}
Classes are NOT unique. You can use the same class on multiple elements and same elements.
Example:
.element {
background: blue;
}
-
-
Unlike the id selector, the class selector is most often used on several elements. This allows you to set a particular style for many HTML elements with the same class. The class selector uses the HTML class attribute, and is defined with a "." A simple way to look at it is that an id is unique to only one element.
Promotional Pen Drive
-
-
Class - Use a class when you want to consistently style multiple elements throughout the page/site. Classes are useful when you have, or possibly will have in the future, more than one element that shares the same style. An example may be a div of "comments" or a certain list style to use for related links.
Examples of class names are: tag, comment, toolbar-button, warning-message, or email.
ID - Use the ID when you have a single element on the page that will take the style. Remember that IDs must be unique. In your case this may be the correct option, as there presumably will only be one "main" div on the page.
Examples of ids are: main-content, header, footer, or left-sidebar.
A good way to remember this is a class is a type of item and the id is the unique name of an item on the page.
-
-
an ID is unique, a class is NOT
there can be only one element in an HTML document (webpage) with an id attribute set to "bla" while there can be (0 t0 infinity) number of elements with class = "blaBla"
-
-
The biggest difference is that a document can have multiple elements with the same class, but not with the same ID.
An Identifier must be specific to a certain element within a document. Id's are unique.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
Bookmarks