CSS ID and class doesn't show any difference [duplicate] - html

This question already has answers here:
Several elements with the same ID responding to one CSS ID selector
(3 answers)
Closed 9 years ago.
I know the differences between ID and Class. But why both IDs take effect in this html? ID should be used uniquely, right?
#text {
text-align:center;
color: red;
}
Then id="text" was used twice in my same html page, and both get same effect. Why do i have to use "class", if 'id' has same effect?

That IDs seem to behave like classes when you have duplicate IDs in a page is nothing more than a side effect of how browsers implement CSS.
Whether a document conforms to its own rule that IDs should be unique is not relevant to CSS. CSS simply says to match elements that have a specific value in its ID attribute, to an ID selector:
An ID selector represents an element instance that has an identifier that matches the identifier in the ID selector.
It does not say anything about what should happen if there is more than one element with such an ID, because it assumes that you are working with a conforming HTML document.
You should use classes to group your elements because that's what HTML says to do, and you are trying to conform to valid HTML. Having duplicate IDs is quite simply not allowed by the HTML spec, and can result in unexpected behavior, so it is not something to be relied on regardless of what effect it has in browsers.

Yes, 'id' have the same effect like 'class'. But there is some different between them.
1. The 'id' selector can't be repeat in the same page. That means when there is a id selector
named "header",there can't be another "header" in this page.This is for js dom command.
2. The 'id' selector have a higher priorty. That means when you have a markup like this:
<style>
#header{color:red;}
.header{color:blue;}
</style>
<div id="header" class="header">
test
</div>
the color in test is "red". http://www.impressivewebs.com/difference-class-id-css/

the DOM will not fail to load with duplicate IDs, but you will have negative effects with other aspects, like javascript. If you are only using ID to identify where CSS rules should apply then they will work just like Class.
In general, ID should always be unique on the page, but the DOM will not enforce this.

IDs may work for css in some browsers, but not in others. There may also be bugs and side effects, for example:
<div>
<input type="radio" id="id1" name="r1" />
<label for="id1">this is radio label for id1</label>
</div>
<div>
<input type="radio" id="id1" name="r1" />
<label for="id1">this is another label for id1</label>
</div>
<!--the second label will also trigger the first radio change -->
Sometimes the functions won't be bind to your radio. Just keep IDs unique on the page.

Related

Is it OK to use id attribute in component's html?

Consider a component c with html
<form id="someID"[formGroup]="codeEditorForm" novalidate>
</form>
with css
#SomeID{
color:grey;
}
If C gets included in a parent component P two times then wouldn't the id get duplicated as well which should not happen in html
Question 1 - should I not use id in Angular?
Question 2 - how shall I then write css rules without using id?
You can use IDs but generally you don't need to do it.
Angular scopes the styles automatically, so a class defined in one component won't affect a class with the same name in another component.
<form id="someID" class="my-form" [formGroup]="codeEditorForm" novalidate>
</form>
.my-form {
color: green;
}
Also note that you should make sure that there is always a space after you define a property. In your posted example there was no space after someId" Maybe it doesn't matter in this case but I guess it's better to start looking out for small things like this right away.
As far as I know there are there are two differences between id and class.
First is that id is used for single specific element, while class can target multiple elements, this means that you normally would use id selector if you are sure that an element will not be repeated, something like navbar.
And the second one is that id selector is more specific than class selector, this means that if there are any conflicts of styles, rules written in id selector override rules written in class selector
Normally you would not use it to override rules as there is other way to do it, so in order to decide which one to use all you need to do is think about this >> is the element you are giving id to really gonna be unique?
Angular support id on html. But as you are loading multiple times the 'C' component on your 'P' component,so the Id will be duplicated. So better you use class instead of id on your html..
<form class="yourClass"[formGroup]="codeEditorForm" novalidate>
</form>
On your 'C' component's css,
.yourClass{
color:grey;
}

What is the difference between id and class in html/css [duplicate]

This question already has answers here:
Difference between ID and Class
(8 answers)
Closed 8 years ago.
I want to know what is the exact difference between id and class in html and css.
I read somewhere that ids are unique and there can be only one element with corresponding id, and classes can be applied to many elements.
But I think this is wrong I have a code in which I have applied a single id to two elements.
<link type="text/css" href="main.css" rel="stylesheet">
</head>
<body id="header">
<p id="header2">Poster</p>
<p id="header2">Hello World</p>
</body>
</html>
and here is my css code
p#header2{
background-color:white;
width:50%;
height:50%;
margin-left:25%;
margin-right:25%;
text-align:center;
font-size:50px;
}
Could anyone please explain me???
Here is the Explanation in detail.
Use a class when you want to consistently style multiple elements throughout the page/site. Use the ID when you have a single element on the page that will take the style. class is a type of item and id is the unique name of an item.
EDIT:
CSS doesn't care
Regarding CSS, there is nothing you can do with an ID that you can't do with a Class and vice versa. I remember when I was first learning CSS and I was having a problem, sometimes I would try and troubleshoot by switching around these values. Nope. CSS doesn't care.
Javascript cares
JavaScript people are already probably more in tune with the differences between classes and ID's. JavaScript depends on there being only one page element with any particular, or else the commonly used getElementById function wouldn't be dependable. For those familiar with jQuery, you know how easy it is to add and remove classes to page elements. It is a native and built in function of jQuery. Notice how no such function exists for ID's. It is not the responsibility of JavaScript to manipulate these values, it would cause more problems than it would be worth.
EDIT2:
The Documentation clearly shows that HTML required the ID attribute to be unique in a page:
This attribute assigns a name to an element. This name must be unique in a document. If you have several elements with the same ID, your HTML is not valid.
So in JavaScript, getElementById() should only ever return one element. You can't make it return multiple elements.
Well, you can have more than one element with the same ID, but you shouldn't - because the consequences of doing so is unpredictable, due to differences between browsers.
ID: Id name must have 1.
<link type="text/css" href="main.css" rel="stylesheet">
</head>
<body id="header"> // ID
<p id="header2">Poster</p>
<p id="header2">Hello World</p>
</body>
</html>
Class: have multi tag using the same class name. And you can apply style for all class have name is "name".
<p class = "name">abcdef</p>
<image class = "name">......</image>
It's right:
The id of an element should be unique in the entire dom.
The class can be applied to multiple elements.
In css if you use this syntax
.yourClass {
color: white;
}
every element with class="yourClass" have the style color: white.
The id HAVE to be unique for different reason. The thing first jump into my mind its the use of jquery where you identify dom elements with the ID
The id attribute is meant to be unique and should contain a single identifier. The class attribute may contain a space-separated list of class identifiers allowing for cumulative application of css rules.
This is notwithstanding to css engines that do not complain on double ids.
Simple - id is the one that can be used only once and it will be unique. Scripts mostly use id's but it doesn't mean that id will only be used with js and not css. You can used a id as selector to in css.
While classes when two or more divs have the same styling so use class instead of increasing lines of codes.

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

Can a HTML element have multiple unique ID attributes? [duplicate]

This question already has answers here:
Can an HTML element have multiple ids?
(18 answers)
Closed 8 years ago.
Needed to know if an HTML element can have multiple attributes of ID's, for instance :
<input type="text" id="identifier1" id="selector1" />
As I needed to clarify this statement mentioned about selectors at W3 website.
If an element has multiple ID attributes, all of them must be treated
as IDs for that element for the purposes of the ID selector. Such a
situation could be reached using mixtures of xml:id, DOM3 Core, XML
DTDs, and namespace-specific knowledge.
The Possible duplicates which people are referring, states question for this syntax
<input type="text" id="identifier1 selector1" />
which is different than syntax that I am asking.
Needed to know if an HTML element can have multiple attributes of ID's
Short answer? No because the browser will only render the first one.
See this Fiddle, I can only target it in CSS using the first id that appears in the DOM. Try changing that CSS selector to use the second id, it won't work.
That's because it seems like the second ID is being disregarded by the browser, as this is the output HTML:
<input type="text" id="identifier1">
If you really need additional identifiers on an element, you should think about using either multiple class names or data attributes to correspond to additional data.
Needed to know if an HTML element can have multiple attributes of ID's
No. No element in HTML may have more then one instance of a given attribute.
As I needed to clarify this statement
Note the last sentence in that statement.
Also note that the CSS idea of an "ID attribute" is not "An attribute with the name id". Also quoting from that document:
Document languages may contain attributes that are declared to be of type ID
Only the id attribute is an ID type in HTML.
No, even if you specify multiple ids, the first encountered id attribute is used.
No ID can not be same for html elements, but the Classes are to use for multiple elements, and one element may have multiple classes
No, because an attribute must not be repeated in tag. This is a general rule in HTML, not limited to the id attribute. For nominally SGML-based versions of HTML and for XHTML, this follows from general SGML and XML rules. For HTML serialized HTML5, see HTML5 CR, 8.1.2.3 Attributes.
It’s difficult to see why you would use duplicate id attributes, so I can’t suggest a workaround. In general, for any normal use of the id attribute, one attribute per element is sufficient.
No. Element IDs should be unique within the entire document.

Why do IDs exist?

Anything that you can do with IDs you can do with classes.
So why is there a ID attribute then?
Yeah the ID is unique but a class can be unique too...
The DOM doesn't exist just for the purpose of styling elements - elements can also be manipulated (typically via Javascript), and generally selecting elements by ID is much more efficient than selecting by class.
Firstly, from the HTML 4.01 Specification Section 7.5.2:
id = name
This attribute assigns a name to an element. This name must be unique in a document.
class = cdata-list
This attribute assigns a class name or set of class names to an element. Any number of elements may be assigned the same class name or names. Multiple class names must be separated by white space characters.
I understand that the spirit of your question is that by intentionally choosing unique class names, you can simulate the functionality given by id attributes, arguably making the id attribute redundant. Well, I can think of at least one thing that can be done only with ids; associating a <label> element with a form element using the for attribute. eg:
<label for="firstname">First Name:<label>
<input type="text" id="firstname" name="firstname" value="" />
This is a good practice for accessibility reasons. If you use <label for="id"> with checkbox or radio button elements, you get the added bonus of the labels being clickable. eg:
<label for="male">Male</label> <!-- the word "Male" is clickable -->
<input type="radio" id="male" name="sex" value="male" />
<label for="female">Female</label> <!-- the word "Female" is clickable -->
<input type="radio" id="female" name="sex" value="female" />
class is for a set of similar elements
id is unique to an element.
If you want to change all, say, checkboxes, you use class.
If you want to do something to a specific checkbox, you use id.
This tizag article might explain it better. In each standard in HTML, class and id have different functions. For instance, an id is (usually) only utilized once, to denotate a unique element. class is (again, usually) used to define a group of elements.
Anything that you can do with IDs you can do with classes.
Actually no. If you give an element an ID, it can be the target of a link (e.g., http://www.example.com/page#foo takes you to the element with id="foo" on page; this is now preferred over the old <a name="foo"> mechanism.)
So why is there a ID attribute then?
Because they serve entirely different purposes. The example above is just one of the myriad of uses to which a unique identifier for content on a page might be put. Having a unique identifier is important for relationships between things.
Yeah the ID is unique but a class can be unique too...
Can be being the operative phrase. If I tell the browser to look up the element with the "foo" id, once it has found that element it can stop looking. If I tell it I want elements with the class "bar", it has to search the entire DOM to find them.
From http://www.w3.org/TR/html401/struct/global.html#adef-class :
The id attribute has several roles in HTML:
As a style sheet selector.
As a target anchor for hypertext links.
As a means to reference a particular element from a script.
As the name of a declared OBJECT element.
For general purpose processing by user agents (e.g. for identifying fields when extracting data from HTML pages into a database, translating HTML documents into other formats, etc.).
The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:
As a style sheet selector (when an author wishes to assign style information to a set of elements).
For general purpose processing by user agents.
I can't seem to find a reference for when the class attribute was added to the HTML spec, but I can say from personal experience that I remember the id attribute a lot farther back (particularly for form processing).