Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I apply IDs more than once in CSS? [closed]

Tags:

html

css

xhtml

This is a beginner's question, but I need to clarify it. Yes according to most of the tutorials it cannot be applied more than once.

ID's are unique
Each element can have only one ID
Each page can have only one element with that ID

Reference http://css-tricks.com/the-difference-between-id-and-class/

But I wrote a simple mark-up in HTML and applied some CSS.

Here is my code

HTML

<!DOCTYPE html>
<html>
<head>
    <title>ID vs Classes</title>
    <link rel="stylesheet" type="text/css" href="ID & Classes.css">
</head>
<body>
    <h1 id="forh1">This is a h1 header</h1>
    <p>Lorem ipsum dolor sit amet</p>

    <h1 id="forh1">This is a h1 header</h1>

    <p>Curabitur sodales ligula</p>

    <h2 id="forh1">This is a h2 header</h2> 
</body>
</html>

CSS

#forh1{
text-align:center;


}

I have applied the same ID more than once (3 times). Two times in h1 header and one time in h2 header. When I run it in browser (chrome & firefox) they worked perfectly. All text in h1 and h2 headers were aligned center. Why is that ? Have not I grasped ID concept properly or is it an issue with the browser? Because I have heard modern browsers are smart enough to understand most of the implicit things. Please help.

like image 382
Gihanmu Avatar asked Feb 01 '14 07:02

Gihanmu


People also ask

Can I use a CSS ID more than once?

The major difference is that IDs can only be applied once per page, while classes can be used as many times on a page as needed. “ if you want to apply same css to multiple html elements, use class. just define a class in css, write all stylings and then give that class to all elements you want to style same.

Can IDs be used more than once in HTML?

IDs must be unique - the same ID should not be used more than once in the same page. Otherwise, they can interfere with the user agent's (e.g. web browser, assistive technology, search engine) ability to properly parse and interpret the page.

How many times can you use ID in HTML?

You can have as many ids as you want on a page but you can only use a specific id once. For example; id="logo" can only be used once on a page. You could also use id="gallery" on the same page but it can be used only once on that page. On the other hand class="someClass" could be used multiple times on a page.


2 Answers

ID's are meant to use for identifying elements uniquely, they will work as far as CSS goes, but when JavaScript comes into action, you will understand why not to do so.

CSS just selects the element, when you write a matching selector, it has nothing to do with valid or invalid HTML you write, the job of CSS is to apply styles as per the selector defined. For example

Demo INVALID HTML, DO NOT USE IT (only for demonstration purpose)


Hence, ID's should be unique,, use class if you want to use the same properties on multiple elements.

From W3C

This attribute assigns a name to an element. This name must be unique in a document.


Side tip: Use _ instead of spaces href="ID & Classes.css" should be href="IDs_Classes.css".

Here's a good read, on why not to do that (So won't repeat here)

like image 148
Mr. Alien Avatar answered Sep 24 '22 11:09

Mr. Alien


You can use ids more than once. The browser will still apply the styles. It is, however, not best practice to do so and is classed as invalid markup.

To get around it you can add a class to the element. Classes can be used on as many elements as you want.

Check the W3C validator and it throws the error duplicate ids

like image 39
Cjmarkham Avatar answered Sep 20 '22 11:09

Cjmarkham