CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

:Last Child


The :last-child pseudo-class works just like :first-child, but it targets an element that is the last child of its parent. This is perfect for adjusting spacing or adding borders to the final element in a group.

Example 1: Removing Bottom Margin from the Last Paragraph

/* Selects the last <p> element inside its parent */
p:last-child {
  margin-bottom: 0;
}

Explanation

This CSS snippet removes the bottom margin from the last paragraph element within any parent container, preventing extra space at the end of a block of text.


Example 2: Styling the Last List Item

/* Targets the last <li> in any list */
ul > li:last-child {
  border-bottom: none;
}

Explanation

This code removes the bottom border from the last list item (<li>) in any unordered list, creating a clean, finished look.


Example 3: Adding a Footer to the Last Section

/* Applies a border to the bottom of the last section */
section:last-child {
  border-bottom: 1px solid #ddd;
  padding-bottom: 15px;
}

Explanation

Here, the last <section> element on the page gets a bottom border and some padding, visually separating it from any content that follows.


Example 4: Highlighting the Last Column of a Table

/* Styles the last <td> in every table row <tr> */
tr td:last-child {
  text-align: right;
  font-style: italic;
}

Explanation

This CSS targets the last table data cell (<td>) in each table row, right-aligning the text and making it italic, often used for summary figures.


Example 5: A Call to Action in the Last Div

/* Styles the last <div> inside a container with the ID 'cta' */
#cta div:last-child {
  background-color: #ffc107;
  padding: 10px;
}

Explanation

This rule selects the last <div> inside an element with the ID #cta and gives it a prominent background color and padding to draw attention.