博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
rem字体响应式布局
阅读量:4594 次
发布时间:2019-06-09

本文共 2260 字,大约阅读时间需要 7 分钟。

引用js,自动算字体大小,响应式布局

<script>

  var iScale = 1;
  iScale = iScale / window.devicePixelRatio;
  document.write('<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=' + iScale + ',minimum-scale=' + iScale + ',maximum-scale=' + iScale + '">');

  /* 动态设置字体大小*/

  function htmlFontSizeChange() {
    var iWidth = document.documentElement.clientWidth;
    document.getElementsByTagName('html')[0].style.fontSize = iWidth / 16 + 'px';
    console.log("1rem = " + iWidth / 16);
  }
  htmlFontSizeChange();
  $(window).resize(function() {
    htmlFontSizeChange();
  });

</script>

<style>

  body{

    font-size:1rem; /*(16px)/(iWidth/16)  (iWidth设计图宽)*/

  }

</style>

 

示例

<!DOCTYPE html>

  <html>

  <head>

  <meta charset="UTF-8">
  <title></title>
  <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <script>
    var iScale = 1;
    iScale = iScale / window.devicePixelRatio;
    document.write('<meta name="viewport" content="width=device-width,user-scalable=no,initial-scale=' + iScale + ',minimum-scale=' + iScale + ',maximum-scale=' + iScale + '">');

    /* 动态设置字体大小*/

    function htmlFontSizeChange() {
      var iWidth = document.documentElement.clientWidth;
      document.getElementsByTagName('html')[0].style.fontSize = iWidth / 16 + 'px';
      console.log("1rem = " + iWidth / 16);
    }
    htmlFontSizeChange();
    $(window).resize(function() {
      htmlFontSizeChange();
    });
  </script>
  <style>
  span {
    font-size: 0.21333333rem;
    /*设计图大小为1200,16/(1200/16)*/
    /*font-size: 16px;*/
  }
  div {
    width: 0.4rem;
    /*30/(1200/16)*/
    /*width: 30px;*/
    background: red;
    height: 0.4rem;
  }
  </style>
  </head>

  <body>

    <span>设计图宽度为1200</span>
    <div></div>
  </body>

</html>

 

 

直接使用css设置rem

html{

  font-size:62.5%; /* 10÷16=62.5% */

}

body{

  font-size:12px;

  font-size:1.2rem ; /* 12÷10=1.2 */

}

媒体查询

@media only screen and (min-width: 481px){

  html {

    font-size: 94%!important;/* 15.04÷16=94% */

  }

}

@media only screen and (min-width: 561px){

  html {

    font-size: 109%!important; /* 17.44÷16=109% */

   }

}

@media only screen and (min-width: 641px){

  html {

    font-size: 125%!important;/* 20÷16=125% */

  }

}

引用来自 

转载于:https://www.cnblogs.com/kcat/p/6165067.html

你可能感兴趣的文章
StringBuilder的使用与总结
查看>>
CSS3基础(2)—— 文字与字体相关样式、盒子类型、背景与边框相关样式、变形处理、动画功能...
查看>>
Java的文档注释之生成帮助文档
查看>>
转:web_url函数学习
查看>>
TCP客户端 服务端详细代码
查看>>
win10用filezilla server搭建ftp服务器一直无法访问
查看>>
SQL存储过程、视图
查看>>
MySQL系列(十一)--外键约束foreign key的基本使用
查看>>
字符串算法(KMP,Trie树,AC自动机)
查看>>
第一次从头至尾的项目
查看>>
ng-options用法详解
查看>>
封装别人的库的时候一些注意点
查看>>
【Jim】I am back (ง •_•)ง
查看>>
Mysql数据库相关知识
查看>>
flex布局学习总结
查看>>
函数式编程
查看>>
js中的事件添加和程序
查看>>
多校 2013 8
查看>>
Docker 容器管理
查看>>
zoj-3706 Break Standard Weight
查看>>