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

Reset/Normalize


Browser vendors apply their own default styles (user-agent stylesheets) to HTML elements. These defaults can vary significantly, leading to inconsistent rendering of your web pages across browsers. CSS resets and normalization techniques aim to address these inconsistencies, providing a consistent baseline for your custom styles.

Why and when to use them

Using a CSS reset or normalize stylesheet helps create a predictable starting point for your designs. Without them, your meticulously crafted styles might appear differently in Chrome, Firefox, or Safari, leading to frustrating debugging. They are essential for achieving cross-browser consistency and reducing development headaches. You should use one at the beginning of every new project to ensure a "clean slate" or a normalized foundation.

Example 1: Basic Universal Reset

/* A simple universal reset: removes default margin and padding, and sets box-sizing */
/* This is a common starting point for many modern projects. */
*,
*::before,
*::after {
  box-sizing: border-box; /* Crucial for predictable sizing */
  margin: 0; /* Removes default margins */
  padding: 0; /* Removes default paddings */
}

Explanation This universal selector (*) with ::before and ::after pseudo-elements applies box-sizing: border-box; and zeroes out margin and padding for all elements. This creates a clean slate where you explicitly control spacing, preventing browser defaults from interfering.


Example 2: Normalize.css (Conceptual)

/*
 * normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css
 * This is a conceptual representation of how Normalize.css would be included.
 * You would typically link to the actual file from a CDN or local directory.
 */
/* @import 'normalize.css'; */ /* If using a module bundler or preprocessor */

/* Or in HTML: */
/* <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css"> */

/* Normalize.css typically corrects specific browser inconsistencies, e.g.: */
body {
  margin: 0; /* Ensures body has no default margin */
}

h1 {
  font-size: 2em; /* Keeps h1 larger but normalizes its size across browsers */
  margin: 0.67em 0; /* Normalizes h1 margin */
}

Explanation Normalize.css is a popular choice that makes browser rendering more consistent by correcting only the inconsistencies while preserving useful defaults. It's less "destructive" than a full reset, meaning elements like headings will still have a default larger font size, but it will be consistent across browsers.


Example 3: Modern CSS Reset (Josh Comeau's Approach - Simplified)

/*
 * A simplified snippet from a modern custom CSS reset.
 * These resets often target specific issues with contemporary browser defaults.
 */
/* Removes default styling from form elements for consistent control */
button,
input,
optgroup,
select,
textarea {
  font-family: inherit; /* Ensures form elements inherit font */
  font-size: 100%; /* Ensures form elements respect font size */
  line-height: 1.15; /* Normalizes line height for form elements */
  margin: 0; /* Removes default margins */
}

/* Make images responsive by default */
img,
picture,
video,
canvas,
svg {
  display: block; /* Prevents extra space below images */
  max-width: 100%; /* Ensures images don't overflow their containers */
}

Explanation Modern CSS resets often take a more opinionated approach, specifically targeting common issues with form elements or ensuring images are responsive by default. They are tailored for contemporary web development practices.


Example 4: When to Choose Normalize over a Full Reset

/*
 * This example highlights why Normalize.css might be preferred for certain projects.
 * Imagine you want consistent heading sizes but don't want to redefine them from scratch.
 */
/* With Normalize.css included: */
h1 {
  /* You can now confidently adjust h1 knowing its base is consistent */
  color: #333; /* Dark gray color for headings */
  text-decoration: underline; /* Add a custom underline */
}

/* If using a full reset, you'd need to re-add basic sizing: */
/* h1 { font-size: 2em; margin-bottom: 0.67em; } */

Explanation If your design incorporates many standard HTML elements and you want to maintain some of their inherent browser-provided styling (like h1 being naturally larger than h2), Normalize.css is a great choice. It focuses on making these defaults consistent rather than removing them entirely.


Example 5: Combining a Minimal Reset with Custom Base Styles

/* A practical approach: a very minimal reset combined with your own base styles */

/* Minimal Reset: Basic box-sizing and overflow */
*,
*::before,
*::after {
  box-sizing: border-box;
}

/* Global body styles for font and background */
body {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; /* Modern sans-serif font stack */
  line-height: 1.5; /* Good reading line height */
  -webkit-font-smoothing: antialiased; /* Improves font rendering on macOS/iOS */
  margin: 0; /* Ensure no default body margin */
}

/* Consistent heading styles */
h1, h2, h3, h4, h5, h6 {
  margin-top: 0; /* Remove top margin */
  margin-bottom: 0.5em; /* Consistent bottom margin */
  line-height: 1.2; /* Tighter line height for headings */
}

Explanation Many developers opt for a very minimal custom reset, focusing on box-sizing and then building their own base styles for elements like body and headings. This gives maximum control and ensures only necessary resets are applied, keeping the CSS lean and optimized.