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

Relative Units


Relative length units are highly recommended for creating flexible and responsive layouts, as they scale relative to other elements or the viewport. This makes your designs adaptable to various screen sizes.

Example 1: em Unit

/* Sets the font size of the paragraph relative to its parent's font size */
p {
  font-size: 1.2em; /* 1.2 times the parent's font size */
}

div {
  font-size: 16px; /* Parent font size */
}

Explanation The em unit is relative to the font size of the element's direct parent. If the parent's font size changes, the element's em-defined properties will adjust accordingly.


Example 2: rem Unit

/* Sets the font size of the heading relative to the root (html) element's font size */
h1 {
  font-size: 2.5rem; /* 2.5 times the root element's font size */
}

html {
  font-size: 16px; /* Root font size */
}

Explanation The rem unit is relative to the font size of the root HTML element. This provides a consistent scaling factor across the entire document, simplifying typography management.


Example 3: vw Unit

/* Sets the width of the image to 50% of the viewport width */
img {
  width: 50vw; /* 50% of the browser window's width */
}

Explanation The vw unit represents a percentage of the viewport's width. This is ideal for creating elements that scale proportionally with the browser window, ensuring responsive layouts.


Example 4: vh Unit

/* Sets the height of a section to 75% of the viewport height */
section {
  height: 75vh; /* 75% of the browser window's height */
}

Explanation The vh unit represents a percentage of the viewport's height. It's perfect for full-height sections or elements that need to occupy a specific portion of the user's screen.


Example 5: vmin Unit

/* Sets the font size to 8% of the smallest viewport dimension (width or height) */
.hero-text {
  font-size: 8vmin; /* 8% of the smaller of viewport width or height */
}

Explanation The vmin unit represents a percentage of the smaller dimension (width or height) of the viewport. This ensures that an element will always be visible regardless of orientation.


Example 6: vmax Unit

/* Sets the width of a sidebar to 30% of the largest viewport dimension (width or height) */
aside {
  width: 30vmax; /* 30% of the larger of viewport width or height */
}

Explanation The vmax unit represents a percentage of the larger dimension (width or height) of the viewport. It can be useful for elements that should always be a significant size relative to the screen.


Example 7: ch Unit

/* Sets the width of a text block to fit 60 characters of the '0' glyph */
.code-block {
  width: 60ch; /* Width based on the width of the '0' (zero) character */
  font-family: monospace;
}

Explanation The ch unit is relative to the width of the "0" (zero) character in the element's current font. It's particularly useful for controlling the width of text blocks to ensure readability.


Example 8: ex Unit

/* Sets the vertical spacing based on the x-height of the font */
figcaption {
  margin-top: 0.5ex; /* 0.5 times the x-height of the font */
}

Explanation The ex unit is relative to the x-height of the element's current font (typically the height of lowercase 'x'). This unit is valuable for fine-tuning vertical alignment and spacing of text.


Example 9: lh Unit

/* Sets the height of a box to 3 times the line-height of its content */
.text-container {
  height: 3lh; /* 3 times the calculated line-height of the element */
}

Explanation The lh unit is relative to the computed line-height of the element. It's useful for creating consistent vertical rhythm and spacing based on actual line heights.


Example 10: Combining Relative Units

/* Uses rem for base font size, em for padding, and vw for max-width for responsiveness */
.card {
  font-size: 1rem; /* Base font size */
  padding: 1.5em; /* Padding relative to card's font size */
  max-width: 80vw; /* Max width relative to viewport */
  margin: 0 auto;
}

Explanation Combining different relative units allows for highly flexible and adaptive designs. This example demonstrates how rem, em, and vw can work together for a responsive card component.