I define the CSS rule:
.info-specs h2, h3, h4, h5 {
font-size: 1.5em;
text-transform: none;
}
This should only apply to h5 within an element with class "info-specs". However, after inspecting, I find other h5 element is also using this rule. Why?
Below is a sample:
.info-specs h2, h3, h4, h5 {
font-size:5em;
text-transform: none;
}
<h5>mytest </h5>
6
Answers
You’re applying class to .info-specs h2 and to h3 and to h4 and to h5.
It’s not equal to .info-specs h2, .info-specs h3, .info-specs h4, .info-specs h5 where all H’s within .info-specs would be targeted, but not H6 for instance.
Read more about grouping_selector and the following paragraph about ‘combinators’.
You have applied to CSS for all classes and elements mentioned. This is the way you can say that the following rule should apply to chosen classes and elements.
if you want to use your CSS inside a particular class you just need to use the ">" selector in CSS.
try this:
https://www.w3schools.com/cssref/css_selectors.php
Your code simply means you applying css to .info-specs h2, h3, h4 and h5 and not to the .info-specs h5, but the code you provided do not contain other heading tags other than h5 or maybe you did not provided the full code, its unclear what you want to achieve but the problem will be fixed putting h5 tag at first,
The CSS interpreter of your browser is going to look for any
h3
,h4
andh5
elements and only forh2
it’s going to look if it is inside a.info-specs
. The comma or grouping selector treats everything seperated by the comma as separate selections.Possible solutions to your issue are:
DOC
To achieve the desired behavior of applying the styles only to h5 elements within an element with the "info-specs" class, you need to explicitly specify the class selector for each element. Here’s the corrected CSS rule:
With this updated rule, the styles will only be applied to h5 elements that are descendants of an element with the "info-specs" class, and not to other h5 elements that dont have that class.