CSS Basic
CSS @media height Property
Photo Credit to CodeToFun
π Introduction
The @media
rule in CSS allows for responsive design by applying styles based on certain conditions, such as screen size, device type, or orientation.
The height feature within the @media
rule helps target devices or viewports with specific heights. This allows developers to apply different styles depending on the height of the viewport, ensuring a more flexible and user-friendly experience across various devices.
π‘ Syntax
The syntax for using the height feature in the @media
rule is straightforward. You define the media query with a specific height condition, and inside the query, you can place the styles you want to apply.
@media (min-height: height-value) {
/* CSS rules */
}
@media (max-height: height-value) {
/* CSS rules */
}
π Property Values
- min-height: Sets the minimum height at which the styles inside the media query will apply.
- max-height: Sets the maximum height at which the styles inside the media query will apply.
- height-value: A length value representing the height of the viewport. It can be set in pixels (px), ems (em), or percentage (%).
ποΈ Default Value
There is no default value for height in a media query because it must be explicitly defined within the query based on the developer's requirements.
π Example Usage
π Basic Usage
In this example, we will change the background color of the page depending on the height of the viewport.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS @media height Example</title>
<style>
body {
background-color: lightblue;
}
/* When the viewport height is at least 600px */
@media (min-height: 600px) {
body {
background-color: lightgreen;
}
}
/* When the viewport height is at most 400px */
@media (max-height: 400px) {
body {
background-color: lightcoral;
}
}
</style>
</head>
<body>
<h1>Resize the browser height to see the effect</h1>
</body>
</html>
π₯οΈ Browser Compatibility
The @media rule
with the height feature is widely supported across all modern browsers, including Chrome, Firefox, Safari, Edge, and Opera. It is essential to ensure your styles behave as expected across different browsers and devices by testing thoroughly.
π Conclusion
The @media rule
with the height feature is a useful tool for creating responsive designs that adapt to different viewport heights.
By utilizing min-height and max-height, you can control how your layout and styles change as the user adjusts their browser or switches devices, ensuring a seamless experience across all platforms.
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS @media height Property), please comment here. I will help you immediately.