How to Select all p Elements Inside a div Element in CSS?

To select all p elements inside a div element, you can use the descendent selector. The descendant selector allows you to select and style all descendants of an element.

To select all the descendants of an element, you have to simply put white space between the parent and the child. For example, div p, ul li, etc.

See this working example:

Example:

div p{
   background: yellow;
   padding: 5px;
}

Similarly, you can select all <li> that are inside of a <ul> or <ol>.

See this example:

Example:

ul li{
   background: yellow;
   margin: 5px;
}

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.