How to Target a CSS Class Inside Another CSS Class

Classes play a crucial role when discussing the specification of any element in CSS or any other programming language. To express a few styles and effects on the HTML components, classes are generated in CSS. By giving the property values, all of the CSS properties can be added to the class body.

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:

<div class="main-content">

<div class="table">

<div class="row">

<div class="c-left">Marry</div>

<div class="c-right">Jack</div>

<div class="c-left">Tom</div>

<div class="c-right">Jully</div>





Step 3: Apply Styling on Main “div” Container

Access the main “div” container with the help of class name as “.main-content”:

.main-content {

width: 40%;

margin: 0 auto;

color: blue;

border: 2px dotted blue;

text-align: center;



  • 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:

.main-content .table {

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:

.main-content .c-right {

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:

.main-content .c-left {

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

.main .c-right {

width: auto;

font-size: 15px;

color: #fff;

margin-right: 20px;

font-weight: normal;



That’s all about targeting a CSS class inside another CSS class.


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.

About the author

Hafsa Javed