This post will state about targeting a CSS class within another CSS class.
How to Target CSS Class Within Another CSS Class?
To target a CSS class inside another CSS class, first, make div containers and add class attributes in each container. Then, access one or more classes in CSS by utilizing their name/value.
Step 1: Add a “div” Container
Initially, add a div element with the help of “<div>”. Then, allocate a class attribute for further use.
Step 2: Make Nested “div” Containers
Next, make nested div containers by following the same procedure of step 1:
Output
Step 3: Apply Styling on Main “div” Container
Access the main “div” container with the help of class name as “.main-content”:
width: 40%;
margin: 0 auto;
color: blue;
border: 2px dotted blue;
text-align: center;
}
Here:
- “width” specifies the element’s width size.
- “margin” allocates space around the element outside the defined border.
- “color” defines the color for the added text in the element.
- “border” defines an outline or boundary around the element in HTML.
- “Text-align” defines the alignment of the element’s text.
Step 4: Style another class
Access the CSS main class and other nested classes by using their names. Then, set the width of the container by specifying the value according to your choice:
width: 80%;
}
Furthermore, access the other class by following the same procedure as above and apply the CSS properties mentioned in the below code snippet:
display: inline-block;
font-size: 20px;
}
According to the above code snippet:
- “display” property is used for setting the display of an element.
- “font-size” specifies the size of the text added in the container.
Now, access the other classes by utilizing the same method and apply the following CSS properties as mentioned below:
width: 140px;
margin-right: 6px;
font-size: 16px;
}
To do so, we will apply “width”, “margin-right” and “font-size” for styling purposes.
Moreover, access the main “div” container along other nested div container by utilizing their class name and apply the following CSS properties
width: auto;
font-size: 15px;
color: #fff;
margin-right: 20px;
font-weight: normal;
}
Output
That’s all about targeting a CSS class inside another CSS class.
Conclusion
To target a CSS class within another CSS class, first, access the main “div” through the assigned class attribute. Then, access another “div” container by following the same procedure. Furthermore, users can target a CSS class within other CSS class. This post has demonstrated the method for targeting a CSS class within another CSS class.