So here I've got a Code Ply again with no frameworks, so pure CSS, and we've got a h1 that has an id 1
of heading as well as a class of title and it just says Hello World. 2
Now over here there's many ways that I can change it's style, 3
right? 4
So I can say, for example, h1 color red, and if I hit run then of course it will turn red. 5
But if you accidentally try to change the same property twice, 6
say, for example, if I said now color is green, then the last CSS rule change will be the one that will 7
be carried out. 8
So if I hit run this is now going to be green instead of red. 9
So basically the process is, changes it red, and then it changes it 10
green. It's reading the code from top to bottom. 11
It's not very clever here. 12
So in this case the last CSS rule has priority over everything above it. 13
Now as we said before I can either use the h1 which is an HTML element selector to change the color 14
or I can use the class which is title to change the color. 15
So let's change it to yellow. 16
Now a class is more specific than an HTML element. 17
So, think about it, on your web page you're probably going to have more HTML elements than classes. 18
So, for example, if you had a whole bunch of paragraphs, they're probably all going to have different classes, 19
so it makes sense to make the class selectors have higher priority, because they are more specific. 20
So if I hit run now, you'll see that the Hello World is now yellow instead of red because this has a higher 21
priority. Now 22
finally, if I decided to change the color by using the id, so let's target that id, heading, and change the 23
color to blue and I hit run you'll see that this has the highest priority of all. 24
And our Hello World will display in blue rather than any other color. 25
And if we take a look at the Chrome Developer Tools over here, you can see that all our colors are being 26
applied but they're just being overridden by something with a higher priority. 27
So that means that if this color change wasn't applied in the id, so if we deleted this line of code, then it 28
would go to the next highest priority 29
CSS rule, which is the class, 30
and finally it would go to the h1. 31
So, knowing what we know about how specific CSS rules are, for example we know that inline styles are 32
more specific than say internal or external CSS styles, 33
so if we change the color here to, 34
I'm running out of colors here, orange, and we hit run, 35
you can see that this is even higher priority than the ids and that is overriding all of our external 36
stylesheet. 37
So it can get pretty confusing very quickly. 38
And also if you're writing a lot of CSS code, very often you're going to get into the situation where 39
you start writing conflicting rules like this bunch we've got over here. 40
So this ends up being a lot of code that's applied to the same thing, well applied three times basically, 41
and the last one standing gets carried through in the end. 42
But this is very inefficient and it's also very very bug prone. 43
So how can you prevent creating conflicting rules? 44
Well, the first thing to remember is, use ids very very sparingly. 45
Don't try and use it when you can use a class. 46
So, for example, in our case we've really only got ids for our sections, and, in part, that's because it 47
helps us with our navigation. 48
So just because you only have one of something isn't good enough to give it an id instead of a class. 49
So, for example, if I wanted to change the style of our copyright text, then even though we're only going 50
to have one copyright text on our page, it's not a good enough reason to use an id rather than using 51
a class. 52
A class is perfectly good enough in this case. 53
So use ids really really sparingly, and I tend to recommend to only use it really for those sections 54
and parts of your code that definitely need an id, 55
for example, if you're working with Bootstrap carousels or Bootstrap elements, then they do need an 56
id to target from the href. They need that navigational ability of the id, and that is a case where you 57
might consider using it, but always first consider using class instead of going straight to an id, even 58
if it only appears once. 59
The other thing to remember is that, when you're applying classes to your elements, to only use one class. I 60
know that it seems a little bit weird because we're using Bootstrap and Bootstrap loves it's multiple 61
classes, which irks a lot of people. 62
But in terms of the CSS classes that youre applying yourself, for example in this case the h1 has a 63
class of big-heading, and it wasn't something like big and heading and, I don't know, white, right, where 64
we have different styles for each of these classes and together they make up the big-heading. 65
This is really bad practice because it's very very easy to get into conflicts that way so try to keep 66
it specific and try to apply only a single custom class to each of your elements. 67
And the other thing is avoid inline styles at all costs. 68
This is really really bad 69
CSS coding practice. 70
There are no cases where your inline style can not be solved by using the external stylesheet. 71
So this is just pure laziness and it's really really bad practice. 72
So don't do that. 73
So those are just a few bits of good practice to incorporate into your workflow so that you avoid conflicts 74
and you have less problems debugging your code later on.