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

Selectors


CSS syntax is the set of rules browsers use to interpret stylesheets. It consists of rule sets, each containing a selector and a declaration block.

/* This is a comment in CSS */
p { /* 'p' is the selector, targeting all paragraph elements */
  color: blue; /* 'color' is the property, 'blue' is the value */
  font-size: 16px; /* 'font-size' is the property, '16px' is the value */
}

Explanation The example demonstrates a CSS rule targeting all <p> (paragraph) elements. It sets their text color to blue and font-size to 16 pixels. Each declaration ends with a semicolon.


Selectors (Type, Class, ID, Universal, Grouping)

CSS selectors are patterns used to select the HTML elements you want to style. They specify which elements a CSS rule applies to.

Example 1: Type Selector

/* Selects all <h1> elements */
h1 {
  color: #333; /* Sets the text color to a dark gray */
}

Explanation This CSS rule uses a type selector to target and style all instances of the <h1> HTML element, setting their text color.


Example 2: Class Selector

/* Selects all elements with the class "highlight" */
.highlight {
  background-color: yellow; /* Sets the background color to yellow */
  font-weight: bold; /* Makes the text bold */
}

Explanation The .highlight class selector styles any HTML element that has the class="highlight" attribute, making its background yellow and text bold.


Example 3: ID Selector

/* Selects the element with the ID "main-header" */
#main-header {
  text-align: center; /* Centers the text */
}

Explanation The #main-header ID selector uniquely targets a single HTML element with id="main-header", typically used for distinct page elements.


Example 4: Universal Selector

/* Selects all elements on the page */
* {
  margin: 0; /* Resets default outer spacing for all elements */
  padding: 0; /* Resets default inner spacing for all elements */
}

Explanation The * universal selector applies the defined styles to every single element within the HTML document, often used for global resets.


Example 5: Grouping Selector

/* Selects all <h1>, <h2>, and <h3> elements */
h1, h2, h3 {
  font-family: Arial, sans-serif; /* Sets a common font family */
}

Explanation The grouping selector h1, h2, h3 applies the same font family style to all <h1>, <h2>, and <h3> elements, promoting consistent typography.