1 定义root html node的font-size,使用px或者rem;

2 使用rem定义元素font-size, 绝大部分情况禁止使用em定义font-size。em等同于当前元素的font-size,如果没有定义font-size,则继承父元素。所以em作为font-size,容易产生不确定的结果,但是em能很好控制一个范围内的比例,所以产生第3点结论

3 margin, padding, width, height,使用em

这样仅仅通过设置html 的font-size, 通过层层传递,html > componet font-size -> component maring/padding/width/height,就可以伸缩整个页面;另外可以单独设置某个component的em, 对某个component做单独的伸缩。

4 多列布局使用flex/ 百分比/vw,不使用rem。因为用rem/em不能保证多个列总和是稳定的。

5 不需要伸缩的,该用px的还是用px。

原文

Comprehensive Guide: When to Use Em vs. Rem​webdesign.tutsplus.com

Practical Application

There may be some debate among web designers and I’m sure different people have different preferred approaches, however my recommendation is as follows.

Use em Units For:

Any sizing that should scale depending on the font-size of an element other than the root.

Generally speaking, the only reason you’ll need to use em units is to scale an element which has non default font sizing.

As per our example above, design components like menu items, buttons, and headings may have their own explicitly stated font sizes. If you change these font sizes, you want the entire component to scale proportionately.

Common properties this guideline will apply to are margin, padding, width, height and line-height settings, when used on elements with non default font sizing.

I recommend that when you do employ em units, the font size of the element they’re used on should be set in rem units to preserve scalability but avoid inheritance confusion.

Typically Don’t Use em Units for Font Sizes

It’s quite common to see em units used for font sizing, particularly in headings, however I would suggest that designs are more manageable if rem units are typically used for font sizing.

The reason headings often use em units is they’re a better choice than px units, being relative to regular text size. However rem units can achieve this goal equally well. If any font size adjustment on the html element is made, the heading sizes will still scale too.

More often than not, we don’t want our font sizes to scale based on any element other than the root, with only a few exceptions.

One example where we might want em based font sizing could be a drop down menu, where we have second level menu item text sized depending on the font size of the first level. Another example might be a font icon used inside a button, where the icon’s size should relate to the button’s text size.

However most elements in a web design will tend not to have this type of requirement, so you’ll generally want to use rem units for font sizing, with em units only where specifically needed.

Use rem units for:

Any sizing that doesn’t need em units for the reasons described above, and that should scale depending on browser font size settings.

This accounts for almost everything in a standard design including most heights, most widths, most padding, most margins, border widths, most font sizes, shadows, basically almost every part of your layout.

In a nutshell, everything that can be made scalable with rem units, should be.

Tip

When creating layouts it’s often easier to think in pixels but output in rem units.

You can have pixel to rem calculations done automatically via a preprocessor like Stylus / Sass / Less, or a postprocessor like PostCSS with the PXtoRem plugin.

Alternatively, you can use PXtoEM to manually do your conversions.

Always Use rem Media Queries

Importantly, when using rem units to create a uniformly scalable design, your media queries should also be in rem units. This will ensure that whatever a user’s browser font size, your media queries will respond to it and adjust your layout.

For example, if a user scales up text very high, your layout may need to snap down from a two columns to a single column, just as it might on a smaller screened mobile device.

If your breakpoints are at fixed pixel widths, only a different viewport size can trigger them. However with rem based breakpoints they will respond to different font sizing too.

Don’t Use em or rem For:

Multi Column Layout Widths

Column widths in a layout should typically be % based so they can fluidly fit unpredictably sized viewports.

However single columns should still generally incorporate rem values via a max-width setting.

For example:

css .container { width: 100%; max-width: 75rem; }

This keeps the column flexible and scalable, but prevents it from becoming too wide for the text therein to be comfortably readable.

When an Element Should be Strictly Unscalable

In a typical web design there won’t be many parts of your layout that can’t be designed for scalability. However occasionally you will come across elements that really do need to use explicit fixed values for the purpose of preventing scaling.

The precondition for employing fixed sizing values should be that if the element in question were scaled it would break. This really doesn’t come up often, so before you’re tempted to whip out those px units, ask yourself if using them is an absolute necessity.

Wrapping Up

Let’s have a quick bullet point recap of what we’ve covered:

  • rem and em units are computed into pixel values by the browser, based on font sizes in your design.
  • em units are based on the font size of the element they’re used on.
  • rem units are based on the font size of the html element.
  • em units can be influenced by font size inheritance from any parent element
  • rem units can be influenced by font size inheritance from browser font settings.
  • Use em units for sizing that should scale depending on the font size of an element other than the root.
  • Use rem units for sizing that doesn’t need em units, and that should scale depending on browser font size settings.
  • Use rem units unless you’re sure you need em units, including on font sizes.
  • Use rem units on media queries
  • Don’t use em or rem in multi column layout widths - use % instead.
  • Don’t use em or rem if scaling would unavoidably cause a layout element to break.

I hope you’ve now built a robust and complete picture of exactly how em and rem units work, and through that know how to best leverage them in your designs.

I encourage you to try the usage guidelines contained in this tutorial for yourself, and enjoy the fully fledged scalability and responsiveness of the layouts they’ll enable you to create.

混合app用百分比还是rem_[笔记]em, rem最佳实践相关推荐

  1. 混合app用百分比还是rem_一次搞懂前端所有CSS长度单位,px、em、rem、rpx、%....

    前端就是一个大杂烩,做为前端工程师需要掌握的知识点太多了.各种长度单位需要我们去了解 除了这些还有印刷单位 pt pc pt cm mm in.... 毫无疑问现在统治前端的长度单位还是px, 一个相 ...

  2. 混合app用百分比还是rem_如何用平板APP和手机APP控制混合音视频矩阵切换器

    平板电脑APP软件和手机APP软件控制混合音视频矩阵切换器,控制准确灵敏,使用灵活方便,内容丰富而且可以任意改动,外观漂亮.适用于混合矩阵.HDMI矩阵.DVI矩阵.VAG矩阵.AV矩阵.视频矩阵.音 ...

  3. 为什么 APP 纷纷开发“暗黑模式”?优酷最佳实践总结

    简介: < 优酷 APP 全量支持"暗黑模式" 设计与技术完整总结>正式发布-- ​ 一.缘起 随着iOS 13和Android 10的正式发布,一个名词"暗 ...

  4. android app套壳马甲开发,Android应用开发最佳实践:马甲包配置管理

    在过去的半年时间中,由于行业大环境的恶化,App产品需求一直处于停滞状态,接到的都是市场导向的马甲包需求.随着马甲包的不断增多,build.gradle文件的内容长度开始变得难以接受,于是优化之旅随之 ...

  5. web前端开发最佳实践--(笔记之JavaScript最佳实践)

    如何避免全局变量污染? 避免定义全局变量或全局函数 用一个变量进行封装,并返回外部需要访问的接口 如何写出高维护的js代码 配置数据和代码逻辑分离 如: 改成: --- 用js模板 mustache ...

  6. 混合app开发学习笔记

    什么是混合移动App开发[重点] 苹果上的软件是如何开发出来的:使用的是 OC.或者使用Swift这门语言 安卓平台上的软件又是如何开发出来的:使用安卓相关的语言开发的,Java,安卓的控件进行开发 ...

  7. 开发缺点_成都嗨创科技:原生APP开发与混合APP开发的优缺点对比

    原生APP开发 1.开发语言: 安卓APP:Java开发语言: 苹果APP:Objective-C 或Swift 开发语言: 均为官方规定开发语言: 2.优点 1)使用体验较好,能够实现较为复杂的交互 ...

  8. 混合式app php怎么打包,如何实现混合 App Web 资源的打包与增量更新

    综述 移动 App 的运行环境具有带宽不稳定,流量收费,启动速度比较重要等特点,所以混合 App 如何加载 Web 资源并不是一个新问题.本文目的是总结出一种资源打包下载的思路和方案,并且提供一种打包 ...

  9. 混合App开发,HBuilder开发移动App

    使用HBuilder开发混合App: Hbuilder:是一个在线打包工具,不需要在本地配置开发环境:直接将做好的网站,通过一些简单的操作,就能在线打包为一个App: 混合APP开发常见技术:Html ...

最新文章

  1. 许昌学院计算机科学住几人间,许昌学院宿舍条件,宿舍环境图片(10篇)
  2. Java中int和short的取值范围_我的处理器上C中的int,short和其他数据类型的范围?...
  3. 浅谈SAP Cloud for Sales 自动化
  4. 对6月份的项目的总结
  5. Linux配置nginx打开报404,Linux下Nginx配置404页面的方法
  6. Java败给Python?不!我有话说
  7. MCS51单片机的型号与组成
  8. C#注入DLL-C#注入器
  9. Yate for Mac音乐标签管理工具
  10. php smart模板技术入门教程
  11. 游戏技能一:激光扫射的实现【CocosCreator 2D】【TypeScript】
  12. 题解 - CF613D Kingdom and its Cities
  13. solidworks批量图号分离_SolidWorks2014基于宏实现快速图号名称分离.docx
  14. 蚂蚁资深技术专家刘晓莹十年支付宝回忆录
  15. iOS中制作一张水印图片
  16. unity_在canvas中修改text和text到底是什么
  17. 微信公众平台开发(六) 翻译功能开发
  18. matlab数据归一化与反归一化处理
  19. 飞机躲子弹小游戏案例
  20. Axure RP 原型设计专业软件

热门文章

  1. 全攻略:大病医疗专项附加扣除,一文集全了
  2. activiti5.13 框架 数据库设计说明书
  3. cad vba编程从入门到精通_【CAD教程】CAD2020零基础入门到精通全套视频教程
  4. 加工中心宏程序生成器_宏程序G1铣锥度螺纹NPT
  5. 匿名内部类 可以访问外部类_Java——内部类详解
  6. Windows 7+Ubuntu 16.04 双系统安装
  7. 计算机网络原理201810自考,2018年10月自考04741计算机网络原理试卷及答案
  8. 阿里巴巴整理的python_阿里P8大佬整理的2020年最全99道python面试题,文末附答案...
  9. pytest+allure之测试报告本地运行
  10. 一个字形容大数据_嘉禾数据科学分享(八):一个大数据专业教师眼里的大数据