CSS Responsive Layout Property Guide: media queries and min-width/max-width
With the popularity of mobile devices, more and more users use mobile phones and tablets to access the website. This requires the website to be able to adapt to different screen sizes and device types to provide a better user experience. CSS Responsive Layout is a solution that allows web content to automatically adapt layout and style on different devices.
When implementing CSS responsive layout, we often use two important properties: media queries and min-width/max-width. This article will detail the usage of these two properties and provide specific code examples.
@media screen and (condition) {
/* CSS 样式规则 */
}
Among them, @media represents the beginning of the media query, and screen represents the query. The device type is screen. The conditions can be various, such as screen width, height, device orientation, etc.
For example, the following code snippet applies specific styles when the screen width is less than 600px:
@media screen and (max-width: 600px) {
/* 在屏幕宽度小于 600px 时应用的样式 */ body { background-color: lightblue; }
}
In this way, when a user visits the website on a device with a screen width less than 600px, the background color of the body will change to light blue.
For example, the following code will make the width of a div element 50% when the screen width is greater than 768px, and automatically adjust to 100% when the screen width is less than 768px:
div {
min-width: 100%; max-width: 50%;
}
When the screen width is greater than 768px, the width of the div element will be limited to 50%. When the screen width is less than 768px, the width of the div element will automatically adjust to 100%.
By combining media queries with min-width/max-width, we can optimize website layout and display effects according to different devices and screen sizes.
Here is a complete code example that demonstrates how to use media queries and min-width/max-width to implement a basic responsive layout:
<!DOCTYPE html> <html> <head> <style> @media screen and (max-width: 600px) { body { background-color: lightblue; } } div { min-width: 100%; max-width: 50%; height: 200px; background-color: lightgreen; margin: 20px; } </style> </head> <body> <div></div> </body> </html>
In the above example, when the screen width When less than 600px, the background color of the body will change to light blue. At the same time, the div element will use adaptive layout to set the width according to the screen width.
To summarize, CSS responsive layout can automatically adjust layout and style according to screen size and device type by using media queries and min-width/max-width properties. This method can greatly improve the user experience of web pages on different devices and improve user satisfaction. Hopefully the guidance provided in this article will help you use these properties in real-world projects.
The above is the detailed content of Guide to CSS responsive layout properties: media queries and min-width/max-width. For more information, please follow other related articles on the PHP Chinese website!