YogeshChauhan . com

How to apply style only to first child and/or only to children other than the first child?

in CSS on August 7, 2020

CSS :first-child Selector

The :first-child selector is used to select the specified selector, only if it is the first child of its parent.

CSS Syntax


:first-child {
  css declarations;
}

Use this code to select and style every i element of every p element, where the p element is the first child of its parent:


p:first-child i {
  background: green;
}

To select and style the first li element in lists, use this code:


li:first-child {
  background: green;
}

If you want to select and style the first child element of every ul element, use this code:


ul > :first-child {
  background: green;
}

How to select all children other than the first one?

CSS Syntax


:not(:first-child) {
  css declarations;
}

Use this code to select and style every i element other than the first child of every p element. Same example as above but with NOT first child.


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

To select and style all the li elements other than the first in lists, use this code:


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

If you want to select and style all the children other than the first of every ul element, use this code:


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

All modern browsers where CSS selectors level 3 are supported. Read More on w3.org

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

4 ways to create Date Objects in JavaScriptJavaScriptHow to check if the element exists using JavaScript and jQuery?JavaScriptHow to Use SQL MAX() Function with Dates?SQL/MySQLIntroduction to components and templates Part 1: Component MetadataAngularHow to remove border from :visited image, link or button?CSSHow to implement NSNumberFormatter in Swift for currency?Swift