Yogesh Chauhan's Blog

The :last-of-type selector

in CSS on September 23, 2020

The :last-of-type selects the element that is the last child, of a particular type, of its parent.

For e.g., in the code below the css selects the last paragraph and the last heading h2.


<!DOCTYPE html>
<html>
<head>
<style> 
p:last-of-type {
 background: #C5CAE9;
}

h2:last-of-type {
 background: #CDDC39;
}
</style>
</head>

<body>

<div class="container">
 <p>1st paragraph</p>
 <p>2bd paragraph</p>
 <p>last paragraph</p>
 <h1>1st Heading</h1>
 <h2>1st heading</h2>
 <h2>last heading</h2>
</div>

</body>
</html>

 


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Order By and Group By in PostgresPostgres3 ways to pass a variable in url() function in SCSS (Sass)SCSSPHP __construct() functionPHPHow to disable right click and drag and drop of images using jQuery?jQueryHow to create a multisite network in WordPress?WordPressAccessing and Setting features of JavaScript ObjectsJavaScriptHow to create a cross-browser smooth scrolling with jQuery?jQueryWindow setTimeout() Method in JavaScriptJavaScriptHow to add transparent text on top of an image using CSS?CSSWhat is HTMLUnknownElement?HTMLSELF JOIN in PostgresPostgresCheck if any column has NULL values in PostgresPostgres