
- CSS Tutorial
- CSS - Home
- CSS - Introduction
- CSS - Syntax
- CSS - Inclusion
- CSS - Measurement Units
- CSS - Colors
- CSS - Backgrounds
- CSS - Fonts
- CSS - Text
- CSS - Images
- CSS - Links
- CSS - Tables
- CSS - Borders
- CSS - Margins
- CSS - Lists
- CSS - Padding
- CSS - Cursors
- CSS - Outlines
- CSS - Dimension
- CSS - Scrollbars
- CSS Advanced
- CSS - Visibility
- CSS - Positioning
- CSS - Layers
- CSS - Pseudo Classes
- CSS - Pseudo Elements
- CSS - @ Rules
- CSS - Text Effects
- CSS - Media Types
- CSS - Paged Media
- CSS - Aural Media
- CSS - Printing
- CSS - Layouts
- CSS - Validations
- CSS3 Tutorial
- CSS3 - Tutorial
- CSS3 - Rounded Corner
- CSS3 - Border Images
- CSS3 - Multi Background
- CSS3 - Color
- CSS3 - Gradients
- CSS3 - Shadow
- CSS3 - Text
- CSS3 - Web font
- CSS3 - 2d transform
- CSS3 - 3d transform
- CSS3 - Animation
- CSS3 - Multi columns
- CSS3 - User Interface
- CSS3 - Box Sizing
- CSS Responsive
- CSS - Responsive Web Design
- CSS References
- CSS - Questions and Answers
- CSS - Quick Guide
- CSS - References
- CSS - Color References
- CSS - Web browser References
- CSS - Web safe fonts
- CSS - Units
- CSS - Animation
- CSS Resources
- CSS - Useful Resources
- CSS - Discussion
The :first-child Pseudo-class in CSS
The CSS :first-child pseudo-class selects an element that is the first child element of some other element.
Syntax
Following is the syntax −
:first-child{ /*declarations*/ }
Example
Let’s see an example of CSS first-child Pseudo class −
<!DOCTYPE html> <html> <head> <style> table { margin: auto; padding: 10px; border: hsl(54, 100%, 50%) solid 13px; border-radius: 6px; text-align: center; } td, th { border-left: 2px solid black; border-top: 3px solid black; } td:first-child, th:first-child { border-left: none; } th { background-color: lightblue; border-top: none; } caption { margin-top: 3px; background-color: purple; caption-side: bottom; color: white; border-radius: 20%; } </style> </head> <body> <table> <caption>MCA Syllabus</caption> <tr> <th colspan="4">Subjects</th> </tr> <tr> <td>C</td> <td>C++</td> <td>Java</td> <td>C#</td> </tr> <tr> <td>MySQL</td> <td>PostgreSQL</td> <td>MongoDB</td> <td>SQLite</td> </tr> </table> </body> </html>
Output
This will produce the following output −
Before using first-child pseudo class selector −
After using first-child pseudo class selector −
Example
Let’s see another example of CSS first-child Pseudo class −
<!DOCTYPE html> <html> <head> <style> * { font-size: 1.1em; list-style: circle; } li:first-child { background-color: seashell; font-family: cursive; } li:nth-child(2) { background-color: azure; font-family: "Brush Script Std", serif; } li:last-child { background-color: springgreen; font-family: "Gigi", Arial; } </style> </head> <body> <h2>Apache Spark</h2> <ul> <li>Apache Spark is a lightning-fast cluster computing technology, designed for fast computation. </li> <li>It is based on Hadoop MapReduce.</li> <li>It extends the MapReduce model to efficiently use it for more types of computations. </li> </ul> </body> </html>
Output
This will produce the following output −
- Related Articles
- Usage of :first-child pseudo-class in CSS
- The :last-child Pseudo-class in CSS
- The :nth-child Pseudo-class in CSS
- The :lang Pseudo-class in CSS
- :active pseudo class in CSS
- visited pseudo class in CSS
- The ::first-line Pseudo-element in CSS
- The ::first-letter Pseudo-element in CSS
- Difference Between Pseudo-Class and Pseudo-Element in CSS
- CSS :first-letter pseudo-element
- What is Pseudo-class in CSS
- CSS :focus-within pseudo class
- Usage of :link pseudo-class in CSS
- Usage of :visited pseudo-class in CSS
- Usage of :hover pseudo-class in CSS

Advertisements