当前位置: 首页> 房产> 建筑 > html文件使用postcss-pxtorem适配移动端 使用tailwindcss库

html文件使用postcss-pxtorem适配移动端 使用tailwindcss库

时间:2025/7/10 0:18:59来源:https://blog.csdn.net/qq_40230735/article/details/139470764 浏览次数:1次

项目截图

请添加图片描述

插件下载

npm i -D postcss@8.4.38 postcss-cli@10.1.0 postcss-pxtorem@6.1.0 tailwindcss@3.4.3

postcss.config.js & tailwind.config.js

postcss.config.js

const pxToRem = require('postcss-pxtorem')
module.exports = {plugins: [pxToRem({rootValue: 75,propList: ['*'],minPixelValue: 2})]
}

tailwind.config.js

/** @type {import('tailwindcss').Config} */
module.exports = {content: ['./index.html',],theme: {extend: {},},plugins: [],
}

package.json的scripts配置监听命令,每次修改项目注意启动监听

"useTailwindCss": "npx tailwindcss -i ./css/tailwindInput.css -o ./css/tailwindOutput.css --watch",
"usePostcss": "postcss ./css/tailwindOutput.css -o ./css/tailwindOutput.css -w",

页面大小变化修改html根元素fontSize大小

// ./js/rem.js 路径
(function (win, doc) {if (!win.addEventListener) returnfunction setFont() {let screenWidth = document.querySelector('html').offsetWidthconst baseSize = 75 // 我的设计稿是750px,如果是375px则写37.5const pageWidth = 750let fontSize = (baseSize * screenWidth) / pageWidthdocument.querySelector('html').style.fontSize = `${fontSize}px`}setFont()setTimeout(() => {setFont()}, 300)doc.addEventListener('DOMContentLoaded', setFont, false)win.addEventListener('resize', setFont, false)win.addEventListener('load', setFont, false)
})(window, document)

index.html引入资源文件

<link rel="stylesheet" href="./css/tailwindOutput.css">
<script src="./js/rem.js"></script>

PS

package.json文件

{"name": "xxx","version": "1.0.0","description": "","main": "postcss.config.js","dependencies": {},"devDependencies": {"postcss": "^8.4.38","postcss-cli": "^10.1.0","postcss-pxtorem": "^6.1.0","tailwindcss": "^3.4.3"},"scripts": {"useTailwindCss": "npx tailwindcss -i ./css/tailwindInput.css -o ./css/tailwindOutput.css --watch","usePostcss": "postcss ./css/tailwindOutput.css -o ./css/tailwindOutput.css -w","test": "echo \"Error: no test specified\" && exit 1"},"keywords": [],"author": "","license": "ISC"
}
关键字:html文件使用postcss-pxtorem适配移动端 使用tailwindcss库

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: