r/csshelp • • Dec 14 '21

Request class and ID differences ?

starting to leanrn more frontend because ive spent a lot of time on the backend. I came across this right here:

     <h5 class = 'author-class' id = 'author-id'>By: Stacy Gray</h5>

now to my understanding, because id is more specific, Stacy Gray will be in the color of cornflower blue right ?

    h5 {
      color:yellow;
    }
    
    .author-class {
      color: pink;
    }
    
    #author-id { 
      color: cornflowerblue; 
    }

however, my problem is that, if the ID is already going to denote what the color is going to be, why can i just take out class = 'author-id' and just leave it as id = ""; ?

7 Upvotes

3 comments sorted by

1

u/be_my_plaything Dec 14 '21

Yes your understanding of hierarchy is correct, and yes in this instance the colour given to H5 (yellow) is redundant as it is over-ruled by the class colour (pink) which is in turn redundant as it's over-ruled by the ID colour (cornflowerblue)... Stacy Gray would appear in cornflowerblue and the other two colours could be deleted without affecting anything.

However, outside of this example, you may have cause to use the element selector (H5) and the class (author-class) and the ID (author-ID). The H5 element would apply to all text that is H5, the class would apply to all authors, and the ID would be unique applying to the one instance of Stacy Gray.

So say for example you had a passage of text, all of which you wanted to be a specific size, font and colour. You could style H5 to cover this...

H5{
font-family: calibri;
font-size: 16px;
color: yellow;
}  

You now write the paragraph and it all appears in that font style and in yellow, not let's say within the paragraph there are multiple names which you want highlighted, you could give all of them the same class and do something like...

.author-class{
font-weight:bold;
text-decoration:underline;
}  

So now you have the paragraph of yellow text but each instance of a name is highlighted from the bulk of the text by being bold and underlined, but now let's say you want to also distinguish each name from the others, you give them each a unique ID and add something like...

#author-class-1{
color:pink;
}  

#author-class-2{
color:blue;
}  

...and so on for each name.  

So rather than have to apply every style to every ID you have three tiers, one for styles that appear in all text (H5) one with additions that apply to every author (Class) and one that is unique for an individual author (ID).

The trouble is, in my opinion, the example you found was just poorly thought out since it only used one attribute and over-wrote it at each step rather than actually demonstrating practical implementation of the different selectors. And the fact the they used an ID of #author-class (Rather than numbering it) implied there was just the one author as opposed to multiple authors each of whom would look unique. Basically each level of hierarchy can be used to add more specific styles to previous universal ones.

There would be nothing wrong with adding everything to the ID, it would still render the page the same but it would require far more lines of code. Say you had ten authors and you styled them purely with IDs, you would have to declare every thing above for each ID...

.author-class-1{
font-family: calibri;
font-size: 16px;
font-weight:bold;
text-decoration:underline;
color:pink;
}

You would then be repeating this ten times (Once per author) with only the colour changing, so by declaring the common attributes by class and only the unique attributes by ID you save a lot of lines.

2

u/IHateTheSATs Dec 15 '21

thank you so much ! that really clears a lot of things up for me !

0

u/[deleted] Dec 14 '21

[deleted]

1

u/theadventurekidz Dec 16 '21

Hi sorry to ask, does that means as long as there's ID the color will follow what's stated in the IDs, not class? I'm still new, still doing my freecodecamp.