YogeshChauhan . com

How to apply style to a specific child element using CSS?

in CSS on October 15, 2020

Similar Post: How To Apply Style Only To First Child And/Or Only To Children Other Than The First Child?

We can use different selectors in CSS to style specific child elements.

Apply style to the first child element


li:first-child {
  background: green;
}

Apply style to the last child element


li:last-child {
    background: green;
}

Apply style to the second last child element


li:nth-last-child(2) {
    background: green;
}

Apply style to nth child element


// 2nd child

p:nth-child(2) {
  background: red;
}

// 4th child 

p:nth-child(4) {
  background: red;
}

Apply style to all child elements other than the first one


li:not(:first-child) {
  background: green;
}

Apply style to all child elements other than the first two


li:nth-child(n+2) {
    background: green;   
}

Apply style to all odd child elements


li:nth-child(odd) {
    background: green;   
}

 Apply style to all even child elements


li:nth-child(even) {
    background: green;   
}

Apply style to first two child elements


li:nth-child(-n+2) {
    background: green;   
}

Apply style to all but skip the first two child elements


li:nth-child(n+2) {
    background: green;   
}
amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

How to add Google Map in WordPress using ACF?WordPressWhat Does Host-Based Intrusion Detection System (HIDS) Mean and What Are Some Advantages Over NIDS?MiscHAVING Clause in Postgres with Examples in All Aggregate FunctionsPostgresHow to use a Subquery to Insert Multiple Rows in SQL Table?SQL/MySQLSQL ANY OperatorSQL/MySQLtransform-origin Property in CSSCSS