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

Rule Sets


In CSS, Rule Sets are the fundamental building blocks that define how HTML elements are styled. Each rule set consists of a selector, which targets the HTML element(s) to be styled, and a declaration block, which contains one or more declarations specifying the styles to be applied.


Example 1: Basic Paragraph Styling

p { /* Selects all <p> (paragraph) elements */
  color: blue; /* Sets the text color to blue */
  font-size: 16px; /* Sets the font size to 16 pixels */
}

Explanation This example demonstrates a basic rule set targeting all <p> elements. It sets their text color to blue and their font size to 16 pixels, showcasing how to apply consistent styles across similar elements.


Example 2: Styling a Specific ID

#header { /* Selects the element with the ID "header" */
  background-color: lightgray; /* Sets the background color to light gray */
  padding: 20px; /* Adds 20 pixels of padding around the content */
}

Explanation Here, a rule set targets a unique HTML element using its ID. This allows for specific styling of a single element, providing precise control over its appearance.


Example 3: Styling Elements by Class

.card { /* Selects all elements with the class "card" */
  border: 1px solid #ccc; /* Adds a 1-pixel solid border with a light gray color */
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.1); /* Adds a subtle shadow effect */
}

Explanation This example uses a class selector to apply styles to multiple HTML elements that share the same class. This is ideal for reusable styles across various components on a webpage.


Example 4: Grouping Selectors

h1, h2, h3 { /* Selects all <h1>, <h2>, and <h3> elements */
  font-family: Arial, sans-serif; /* Sets the font family to Arial or a generic sans-serif */
  text-align: center; /* Centers the text horizontally */
}

Explanation In this rule set, multiple selectors are grouped to apply the same styles to different HTML elements simultaneously. This reduces code redundancy and improves maintainability.


Example 5: Descendant Selector

ul li { /* Selects all <li> (list item) elements that are descendants of a <ul> (unordered list) */
  list-style-type: square; /* Changes the list item marker to a square */
  margin-bottom: 5px; /* Adds a bottom margin of 5 pixels to each list item */
}

Explanation This example uses a descendant selector to target <li> elements specifically within <ul> elements. This provides more granular control, allowing for precise styling of nested elements in your web layout.