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

:Nth Child


The :nth-child() pseudo-class is a more flexible selector that targets elements based on their position using a number, keyword (odd or even), or a formula. This allows for complex striping patterns and other advanced selections.

Example 1: Styling Even and Odd Rows

/* Styles even and odd list items differently */
li:nth-child(odd) {
  background-color: #f9f9f9;
}

li:nth-child(even) {
  background-color: #eee;
}

Explanation

This code creates a "zebra-striped" effect on a list by applying a slightly different background color to odd and even list items.


Example 2: Highlighting a Specific Item

/* Targets the third list item */
li:nth-child(3) {
  font-weight: bold;
}

Explanation

This CSS selects only the third list item (<li>) in a list and makes its text bold, useful for calling out a specific item.


Example 3: Styling in Groups

/* Styles every third element starting from the first */
.gallery-item:nth-child(3n+1) {
  clear: left;
}

Explanation

This rule targets gallery items with the class .gallery-item in a pattern of every third item, starting with the first one (1, 4, 7, etc.). It's often used to clear floats in a grid layout.


Example 4: Selecting the First Few Elements

/* Targets the first three <div> elements */
div:nth-child(-n+3) {
  border: 1px solid #007bff;
}

Explanation

This CSS applies a blue border to the first three <div> elements within a parent container. The formula -n+3 selects all elements from the first up to the third.


Example 5: Skipping the First Few Elements

/* Targets all list items except the first two */
li:nth-child(n+3) {
  display: none;
}

Explanation

This code hides all list items (<li>) starting from the third one. The formula n+3 selects all elements from the third to the end.