Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

#edpressoworkshop
css

How to apply style to parent if it has a child with CSS

Varshana Gopal

Introduction

In this shot, we will learn how to apply style to a parent class that has a child class with CSS.

It’s easy to apply style to a child element, but if you want to apply style to a parent class that already has child elements, you can use the CSS selector child combinators (>), which are placed between two CSS selectors. For example, div > p selects all <p> elements where the parent is a <div> element.

Syntax

selector1 > selector2{
  style code
}

Selector 1 is the parent element and selector 2 is the child element.

In the example above, .div2 is the child element of .div1. The style can also be applied in a CSS file which will then have to be linked to the HTML file. However, this applies to immediate children. For nested children, we’ll have to use the descendant combinator.

Browser Compatibility

7* : Before Internet Explorer 10, the combinator only works in standards mode.

RELATED TAGS

#edpressoworkshop
css
RELATED COURSES

View all Courses

Keep Exploring