To customize your website's appearance for different devices, consider using CSS media queries.
CSS Media Queries is a module in CSS3 that allows you to adjust the layout of your content based on various factors like screen resolution (e.g., smartphone vs. computer).
Instead of just targeting specific devices, media queries enable you to cater to different capabilities of devices. This includes considerations such as:
The height and width of the device or browser
The screen resolution and orientation (portrait or landscape) for mobile phones and tablets
In CSS2, you could specify stylesheets for different media types like screen or print. CSS3 takes this further by introducing media queries, allowing you to apply styles based on conditions.
You can create tailored stylesheets for various resolutions and devices without altering the actual content, making it a powerful tool for responsive design.
For example:
If you want a certain style applied when the viewing area is smaller than 600px, you can use the following CSS:
@media screen and (max-width: 600px) {
.class {
background: #ccc;
}
}
To link a separate stylesheet based on a media query, include this line of code within the <head>
tag:
<link rel="stylesheet" media="screen and (max-width: 600px)" href="small.css" />
Using Multiple Media Queries:
You can combine multiple media queries to apply styles under different conditions. For instance, the following code applies when the viewing area ranges between 600px and 900px:
@media screen and (min-width: 600px) and (max-width: 900px) {
.class {
background: #333;
}
}
Considering Device Width:
If you wish to target devices with a maximum width of 480px (like an iPhone display), you can utilize the max-device-width property. Keep in mind that max-device-width refers to the actual device resolution, while max-width relates to the viewing area resolution.
@media screen and (max-device-width: 480px) {
.class {
background: #000;
}
}