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

Orientation


The orientation media feature allows you to apply styles based on whether the viewport is in landscape (wider than it is tall) or portrait (taller than it is wide) mode.

Example 1: Two Columns in Landscape

/* Applying a two-column layout specifically for landscape mode on mobile */
@media (orientation: landscape) {
  .article-content {
    column-count: 2; /* a CSS property that breaks an element's content into the specified number of columns */
    column-gap: 25px; /* a CSS property that sets the size of the gap between an element's columns */
  }
}

Explanation

When a device is held in landscape orientation, this CSS will split the .article-content into two columns, which can make reading more comfortable.


Example 2: Adjusting Image Height in Landscape

/* Limiting the height of a hero image in landscape to prevent it from filling the screen */
@media (orientation: landscape) {
  .hero-image {
    max-height: 50vh; /* a CSS property that defines the maximum height of an element, based on viewport height */
  }
}

Explanation

In landscape mode, this code ensures that the hero image takes up no more than 50% of the viewport's height, preventing it from pushing important content out of view.


Example 3: Form Layout in Portrait

/* Forcing form elements to be full-width in portrait mode for easier use */
@media (orientation: portrait) {
  .form-input {
    width: 100%; /* a CSS property that defines the width of an element */
    margin-bottom: 10px; /* a CSS property that defines the margin area on the bottom of an element */
  }
}

Explanation

When the device is in portrait orientation, this CSS makes form input fields take up the full width of their container, providing a larger target for users.


Example 4: Hiding Ads in Landscape

/* Hiding a vertical banner ad in landscape mode where vertical space is limited */
@media (orientation: landscape) {
  .vertical-ad {
    display: none; /* Hide the ad */
  }
}

Explanation

This media query hides elements with the class .vertical-ad when the viewport is in landscape mode, freeing up horizontal space for the main content.


Example 5: Different Logo for Landscape

/* Using a wider, shorter logo in landscape mode */
.logo-portrait {
  display: block; /* a single block-level element that takes up the full width available */
}
.logo-landscape {
  display: none; /* a CSS property that removes an element from the document flow */
}

@media (orientation: landscape) {
  .logo-portrait {
    display: none; /* a CSS property that removes an element from the document flow */
  }
  .logo-landscape {
    display: block; /* a single block-level element that takes up the full width available */
  }
}

Explanation

This allows you to swap logos based on orientation. A taller logo might work well in portrait mode, while a wider one is more suitable for landscape.


Example 6: Adjusting Padding in Portrait

/* Increasing vertical padding in portrait mode */
@media (orientation: portrait) {
  .main-content {
    padding-top: 20px; /* a CSS property that defines the padding area on the top of an element */
    padding-bottom: 20px; /* a CSS property that defines the padding area on the bottom of an element */
  }
}

Explanation

This CSS adds more vertical padding to the main content area in portrait mode, which can improve visual balance when the layout is taller.


Example 7: Video Player Sizing

/* Making a video player take up more width in landscape */
@media (orientation: landscape) {
  .video-player {
    width: 80vw; /* 80% of the viewport width */
    height: 45vw; /* a CSS property that defines the height of an element */
  }
}

Explanation

In landscape mode, this CSS adjusts the video player's dimensions to maintain a 16:9 aspect ratio while using 80% of the viewport's width, ideal for video playback.