Apply CSS rules to a nested class inside a div

HtmlCss

Html Problem Overview


I don’t know exactly how to apply CSS to a nested element. Here is my example code, but I’m looking for a manual that explains all the rules:

<div id="content">
  <div id="main_text">
    <h2 class="title"></h2>
  </div>
</div>

How can I apply CSS to only the class title, nested inside that particular div?

Html Solutions


Solution 1 - Html

You use

#main_text .title {
  /* Properties */
}

If you just put a space between the selectors, styles will apply to all children (and children of children) of the first. So in this case, any child element of #main_text with the class name title. If you use > instead of a space, it will only select the direct child of the element, and not children of children, e.g.:

#main_text > .title {
  /* Properties */
}

Either will work in this case, but the first is more typically used.

Solution 2 - Html

> Use Css Selector for this, or learn more about Css Selector just go here https://www.w3schools.com/cssref/css_selectors.asp

#main_text > .title {
  /* Style goes here */
}

#main_text .title {
  /* Style goes here */
}

Solution 3 - Html

If you need to target multiple classes use:

#main_text .title, #main_text .title2 {
  /* Properties */
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMazzyView Question on Stackoverflow
Solution 1 - HtmlWill P.View Answer on Stackoverflow
Solution 2 - HtmlSunil R.View Answer on Stackoverflow
Solution 3 - HtmlCiprianView Answer on Stackoverflow