HTML class vs. id for a group of divs - html

This becomes confusing to me:
<div class='wrapper'>
<div id='redRose' class='roses'>...</div>
</div>
<div class='wrapper'>
<div id='redRose' class='roses'>...</div>
</div>
<div class='wrapper'>
<div id='redRose' class='roses'>...</div>
</div>
I can see the obvious difference between 'redRose' and 'roses', but why he assigns the same id 'redRose' to multiple divs? It seems it looses id's unique identification character here. Did I miss something?
Update:
Thanks for everyone's help. I wish I could mark everyone's answer correct.

This is not valid; the id must be unique. Class names can be shared however.
http://www.w3.org/TR/html401/struct/global.html#h-7.5.2

Well, the obvious thing to say is that markup is wrong.
Id is unique, class is not.
http://css-tricks.com/the-difference-between-id-and-class/
There could be an id of redRose to a parent container if one were to want to target something like #redRose div or #redRose .roses but as it stands the css for this would only target one of the id's specified and is just wrong to begin with.

Using the same ID more than once on any given page is wrong. It should never be done and will break code that references that ID. The proper way to do it is with classes. A css class can be assigned multiple times, whereas a ID can only be assigned once.

http://css-tricks.com/the-difference-between-id-and-class/
ID's are unique
Each element can have only one ID Each page can have only one element
with that ID
Classes are NOT unique You can use the same class on multiple
elements. You can use multiple classes on the same element.

Related

What error will i get if i use the same ID for multiple HTML elements?

I understand that unlike class, an ID should be unique in a HTML document. What will happen if I use the same id multiple times for multiple HTML elements? Will it throw any error? or will it just not work?
I tried to try this scenario but wanted to know more context around it.
It would be invalid HTML.
In some environments, it may produce a console warning that multiple IDs in a single document is invalid. Harmless, but annoying.
It will prevent document.getElementById from working properly for those elements, because getElementById selects by ID, but there should only be one element with a given ID in the document. Similarly, it may prevent querySelector and querySelectorAll with ID selectors from working properly.
Using the same ID multiple times may well not break your application in most cases, but just because it might be doable in some circumstances doesn't mean it's a good idea. There's no reason to do it, so don't.
Yes, Unlike html class, We cannot apply same id in multiple html elements, It will not throw any error but only the first element with the id will work and other elements won't work.
$('#foo').on('click',function(){
alert('work')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="foo">Click me</div>
$('#foo').on('click',function(){
alert(' work')
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="foo">Click me(work)</div>
<div id="foo">Click me(won't work)</div>
<div id="foo">Click me(won't work)</div>
I have tried it and it works if we put same id name for multiple elements its going to change properties where the id is same
Nothing really happens. JavaScript and CSS selectors will only select the first element they find with the id. It's not like your website will crash. It's just very bad practice because an id should be unique on each page.

Benefit of using class instead of id

Why would you use "div class" instead of "div id"
Usually when I name a div I use "id" to give a name to an element and with set parameters. I really only use "class" to give it special characteristics. For my example .
However, I have a professor who will "class" to identify all his divs. For example prof example .
Can someone explain the benefit of something like "div class" instead of "div id".
<div class="some_class">
<p>This can be used any number of times on one HTML page</p>
</div>
<div id="some_id">
<p>This CAN be used multiple times with the same ID,
but it is invalid code, as a specific ID should
only be used ONCE per html page</p>
</div>
Here's an older yet still good explanation.
ID are unique for the page, so it's better for identification.
Class aren't unique and are supposed to be to group similar style things together.
Said otherwise, if you need to apply something to THAT SPECIFIC div, you should call it by the id, because it will take priority over other CSS styles that may affect it.
Classes will allow you to set some common ground in your style so you can use similar fonts or sizes in different kind of elements.
If you are using Javascript there is a big advantage of using classnames to identify elements instead of id's.
Giving an id to an element also creates a global javascript variable with the same name. See Do DOM tree elements with ids become global variables? for more about that behaviour.
Having such implicitly created variables is at best confusing, and at worst leading to hard to find errors.

Which to use <div class="name"> or <div id="name">? [duplicate]

This question already has answers here:
What's the difference between an id and a class?
(17 answers)
Closed 9 years ago.
I am learning HTML. Can someone please tell me what is the difference between class and id and when to use one over the other? They seem to do the same thing
<!DOCTYPE HTML>
<html>
<head>
<style>
#mycolor1 {color: red;}
.mycolor2 {color: red;}
</style>
</head>
<body>
<div id="mycolor1"> hello world </div>
<div class="mycolor2"> hello world </div>
</body>
</html>
They do not do the same thing.id is used to target a specific element, classname can be used to target multiple elements.
Example:
<div id="mycolor1" class="mycolor2"> hello world </div>
<div class="mycolor2"> hello world2 </div>
<div class="mycolor2"> hello world3 </div>
Now, you can refer all the divs with classname mycolor2 at once using
.mycolor2{ color: red } //for example - in css
This would set all nodes with class mycolor2 to red.
However, if you want to set specifically mycolor1 to blue , you can target it specifically like this:
#mycolor1{ color: blue; }
Read the spec for the attributes and for CSS.
id must be unique. class does not have to be
id has higher (highest!) specificity in CSS
Elements can have multiple non-ordinal classes (separated by spaces), but only one id
It is faster to select an element by it's ID when querying the DOM
id can be used as an anchor target (using the fragment of the request) for any element. name only works with anchors (<a>)
Classes should be used when you have multiple similar elements.
Ex: Many div's displaying song lyrics, you could assign them a class of lyrics since they are all similar.
ID's must be unique! They are used to target specific elements
Ex: An input for a users email could have the ID txtEmail -- No other element should have this ID.
The object itself will not change. The main difference between these 2 keyword is the use:
The ID is usually single in the page
The class can have one or many occurences
In the CSS or Javascript files:
The ID will be accessed by the character #
The class will be accessed by the character .
To put it simnply: id is unique to just one element in the whole HTML document, but class can be added to numerous elements.
Also, ID properties have priority over class properties.
ids and classes are especially useful if you plan on using javascript or any of its frameworks.
class is used when u want to set properties for a group of elements, but id can be set for only one element.
ID's must be unique (only be given to one element in the DOM at a time), whereas classes don't have to be. You've already discovered the CSS . class and # ID prefixes, so that's pretty much it.
ID provides a unique indentifier for the element, in case you want to manipulate it in JavaScript. The class attribute can be used to treat a group of HTML elements the same, particularly in regards to fonts, colors and other style properties...
ID is suitable for the elements which appears only once
Like
Logo sidebar container
And Class is suitable for the elements which has same UI but they can be appear more than once.
Like
.feed in the #feeds Container
the Id selector is used when referring to a single unique element.
The Class selector referrers a group of elements.
For example, if you have a multiple buttons that look the same, you should use class="mybutton" to have consistent styling.
In terms of performance:
CSS selectors are matched from right to left.
Therefore, .myClass should be "faster" than #myID because it misses out testing.
The performance speed is negligible for normally sized pages and you will probably never notice a difference so it's mostly just about convention.
Here is more info on why css is browsers match css selectors from right to left

When should I use a class and when should I use a id?

When should I use a class and when should I use an id in html for styling my page.
Example: <h1 class="ClassName">
<h1 id="IDName">
IDs are unique, whereas classes can be reused. You could also use both simultaneously:
<h1 class="ClassName" id="IDName">
As a general rule of thumb, use id's as individual identifiers, and classes for common markup that you intend to reuse.
You use id if you have only one element of a kind and class if you have many.
For example you have a list:
<div class="listItem">Item 1</div>
<div class="listItem" id="selected">Item 2</div>
<div class="listItem">Item 3</div>
Here you can get the selected element using javascript: document.getElementById('selected').
You should never have the same id twice but you should always have a class many times.
Also have a look at the documentation, it explains it very nicely.
Use id to identify elements that there will only be a single instance of on a page or single div . For instance, if you have a single navigation bar that you are placing in a specific location, use id="navi"., for header used id="header_sectoin", for any used function in jquery than used to id id="slider_left"
Use class to group elements that all behave a certain way. For instance, if you want your company name to appear in bold in body text, you might use .
Example:
Text
#header_section {font-color:#fff}
.header_section {font-color:#000}
The text would be white.
For details :
http://css-tricks.com/the-difference-between-id-and-class/
id is unique. For example if you want to assign a css style only to one of the tags you cand relate them with id. class can be reused. for example if you want to assign a css style to all of the <a> tags you must use class.
ID's are unique
Each element can have only one ID
Each page can have only one element with that ID
Classes are NOT unique
You can use the same class on multiple elements.
You can use multiple classes on the same element.
There are no browser defaults for any ID or Class
Adding a class name or ID to an element does nothing to that element by default.
Elements can have BOTH

How to select inner divs with the IDs?

I would like to select a div within a div by specifying the ID.
HTML with specified ID
<div id="divOuter">
<div id="divInner">
</div>
</div>
And if I want to select divInner by using ID selectors in more to the point style what should I use?
I tried
div#divOuter> div#divInner
but cannot make it work.
As IDs have to be unique,
#divInner
is sufficient.
If you have more elements with the same ID you have to change that (you might want to use classes instead).
Update:
Ok, I got your point. But your selector is correct: http://jsfiddle.net/fkling/AsPam/
Either you have another rule that overrides this one, or your structure is different.
Update 2:
Oh, as others have noted , the child selector is not supported by IE6. You have to omit it then:
div#divOuter div#divInner
As IDs are unique all you really need is
#divInner
{
/*styles*/
}
This will select the ID attribute of the HTML element and apply the styles.
If you need to repeat the styles to more than one element, use a class
.divInner
{
/*styles*/
}
#divOuter div{...}
or
#divInner{...}
For starters (and I know this might be a typo), have you checked the spelling?
When it comes to the selection, you seem to want to use the id tag, in other words, all you need is to do as everyone above says: #idInner{...}