Implementation of postcss-pxtorem mobile adaptation

Implementation of postcss-pxtorem mobile adaptation

Execute the command to install the plugin postcss-pxtorem

npm install postcss-pxtorem -D

Create a new package.json in the same directory as postcss.config.js. The content of the file is as follows

module.exports = {
  plugins: {
    'autoprefixer': {
      browsers: ['Android >= 4.0', 'iOS >= 7']
    },
    'postcss-pxtorem': {
      rootValue: 32, //The result is: design element size/32 (generally, the root element size of a 750px design is set to 32). For example, if the element width is 320px, the final page will be converted to 10rem
      propList: ['*'], //property selector, * indicates general selectorBlackList:[] ignores the selector.ig- indicates that all the selectors starting with .ig- will not be converted}
  }
}

After the configuration of postcss.config.js is completed, the project needs to be restarted to take effect

Create a new rem.js file in the util directory in the root directory src.

// rem proportional adaptation configuration file // Base size const baseSize = 750 // Note that this value must be consistent with the rootValue in the postcss.config.js file // Set rem function function setRem () {
  // The current page width is relative to the 375 width scaling ratio, which can be modified according to your needs. Generally, the design draft is 750 width (you can get the design drawing and modify it for convenience).
  const scale = document.documentElement.clientWidth / 375
  // Set the font size of the root node of the page ("Math.min(scale, 2)" means the maximum magnification ratio is 2, which can be adjusted according to actual business needs)
  document.documentElement.style.fontSize = baseSize * Math.min(scale, 2) + 'px'
}
// Initialize setRem()
// Reset rem when changing window size
window.onresize = function () {
  setRem()
}

Import the rem.js file into main.js and start the project

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.

<<:  Collection of 25 fonts used in famous website logos

>>:  Detailed explanation of how to pass values ​​between react hooks components (using ts)

Recommend

Ten important questions for learning the basics of Javascript

Table of contents 1. What is Javascript? 2. What ...

Search optimization knowledge to pay attention to in web design

1. Link layout of the new site homepage 1. The loc...

Summary of some thoughts on binlog optimization in MYSQL

question Question 1: How to solve the performance...

Example code for implementing equal width layout in multiple ways using CSS

The equal-width layout described in this article ...

A brief talk about cloning JavaScript

Table of contents 1. Shallow cloning 2. Deep clon...

12 Useful Array Tricks in JavaScript

Table of contents Array deduplication 1. from() s...

VMware Workstation Installation (Linux Kernel) Kylin Graphic Tutorial

This article shares with you how to install Kylin...

Don't forget to close the HTML tag

Building web pages that comply with Web standards ...

Some ways to solve the problem of Jenkins integrated docker plugin

Table of contents background Question 1 Error 2 E...

Detailed explanation of where Docker saves log files

Table of contents Where are the logs stored? View...

docker-maven-plugin packages the image and uploads it to a private warehouse

Table of contents 1. Introduction to docker-maven...

Summary of js execution context and scope

Table of contents Preface text 1. Concepts relate...

Use of docker system command set

Table of contents docker system df docker system ...