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

Dynamic Viewport


Dynamic viewport units provide more control over sizing elements relative to the viewport, even when browser UI elements like address bars are present. This ensures a consistent layout across different devices and browser states.

Example 1: Using svw for Small Viewport

.hero-section {
  width: 100svw; /* 100% of the small viewport width */
  height: 70svh; /* 70% of the small viewport height */
  background-color: lightblue;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 3svh; /* Font size scales with small viewport height */
}

/* Consider how svw handles situations where browser UI is visible */
/* Useful for content that should always fit within the 'safest' visible area */

Explanation svw and svh represent the small viewport width and height, respectively. This means the size is calculated excluding any dynamic browser UI elements (like the address bar) that might be present, providing a "safer" or "smaller" visible area.


Example 2: Using lvw for Large Viewport

.full-page-overlay {
  width: 100lvw; /* 100% of the large viewport width */
  height: 100lvh; /* 100% of the large viewport height */
  background-color: rgba(0, 0, 0, 0.5);
  position: fixed;
  top: 0;
  left: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 5lvw; /* Font size scales with large viewport width */
}

/* lvw is ideal for elements that should occupy the maximum possible space */
/* Useful for overlays or backgrounds that extend under browser UI */

Explanation lvw and lvh represent the large viewport width and height. These units calculate the size including any dynamic browser UI elements, providing the largest possible viewport dimension.


Example 3: Using dvw for Dynamic Viewport

.responsive-card {
  width: 90dvw; /* 90% of the dynamic viewport width */
  max-width: 500px;
  margin: 2dvh auto; /* Margin scales with dynamic viewport height */
  padding: 20px;
  border: 1px solid #ccc;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  font-size: 2.5dvh; /* Font size scales with dynamic viewport height */
}

/* dvw adjusts dynamically based on the presence or absence of browser UI */
/* Provides a balanced approach for responsive designs */

Explanation dvw and dvh represent the dynamic viewport width and height. These units automatically adjust based on whether browser UI elements are present or not, providing a fluid and adaptive sizing solution.


Addressing Browser UI Elements in Viewport Calculations

Traditional viewport units (vw, vh) don't account for dynamic browser UI, leading to potential layout shifts. Dynamic viewport units (svw, lvw, dvw) solve this by providing different viewport definitions based on browser UI presence, ensuring more stable layouts.

Example 4: vw vs dvw in a Sticky Header

.header-old {
  position: sticky;
  top: 0;
  width: 100vw; /* May cause layout shift when address bar appears/disappears */
  background-color: #f8f8f8;
  padding: 15px;
  text-align: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

.header-new {
  position: sticky;
  top: 0;
  width: 100dvw; /* Adapts dynamically to browser UI changes */
  background-color: #e6e6e6;
  padding: 15px;
  text-align: center;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}

/* Observe the difference in how these headers behave on mobile browsers */
/* The 'new' header provides a more consistent experience */

Explanation This example highlights how 100vw can lead to layout shifts when the browser's dynamic UI (like the address bar on mobile) appears or disappears. Conversely, 100dvw adapts fluidly, maintaining a more stable layout.


Example 5: Impact on Full-Screen Modals

.modal-legacy {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw; /* Might be obscured by browser UI or cause scroll issues */
  height: 100vh; /* Can result in content being pushed off-screen */
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
}

.modal-modern {
  position: fixed;
  top: 0;
  left: 0;
  width: 100dvw; /* Ensures modal always fits the current visible viewport */
  height: 100dvh; /* Prevents unwanted scrolling due to UI elements */
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
}

/* Test these modals on a mobile device to see the difference */
/* The 'modern' modal provides a true full-screen experience */

Explanation Using 100vw and 100vh for full-screen modals can lead to issues where the modal is partially hidden by browser UI or causes unwanted scrolling. 100dvw and 100dvh guarantee that the modal always fits within the dynamically adjusted visible viewport, providing a seamless user experience.