In this example using .gargoyle {...} would targer both the div and the figure.
Using div.gargoyle {...} would only target the div, it would not affect the figure with that class.
With CSS you can select by id, class or element tag name. CSS also allows you to use more than one of those selectors in combination to target an element by excluding spaces.
For example:
â.class1.class2â, âul.class1â
The more of those you use in combination the more specificity it will add to the rule. So it can also be used to override other styles.
.class1.class2 will override the styles in .class1
That was my first post
There is no visual difference just more specificity. The styles in .class1 will be overridden by the styles in div.class1
Edit:
There is documentation about it here https://developer.mozilla.org/en-US/docs/Web/CSS/Specificity
Suppose you want certain sections of your page to stand out, so you decide to add a background colour to those sections. You add a class and apply your style(s).
But now you want to add a border to the div containing the image. You canât add the rule to the .special class, because then it would be applied to the paragraph with that class, too. You canât simply style the div tag, because that would also add a border to the outer (container) div. So you need to write a rule which targets only a div with the class âspecialâ.
div.special {border: 2px solid #0000FF;}
If you wanted to target the paragraph instead, the rule would be
Thatâs a bad demo as you only have the special class on a div? Add the special class to a p tag also and perhaps another element and then see what the difference is.
At the risk of flogging the proverbial dead horse hereâs another demo that may be simpler to explain the difference.
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
div.special {
background:red;
}
</style>
</head>
<body>
<div class="special">I have a red background</div>
<p class="special">I don't have a red background</p>
</body>
</html>
So when you say div.special then the special class only applies to elements that are divs. If you say .special then the rule will apply to any element with the special class.
In the end though itâs a confusing practice and its better to stick with just .special only. If you want a div to have a different styling then use a different class and create some better logic with semantic class names.
In the end the less specificity you have the easier it is to maintain so avoid prefixing tag names to your classes. If you have a class called .special but you apply a different style by prefixing div, p, or whatever tag then you are just making the css more complex to maintain.
For your own code itâs easy enough to add another class value to an element. eg. <div class="special extraordinary supercalifragilisticexpialidocious">
The times I most often use selectors like div.special are when Iâm writing JavaScript code that works with HTML that is not my own (i.e. user scripts, browser extensions) and itâs easy to be more specific that it is to have JavaScript add a class value. Because I write more JavaScript than I design pages I do tend to be over specific in my own code if I donât catch myself doing it.
Overall, like much in web dev, I donât think it makes much difference for small single use code. The problems of not following best practice become more obvious when the code gets larger and more complex / you want to reuse it.