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

dreamhost

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 uninstall Cocoapods from the Mac OS? #4 How to add Read More Read Less Button using JavaScript? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

May 5 Use inline if to make a shorter conditional syntax in React May 4 What is Conditional Rendering in React? May 3 How does @extend rule work in SCSS (Sass)? May 2 How different is Handling Events in React vs HTML DOM? May 2 How to create bouncing balls using HTML canvas and JavaScript? Apr 30 HTML canvas methods

You might also like these

What’s a repository on Github?MiscWhat’s a Strict mode in JavaScript?JavaScriptStyling Lists with CSSCSSHow to convert datetime to date format in JavaScript?JavaScriptQuick Introduction to 11 Array Iteration Methods in JavaScriptJavaScriptINNER JOIN in PostgresPostgres