search
HomeWeb Front-endCSS TutorialCSS 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 designcss layoutEquipment 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%;
  }
}

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;
}

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;
}

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) {
  /* 高密度设备上加载高分辨率图片和字体 */
}

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
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
Iterating a React Design with Styled ComponentsIterating a React Design with Styled ComponentsApr 21, 2025 am 11:29 AM

In a perfect world, our projects would have unlimited resources and time. Our teams would begin coding with well thought out and highly refined UX designs.

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Oh, the Many Ways to Make Triangular Breadcrumb Ribbons!Apr 21, 2025 am 11:26 AM

Oh, the Many Ways to Make Triangular Breadcrumb Ribbons

SVG Properties in CSS GuideSVG Properties in CSS GuideApr 21, 2025 am 11:21 AM

SVG has its own set of elements, attributes and properties to the extent that inline SVG code can get long and complex. By leveraging CSS and some of the forthcoming features of the SVG 2 specification, we can reduce that code for cleaner markup.

A Few Functional Uses for Intersection Observer to Know When an Element is in ViewA Few Functional Uses for Intersection Observer to Know When an Element is in ViewApr 21, 2025 am 11:19 AM

You might not know this, but JavaScript has stealthily accumulated quite a number of observers in recent times, and Intersection Observer is a part of that

Revisting prefers-reduced-motionRevisting prefers-reduced-motionApr 21, 2025 am 11:18 AM

We may not need to throw out all CSS animations. Remember, it’s prefers-reduced-motion, not prefers-no-motion.

How to Get a Progressive Web App into the Google Play StoreHow to Get a Progressive Web App into the Google Play StoreApr 21, 2025 am 11:10 AM

PWA (Progressive Web Apps) have been with us for some time now. Yet, each time I try explaining it to clients, the same question pops up: "Will my users be

The Simplest Ways to Handle HTML IncludesThe Simplest Ways to Handle HTML IncludesApr 21, 2025 am 11:09 AM

It's extremely surprising to me that HTML has never had any way to include other HTML files within it. Nor does there seem to be anything on the horizon that

Change Color of SVG on HoverChange Color of SVG on HoverApr 21, 2025 am 11:04 AM

There are a lot of different ways to use SVG. Depending on which way, the tactic for recoloring that SVG in different states or conditions — :hover,

See all articles

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

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.