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

Viewport Width


Viewport Units for responsive sizing, a cornerstone of modern responsive web design, allow developers to size elements relative to the browser's viewport. This ensures that layouts adapt fluidly across a multitude of devices and screen sizes, enhancing the user experience. By using units like vw, vh, vmin, and vmax, you can create scalable and maintainable CSS.

The vw unit is based on a percentage of the viewport's width. One vw is equivalent to 1% of the width of the viewport, making it ideal for creating full-width elements or typography that scales with the browser window.


Example 1: Responsive Typography

/* Sets the font size to 5% of the viewport width. */
h1 {
  font-size: 5vw;
}

Explanation This code makes the h1 element's font size directly proportional to the viewport's width. As the browser window is resized, the text will scale accordingly, ensuring it remains a consistent proportion of the screen.


Example 2: Full-Width Section

/* Creates a section that spans the entire width of the viewport. */
.full-width-section {
  width: 100vw;
}

Explanation Here, the .full-width-section class is assigned a width of 100vw, forcing it to occupy the full width of the browser's viewport, regardless of the parent container's width.


Example 3: Responsive Padding

/* Applies horizontal padding that scales with the viewport width. */
.container {
  padding-left: 10vw;
  padding-right: 10vw;
}

Explanation This example uses vw units for padding, creating flexible horizontal space within an element. The padding will grow or shrink as the viewport width changes.


Example 4: Minimum Font Size

/* Ensures a minimum font size using calc() with vw. */
p {
  font-size: calc(16px + 1vw);
}

Explanation By using calc(), this code sets a base font size of 16px and adds 1vw. This provides a responsive font size that won't become too small on narrow screens.


Example 5: Element Spacing

/* Creates a responsive gap between flex items. */
.flex-container {
  display: flex;
  gap: 2vw;
}

Explanation The gap property with a vw unit creates a space between flex items that is relative to the viewport's width, ensuring consistent proportional spacing.


Example 6: Border Width

/* Defines a border width that scales with the viewport. */
.responsive-border {
  border: 0.5vw solid black;
}

Explanation This example demonstrates a border whose thickness is determined by the viewport width. The border will appear thicker on wider screens and thinner on narrower ones.


Example 7: Negative Margin

/* Creates a "full-bleed" effect by pulling an element out of its container. */
.full-bleed {
  width: 100%;
  margin-left: -10vw;
  margin-right: -10vw;
}

Explanation This technique uses negative margins with vw units to make an element break out of its parent's constraints, often used for full-width images or sections within a padded layout.


Viewport Height (vh)

The vh unit corresponds to a percentage of the viewport's height. One vh is equal to 1% of the height of the viewport. This is particularly useful for creating elements that should occupy a specific portion of the screen's vertical space.

Example 1: Full-Height Hero Section

/* Makes the hero section take up the full height of the viewport. */
.hero {
  height: 100vh;
}

Explanation This code sets the .hero element's height to 100vh, causing it to fill the entire vertical space of the browser window, a common technique for landing pages.


Example 2: Vertically Centered Content

/* Uses flexbox and vh to vertically center a div. */
.center-container {
  height: 100vh;
  display: flex;
  align-items: center;
}

Explanation By setting the container's height to 100vh and using flexbox properties, the child elements within .center-container will be perfectly centered vertically in the viewport.


Example 3: Responsive Font Size based on Height

/* Sets the font size relative to the viewport height. */
h2 {
  font-size: 8vh;
}

Explanation In this case, the h2 font size will scale based on the viewport's height. This can be useful for designs where vertical space is the primary consideration.


Example 4: Section with Minimum Height

/* Ensures a section has a minimum height of half the viewport. */
.content-section {
  min-height: 50vh;
}

Explanation This example uses min-height with vh to guarantee that a section will always occupy at least 50% of the viewport's height, but can grow larger if its content requires more space.


Example 5: Spacing Between Sections

/* Adds vertical margin based on viewport height. */
section {
  margin-bottom: 10vh;
}

Explanation Using vh for margins creates vertical spacing between sections that is proportional to the screen height, leading to a more consistent vertical rhythm across different devices.


Example 6: Modal Dialog Height

/* A modal dialog that is never taller than 90% of the viewport height. */
.modal-content {
  max-height: 90vh;
  overflow-y: auto;
}

Explanation This code ensures that a modal's content area will not exceed 90% of the viewport's height. If the content is taller, a scrollbar will appear.


Example 7: Step Indicator Height

/* Each step in a vertical progress indicator has a specific height. */
.step {
  height: 25vh;
}

Explanation This sets each .step element to be 25% of the viewport height, which could be used to create a full-screen, four-step vertical wizard or guide.