YogeshChauhan.com
How to apply style only to first child and/or only to children other than the first child?
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

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 5 How to create flickering text using CSS? Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress?

You might also like these

How to insert Bootstrap 4 in Angular 9 app?AngularJavaScript String fromCharCode() MethodJavaScriptSolution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software”MiscSQL ANY OperatorSQL/MySQLEXISTS and NOT EXISTS in PostgresPostgresHow to Use Aggregate Functions (MIN, MAX, SUM, AVG, COUNT) to Summarize Data in SQL?SQL/MySQL