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

Defining variables


Defining variables (--variable-name: value;)

CSS custom properties, more commonly known as CSS variables, allow you to store specific values for reuse throughout your stylesheets. By defining a variable in a central place, you can easily manage and update values like colors, fonts, or sizes, which promotes cleaner and more maintainable code. To declare a global variable, you define it within the :root pseudo-class, making it accessible across the entire document.


Example 1: Basic Color Variable

/* Define a global variable for the primary color */
:root {
  --primary-color: #3498db;
}

/* Use the variable for a button's background color */
.button {
  background-color: var(--primary-color);
  color: white;
  padding: 10px 20px;
}

Explanation: This code defines a global variable named --primary-color with a blue value. The .button class then uses this variable for its background-color, ensuring brand consistency.


Example 2: Font Size Variable

/* Define a global variable for the base font size */
:root {
  --base-font-size: 16px;
}

/* Apply the font size to the body */
body {
  font-size: var(--base-font-size);
}

/* Use a calculated value for a larger heading */
h1 {
  font-size: calc(var(--base-font-size) * 2);
}

Explanation: Here, --base-font-size is set to 16px. This variable is then applied directly to the body and used in a calc() function to make the h1 element twice the base size.


Example 3: Spacing Variable

/* Define a global variable for standard spacing */
:root {
  --standard-spacing: 15px;
}

/* Use the variable for margin on a container */
.container {
  margin: var(--standard-spacing);
}

/* Use the variable for padding on a card */
.card {
  padding: var(--standard-spacing);
}

Explanation: This example establishes a consistent spacing unit with the --standard-spacing variable. It's then applied to both the margin of a container and the padding of a card for a uniform layout.


Example 4: Border Style Variable

/* Define a global variable for a common border style */
:root {
  --default-border: 1px solid #ccc;
}

/* Apply the border to an input field */
input {
  border: var(--default-border);
}

/* Apply the border to an image */
img {
  border: var(--default-border);
}

Explanation: The --default-border variable stores a complete border definition. This makes it easy to apply a consistent border style to multiple elements like input fields and img tags.


Example 5: Background Color Variable

/* Define a global variable for the page's background color */
:root {
  --background-color: #f4f4f4;
}

/* Apply the background color to the body */
body {
  background-color: var(--background-color);
}

Explanation: A variable named --background-color is created to hold the background color for the page. This allows for quick and easy theme changes by updating a single line of code.


Example 6: Box Shadow Variable

/* Define a global variable for a subtle box shadow */
:root {
  --box-shadow-light: 0 2px 4px rgba(0,0,0,0.1);
}

/* Apply the box shadow to a card component */
.card {
  box-shadow: var(--box-shadow-light);
}

Explanation: This code defines --box-shadow-light to store a reusable box-shadow value. Applying this variable to elements like .card ensures a consistent shadow effect across the site.


Example 7: Overriding a Variable

/* Define a global theme color */
:root {
  --theme-color: #3498db; /* Blue */
}

/* Define a special section with a different theme color */
.special-section {
  --theme-color: #e74c3c; /* Red */
}

/* Style a button within the special section */
.special-section .button {
  background-color: var(--theme-color);
}

Explanation: This example shows the power of variable scope. A global --theme-color is set, but it is overridden within the .special-section. Any element inside this section that uses --theme-color will now use the new value.