CSS Responsive Design: Media Queries


Media inquiries

Media queries in CSS are a method for defining style sheets based on the screen size and resolution of different devices. In CSS, we can use media queries to apply different styles based on different device types and screen sizes to achieve responsive design.

The basic syntax of a media query is as follows:

@media screen and (max-width: 600px) {
    
    
  /* CSS样式 */
}

In the above code, @media specifies the media type, screen indicates that the device type is a screen device, and specifies multiple conditions, max-width: 600px indicates that the style is applied when the screen width is less than or equal to 600px.

In addition to screen, CSS also provides other media types, such as print (printing device), speech (speech synthesizer), etc. We can apply different styles based on different device types and screen sizes.

In media queries, we can use various conditions to define the application scope of the style sheet, such as max-width (maximum width), min-width (minimum width), orientation (screen direction), etc. We can choose different conditions according to specific needs.

In responsive design, we usually use media queries to define the layout and style under different screen sizes to ensure the adaptive layout and display effect of web pages on different devices. By using media queries appropriately, we can create more flexible and adaptable web page layouts and styles.

Add breakpoint

In CSS, we can use media queries to add breakpoints to apply different styles on different screen sizes. Breakpoints are the dividing points where different styles are applied on different screen sizes. By adding breakpoints, we can apply different layouts and styles based on different screen sizes to achieve responsive design.

Here is a simple example that demonstrates how to add breakpoints using media queries:

/* 默认样式 */
.container {
    
    
  width: 100%;
}

.box {
    
    
  width: 50%;
  float: left;
}

/* 在屏幕宽度小于 600px 时应用不同的样式 */
@media screen and (max-width: 600px) {
    
    
  .container {
    
    
    width: 100%;
  }

  .box {
    
    
    width: 100%;
    float: none;
  }
}

In the above example, we defined a container and two boxes. By default, the container width is 100% and each box is 50% wide and floated to the left. When the screen width is less than 600px, we apply a different style, where the container width is still 100%, but the width of each box becomes 100%, and the float is cancelled. This makes it possible to display a single box on a small screen and two boxes side by side on a large screen.

In practical applications, we can add multiple breakpoints as needed and define different styles for each breakpoint. By adding breakpoints, we can create more flexible and adaptable web page layouts and styles to accommodate different devices and screen sizes.

Designed for mobile first

Designing for mobile first means prioritizing the experience of mobile devices when designing and developing web pages to ensure that the display and operation of web pages on mobile devices are comparable to, or even better than, those on desktop devices.

Here are some tips for designing for mobile first:

  1. Responsive design: Use responsive design to ensure that your web pages adapt to different screen sizes, including mobile devices. This means using media queries to define different styles to accommodate different screen sizes.
  2. Optimize images and icons: For mobile devices, loading speed and memory usage are very important. Therefore, the file size and quality of images and icons need to be optimized to ensure they load quickly and take up less memory.
  3. Optimize buttons and text: On mobile devices, the size of buttons and text should be large enough for users to click and read easily. In addition, you should avoid using text and buttons that are too small to prevent users from accidentally clicking or operating them accidentally.
  4. Provide simple navigation: For mobile devices, simple navigation allows users to find what they are looking for faster. Therefore, the navigation menu should be simplified as much as possible and ensure that it is easy to understand and operate.
  5. Provide a complete user experience: Despite the smaller screen size of mobile devices, it should provide a complete user experience including various functions and information. For example, search functions, shopping carts, forms and other elements still need to be provided on mobile devices to ensure that users can easily complete the required operations and tasks.

All in all, designing for mobile first is an important trend in web design and development right now. By optimizing how your web pages appear and operate, you can provide a better user experience and attract more mobile device users.

Other breakpoints

In addition to using media queries to add breakpoints in CSS, there are other ways to implement breakpoints.

  1. JavaScript: Use JavaScript to detect the screen size of the current device and apply different styles or layouts based on different sizes. By writing JavaScript code, we can dynamically change the style and layout of elements according to the screen size to implement the function of breakpoints.
  2. Flexbox: Flexbox with CSS makes responsive design easy. By using Flexbox, you can easily adjust the size and position of elements to ensure optimal presentation on different screen sizes. Flexbox also provides various properties and values ​​to further control the layout and arrangement of elements.
  3. Grid: CSS’s Grid layout also provides the ability to implement responsive design. By using Grid, you can divide a web page into different grid areas and adjust the size and position of the grid according to the screen size. Grid layout also provides various properties and values ​​that can further control the properties and style of the grid.

In short, there are many ways to implement breakpoints, including using media queries in CSS, JavaScript, Flexbox, and Grid. Which method to choose depends on the needs of the project and the preferences of the developer.

Orientation: landscape/portrait

In CSS, you can use media queries and the device's orientation properties (such as orientation) to detect the screen orientation of the device and apply different styles accordingly. However, please note that device orientation information may no longer be supported by all browsers based on browser security policies and privacy considerations.

Here's a simple example where we try to change the background color based on the device's orientation:

/* 默认样式 */
body {
    
    
  background-color: lightblue;
}

/* 横屏样式 */
@media screen and (orientation: landscape) {
    
    
  body {
    
    
    background-color: lightgreen;
  }
}

/* 竖屏样式 */
@media screen and (orientation: portrait) {
    
    
  body {
    
    
    background-color: lightcoral;
  }
}

In this example, the background color will change to light green when the device is oriented in landscape, and to light coral when the device is oriented in portrait. When the device's orientation information cannot be obtained, the default style is applied, which is a light blue background color.

Please note that the above code may not work correctly on all devices and browsers as the device's orientation information may be disabled for privacy and security reasons. In actual projects, you should ensure that there are alternatives or alternative styles to deal with situations where device orientation information cannot be obtained.

Guess you like

Origin blog.csdn.net/m0_62617719/article/details/133071474