Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply CSS rules to a nested class inside a div

Tags:

html

css

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?

like image 601
Mazzy Avatar asked Feb 08 '12 17:02

Mazzy


People also ask

How do I style a nested class in CSS?

Nested rules are defined as a set of CSS properties that allow the properties of one class to be used for another class and contain the class name as its property. In LESS, you can use class or ID selectors to declare mixin in the same way as CSS styles.

How do I style a div inside a div in CSS?

To move the inner div container to the centre of the parent div we have to use the margin property of style attribute. We can adjust the space around any HTML element by this margin property just by providing desired values to it. Now here comes the role of this property in adjusting the inner div.

How do you style a class within a class in CSS?

class selector selects elements with a specific class attribute. To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class.

Does CSS support nesting of classes?

When we use a CSS preprocessor like Sass or Less, we can nest a CSS style rule within another rule to write clean and understandable code. This nesting rule is not supported yet in native CSS. At the moment, it is a working draft and only available for discussion.


1 Answers

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.

like image 161
Will P. Avatar answered Oct 05 '22 22:10

Will P.