Table of Contents
1. Media queries
2. Flexible layout
3. Responsive design for images and media
4. Adapt to different screen densities
Home Web Front-end CSS Tutorial CSS responsive design: adapt layout to different devices and screen sizes

CSS responsive design: adapt layout to different devices and screen sizes

Nov 18, 2023 pm 01:21 PM
Responsive design css layout Equipment adaptation

CSS responsive design: adapt layout to different devices and screen sizes

CSS responsive design: adapting layouts to different devices and screen sizes, specific code examples are required

With the popularity of mobile devices and the emergence of different screen sizes, we There is an increasing need to consider layout adaptability on different devices in web design. CSS responsive design is a technology that enables web pages to display the best results on different devices. This article will introduce the implementation method of CSS responsive design through specific code examples.

1. Media queries

Media queries are a way in CSS to adapt to different devices and screen sizes. By using @media rules, you can apply different CSS styles based on the width, height, pixel ratio and other characteristics of the device screen.

/* 当设备宽度小于等于768px时应用以下样式 */
@media (max-width: 768px) {
  body {
    font-size: 14px;
  }
  .container {
    width: 90%;
  }
}

/* 当设备宽度大于768px时应用以下样式 */
@media (min-width: 769px) {
  body {
    font-size: 16px;
  }
  .container {
    width: 70%;
  }
}
Copy after login

In the above example, when the device width is less than or equal to 768px, the font size of the entire page will become 14px, and the width of the container is 90%; when the device width is greater than 768px, the font size will be 16px, The container width is 70%. Through media queries, we can apply different styles according to different device sizes to achieve responsive layout.

2. Flexible layout

CSS’s flexible layout is also a key technology of responsive design. With flex layout, an element can dynamically adjust its size and position based on the size of its parent element.

.container {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.box {
  flex: 1;
}
Copy after login

In the above example, the .container element uses display: flex to create a flexible layout container, and the internal .box element Use flex: 1 to occupy the remaining space. In this way, no matter how the width of the container changes, the internal .box elements will automatically adjust their width, achieving responsiveness in the page layout.

3. Responsive design for images and media

Loading large-sized images and media on mobile devices will cause pages to load slowly and waste bandwidth. In order to improve the page loading speed and user experience, we can use the max-width property of CSS to implement responsive design for images and media.

img {
  max-width: 100%;
  height: auto;
}
Copy after login

In the above code, we set max-width to 100%, and the image will automatically adjust its size according to the width of the parent element while maintaining the aspect ratio. In this way, the picture will not exceed the boundaries of the parent container on different devices, ensuring the integrity of the page layout.

4. Adapt to different screen densities

On high-density devices, such as Retina displays, in order to ensure the clarity of text and images, we need to use high-resolution images and fonts. CSS provides suffixes such as @2x, which can load different resources on different screen densities.

@media only screen and (-webkit-min-device-pixel-ratio: 2),
       only screen and (min--moz-device-pixel-ratio: 2),
       only screen and (-o-min-device-pixel-ratio: 2/1),
       only screen and (min-resolution: 192dpi),
       only screen and (min-resolution: 2dppx) {
  /* 高密度设备上加载高分辨率图片和字体 */
}
Copy after login

In the above code, we use media query functions such as -webkit-min-device-pixel-ratio to identify high-density devices and load high-resolution resources. This way we can ensure the best results across different screen densities.

Summary:
CSS responsive design is a layout technology that adapts to different devices and screen sizes. Through media queries, flexible layout, responsive design of images and media, and adapting to different screen densities, we can achieve the best display effect of the page on different devices. In actual development, we can choose different responsive design methods based on needs and user groups, and implement them through specific code examples.

The above is the detailed content of CSS responsive design: adapt layout to different devices and screen sizes. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

React responsive design guide: How to achieve adaptive front-end layout effects React responsive design guide: How to achieve adaptive front-end layout effects Sep 26, 2023 am 11:34 AM

React Responsive Design Guide: How to Achieve Adaptive Front-end Layout Effects With the popularity of mobile devices and the increasing user demand for multi-screen experiences, responsive design has become one of the important considerations in modern front-end development. React, as one of the most popular front-end frameworks at present, provides a wealth of tools and components to help developers achieve adaptive layout effects. This article will share some guidelines and tips on implementing responsive design using React, and provide specific code examples for reference. Fle using React

How to use CSS Flex layout to implement responsive design How to use CSS Flex layout to implement responsive design Sep 26, 2023 am 08:07 AM

How to use CSSFlex elastic layout to implement responsive design. In today's era of widespread mobile devices, responsive design has become an important task in front-end development. Among them, using CSSFlex elastic layout has become one of the popular choices for implementing responsive design. CSSFlex elastic layout has strong scalability and adaptability, and can quickly implement screen layouts of different sizes. This article will introduce how to use CSSFlex elastic layout to implement responsive design, and give specific code examples.

Questions frequently asked by front-end interviewers Questions frequently asked by front-end interviewers Mar 19, 2024 pm 02:24 PM

In front-end development interviews, common questions cover a wide range of topics, including HTML/CSS basics, JavaScript basics, frameworks and libraries, project experience, algorithms and data structures, performance optimization, cross-domain requests, front-end engineering, design patterns, and new technologies and trends. . Interviewer questions are designed to assess the candidate's technical skills, project experience, and understanding of industry trends. Therefore, candidates should be fully prepared in these areas to demonstrate their abilities and expertise.

How to implement responsive layout using Vue How to implement responsive layout using Vue Nov 07, 2023 am 11:06 AM

Vue is a very excellent front-end development framework. It adopts the MVVM mode and achieves a very good responsive layout through two-way binding of data. In our front-end development, responsive layout is a very important part, because it allows our pages to display the best effects for different devices, thereby improving user experience. In this article, we will introduce how to use Vue to implement responsive layout and provide specific code examples. 1. Use Bootstrap to implement responsive layout. Bootstrap is a

The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element The evolution and application of CSS layout units: from pixels to relative units based on the font size of the root element Jan 05, 2024 pm 05:41 PM

From px to rem: The evolution and application of CSS layout units Introduction: In front-end development, we often need to use CSS to implement page layout. Over the past few years, CSS layout units have evolved and developed. Initially we used pixels (px) as the unit to set the size and position of elements. However, with the rise of responsive design and the popularity of mobile devices, pixel units have gradually exposed some problems. In order to solve these problems, the new unit rem came into being and was gradually widely used in CSS layout. one

Methods and techniques on how to implement waterfall flow layout through pure CSS Methods and techniques on how to implement waterfall flow layout through pure CSS Oct 20, 2023 pm 06:01 PM

Methods and techniques on how to implement waterfall flow layout through pure CSS. Waterfall layout (Waterfall Layout) is a common layout method in web design. It arranges content in multiple columns with inconsistent heights to form an image. Waterfall-like visual effects. This layout is often used in situations where a large amount of content needs to be displayed, such as picture display and product display, and has a good user experience. There are many ways to implement a waterfall layout, and it can be done using JavaScript or CSS.

CSS Viewport: How to use vh, vw, vmin, and vmax units for responsive design CSS Viewport: How to use vh, vw, vmin, and vmax units for responsive design Sep 13, 2023 pm 12:15 PM

CSSViewport: How to use vh, vw, vmin and vmax units to implement responsive design, specific code examples required In modern responsive web design, we usually want web pages to adapt to different screen sizes and devices to provide a good user experience. The CSSViewport unit (viewport unit) is one of the important tools to help us achieve this goal. In this article, we’ll cover how to use vh, vw, vmin, and vmax units to achieve responsive design.

CSS Layout Tips: Best Practices for Implementing Circular Grid Icon Layout CSS Layout Tips: Best Practices for Implementing Circular Grid Icon Layout Oct 20, 2023 am 10:46 AM

CSS Layout Tips: Best Practices for Implementing Circular Grid Icon Layout Grid layout is a common and powerful layout technique in modern web design. The circular grid icon layout is a more unique and interesting design choice. This article will introduce some best practices and specific code examples to help you implement a circular grid icon layout. HTML structure First, we need to set up a container element and place the icon in this container. We can use an unordered list (<ul>) as a container, and the list items (<l

See all articles