Mobile web screen adaptation (rem)

Mobile web screen adaptation (rem)

Preface

I recently sorted out my previous notes on learning front-end, and found that I didn’t really understand the mobile web screen adaptation (rem), I just knew how to use it.

Next, I’ll record some of my thoughts on mobile web screen adaptation (rem).

rem Introduction

rem represents the font-size of the root element (<html>). That is, if the font-size of the root element is 14px, then 1rem = 14px

rem adapted to mobile web

Adaptation effect

On screens of different sizes, the size of the same element may not appear to be the same, but the proportion of the screen width they occupy is the same.

Code

// In the head tag of the html file <script type="text/javascript">
  (function(){
    var html = document.documentElement;
    // Get the screen width (px)
    var hWidth = html.getBoundingClientRect().width;
    // Set the font-size of the html tag to hWidth/15
    html.style.fontSize = hWidth/15 + 'px';
  })()
</script>
// In less /* define variable @r: 750/15 */
@r:50rem; 
div {
  width: 100/@r;
  height: 200/@r;
}

javascript code

First, we copy 1/15 of the screen size (px) to the font-size attribute of the html tag. At this point, on any screen size, 1/15 px of the screen size (px) is equal to 1rem. That is, on screens of any size, as long as the same rem value is set for an element, the proportion of the screen width occupied by the element will be the same on screens of all sizes. If the proportion is the same, it will be adapted to screens of all sizes.

less code

Now we just need to convert the px units of the elements in the design into rem units.

So, at this time, we can regard the design draft as a mobile phone screen of a certain size.
In my case, the width of the design is 750px.

Therefore, 750/15 = 50px, that is, on a mobile phone screen of the size of the design draft, 1rem = 50px.

Then, in the less code, we define a variable @r.

The width of the div is measured to be 100px. Because in a screen of the size of the design draft, 1rem = 50px, so the rem value of the div is: 100/50 rem, that is, 100/@r.

The height of the div is measured to be 200px. Because in a screen of the size of the design draft, 1rem = 50px, so the rem value of the div is: 200/50 rem, that is, 200/@r.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  How to build YUM in Centos7 environment

>>:  Move MySQL database to another disk under Windows

Recommend

vue+tp5 realizes simple login function

This article example shares the specific code of ...

Website Color Schemes Choosing the Right Colors for Your Website

Does color influence website visitors? A few year...

MySQL 5.6 compressed package installation method

There are two installation methods for MySQL: msi...

How to make full use of multi-core CPU in node.js

Table of contents Overview How to make full use o...

Tutorial on customizing rpm packages and building yum repositories for Centos

1 Keep the rpm package downloaded when yum instal...

Tips and precautions for using MySQL index

1. The role of index In general application syste...

How to use explain to query SQL execution plan in MySql

The explain command is the primary way to see how...

Detailed explanation of mysql transaction management operations

This article describes the MySQL transaction mana...

JavaScript canvas to load pictures

This article shares the specific code of JavaScri...

One minute to experience the smoothness of html+vue+element-ui

Technology Fan html web page, you must know vue f...

Analysis of centos6 method of deploying kafka project using docker

This article describes how to use docker to deplo...

Several ways of running in the background of Linux (summary)

1. nohup Run the program in a way that ignores th...

win2008 server security settings deployment document (recommended)

I had been working on the project before the New ...

How to generate Vue user interface by dragging and dropping

Table of contents Preface 1. Technical Principle ...

How to recover deleted MySQL 8.0.17 root account and password under Windows

I finished learning SQL by myself not long ago, a...