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.