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

Inheritance


CSS inheritance is a fundamental mechanism where certain CSS properties applied to a parent element are automatically passed down to its child elements. This cascade simplifies styling and promotes efficient code, reducing the need to explicitly style every element. Understanding inheritance is key to effective CSS architecture.

Example 1: Inherited Text Properties

/* Body font-family and color will inherit to all text elements within it */
body {
  font-family: 'Arial', sans-serif; /* Sets a default font for the whole document */
  color: #333; /* Dark gray text color */
}

div {
  /* This div's text will inherit Arial and #333 color from body */
  background-color: #f0f0f0;
  padding: 15px;
}

p {
  /* This paragraph will also inherit font-family and color */
  font-size: 16px; /* Only font-size is explicitly set here */
}

Explanation In this example, font-family and color applied to the body element are inherited by the div and p elements. This means you don't need to explicitly set these properties on every text element, saving code and ensuring consistency.


Example 2: Non-Inherited Properties (e.g., border)

/* Border property is not inherited */
.parent-box {
  width: 200px;
  height: 100px;
  border: 2px solid blue; /* This border will not pass to children */
  background-color: lightblue;
}

.child-box {
  width: 50%; /* This is relative to parent's width */
  height: 50%;
  background-color: lightgreen;
  /* No border property explicitly set, so no border will appear from parent */
}

Explanation Properties like border, margin, and padding are generally not inherited. This allows for precise control over an element's individual box model without affecting its descendants, which is crucial for layout.


Example 3: Using the inherit keyword

/* Explicitly inheriting a property that normally doesn't inherit */
.main-container {
  border: 3px dashed purple; /* Parent border */
  padding: 20px;
}

.inherited-border-child {
  /* Normally border wouldn't inherit, but using 'inherit' forces it */
  border: inherit; /* This child will get the purple dashed border */
  padding: 10px;
  background-color: #eee;
}

Explanation The inherit keyword explicitly forces a property to take on the computed value of its parent element, even if that property is not typically inherited. This offers fine-grained control when you need to override default inheritance behavior.


Example 4: Inheriting from initial vs. inherit

/* Demonstrating the difference between 'initial' and 'inherit' */
.parent-text {
  font-size: 24px;
  color: darkred;
}

.child-reset-initial {
  /* 'initial' sets the property to its default browser value (e.g., black) */
  color: initial; /* Resets color to browser's default for text */
}

.child-inherit-color {
  /* 'inherit' makes it take the parent's value (darkred) */
  color: inherit; /* Explicitly inherits darkred from parent */
}

Explanation initial resets a property to its browser default, while inherit specifically copies the computed value from the parent. This distinction is vital for accurately controlling how styles cascade through your document.


Example 5: Global Application with Inheritance

/* Setting a base font-size and line-height on the root HTML element */
html {
  font-size: 16px; /* Base font size for 'rem' units */
  line-height: 1.5; /* Default line spacing for all text */
  box-sizing: border-box; /* Important for global box model consistency */
}

body {
  /* Body will automatically inherit font-size and line-height from html */
  margin: 0;
  padding: 0;
  font-family: 'Verdana', sans-serif; /* This overrides font-family if set on html */
}

article p {
  /* Paragraphs in articles will inherit line-height and font-size (unless overridden) */
  margin-bottom: 1em;
}

Explanation Setting properties like font-size and line-height on the html or body element leverages inheritance to establish global defaults. This ensures consistent typography across your entire website, making maintenance and responsive adjustments simpler.