search
HomeWeb Front-endJS TutorialMobile terminal - responsive, rem/em, using Js to dynamically implement mobile terminal adaptation

With the popularity of 3G, more and more people are using mobile phones to access the Internet. Mobile devices are surpassing desktop devices as the most common terminal for accessing the Internet. As a result, web designers have to face a difficult problem: How to render the same web page on devices of different sizes? This article will describe the concepts and methods of adaptive web design, allowing web developers to maintain the same web page code to make the website have a better reading experience on multiple devices. This article introduces the implementation method of adaptive web pages in detail, hoping to help you who are confused.

1. Add a meta tag to the head of HTML

Add a meta tag to the head of HTML, that is, the head tag, to tell the browser that the web page width is equal to the device screen width, and does not proceed. Zoom, the code is as follows:


##

  Briefly analyze the meaning of this line of code: width=device-width means that the width of the web page is equal to The width of the device screen, initial-scale=1.0 means setting the initial zoom ratio of the page to 1, user-scalable=no means prohibiting the user from zooming, maximum-scale=1.0 and minimum-scale=1.0 means setting the largest and smallest pages scaling ratio. Because major browsers parse meta tags to different degrees, we must try our best to be compatible with all browsers.

2. Percent layout

In page layout, combining relative width and absolute width for layout will be more conducive to the maintainability of the web page.

The following pictures are the layouts of Lagou.com under iPhone5, iPhone6 ​​and iPhone 6 Plus. It can be seen that with the different screen widths of the devices, even the font size and spacing displayed by the same set of webpage codes are different. All different. The part inside the red line box uses percentage layout, which will make the maintainability of the web page better.

         

3. Implementation of responsive pages

At present, there are two common methods to implement responsiveness. One is to use

media query, the other is grid layout under bootstrap. I will introduce grid layout when I introduce bootstrap later. Here I will mainly talk about how to use media queries to implement responsive layout.

Media queries, namely @media queries,

Media queries can set different styles for different screen sizes, especially if you need to design responsive pages, @media is very useful . When you reset the browser size, the page will also be re-rendered based on the browser's width and height. Because it is setting the style, just put the media query related code at the bottom of the css file.

In order to understand the usage of responsiveness more clearly, I have listed two cases below. The first case is relatively simple and realizes the function of changing the background color of the body in different page widths. The second case uses a specific project as an example to make it more convenient for users

Example 1:

If the page width is less than 300 pixels, modify the background color of the body to red:

@media screen and (max-width: 300px) {
    body {
         background-color:red;
    }
}
If the page width is greater than 300 pixels and less than 600 pixels, modify the background color of the body to green:

@media screen and (min-width: 300px) and (max-width:600px) {
    body {
         background-color:green;
    }
}

如果页面宽度大于 600 像素,则修改body的背景颜色为蓝色:

@media screen and (min-width: 600px) {
    body {
         background-color:blue;
    }
}

 

代码解释:

  screen 表示电脑屏幕,平板电脑,智能手机等,min-width和max-width 用于定义设备中页面的最小和最大宽度。


实例2:视觉中国首页的响应式实现

  首先来看该页面在不同窗口中的展示效果:

  在窗口宽度大于1200px时候的页面样式如下:

 

  在窗口宽度大于900px并且小于1200px时候页面样式如下:

 

当页面宽度小于900px时候页面样式如下:

 

接下来我们来看具体的代码实现:

  html代码如下:注意有几张图片则写几个col


<p>
    </p><p>
        </p><p>
            </p><p>
                <img  src="/static/imghwm/default1.png" data-src="img/8.jpg" class="lazy" alt="Mobile terminal - responsive, rem/em, using Js to dynamically implement mobile terminal adaptation" >
            </p>
        
        <p>
            </p><p>
                <img  src="/static/imghwm/default1.png" data-src="img/9.jpg" class="lazy" alt="Mobile terminal - responsive, rem/em, using Js to dynamically implement mobile terminal adaptation" >
            </p>
        
    

 

css代码如下,默认是页面宽度大于1200px时候的页面:


.group_wrap{
    width: 100%;
    overflow: hidden;
}
.group{
    width: 1200px;
    margin: 0 auto;
    overflow: hidden;
}
.col{
    width: 280px;
    margin: 10px;
    float: left;
}
.img_logo{
    padding: 10px;
    background: white;
}

 实现响应式代码如下,放在css文件的最下方即可:


/*当页面的宽度在900px ~ 1200px之间的时候*/
@media screen and (min-width: 900px) and (max-width: 1200px){
    .group{
        width: 900px;
    }
}
/*当页面的宽度在600px ~ 900px之间的时候*/
@media screen and (min-width:600px) and (max-width: 900px){
    .group{
        width: 600px;
    }
}


 

总结:实际上响应式页面的实现非常简单,只要认真学,经常练,一定可以熟练掌握的!

 

  

四. 页面使用相对字体

  在我们平常的网页布局过程中经常使用绝对单位像素(px)来进行布局,这样的布局不适合我们自适应网页的实现,所以我们现在来介绍两种常见的绝对单位em和rem。rem(font size of the root element)是指相对于根元素的字体大小的单位。简单的说它就是一个相对单位。看到rem大家一定会想起em单位,em(font size of the element)是指相对于父元素的字体大小的单位。它们之间其实很相似,只不过一个计算的规则是依赖根元素一个是依赖父元素计算。

  1. 相对长度单位em

      em的特点 : ① em的值并不是固定的; ② em始终会继承父级元素的字体大小。

      废话不多说,直接上代码:

html代码:

<p>
    <span>第一层</span>
    </p><p>
        <span>第二层</span>
        </p><p>
            <span>第三层</span>
        </p>
    

 

css代码:


The above is the detailed content of Mobile terminal - responsive, rem/em, using Js to dynamically implement mobile terminal adaptation. 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
The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Python vs. JavaScript: Development Environments and ToolsPython vs. JavaScript: Development Environments and ToolsApr 26, 2025 am 12:09 AM

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Is JavaScript Written in C? Examining the EvidenceIs JavaScript Written in C? Examining the EvidenceApr 25, 2025 am 12:15 AM

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript's Role: Making the Web Interactive and DynamicJavaScript's Role: Making the Web Interactive and DynamicApr 24, 2025 am 12:12 AM

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C   and JavaScript: The Connection ExplainedC and JavaScript: The Connection ExplainedApr 23, 2025 am 12:07 AM

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

From Websites to Apps: The Diverse Applications of JavaScriptFrom Websites to Apps: The Diverse Applications of JavaScriptApr 22, 2025 am 12:02 AM

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python vs. JavaScript: Use Cases and Applications ComparedPython vs. JavaScript: Use Cases and Applications ComparedApr 21, 2025 am 12:01 AM

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

The Role of C/C   in JavaScript Interpreters and CompilersThe Role of C/C in JavaScript Interpreters and CompilersApr 20, 2025 am 12:01 AM

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

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.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

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