首页 \ 问答 \ 为什么em而不是px?(Why em instead of px?)

为什么em而不是px?(Why em instead of px?)

我听说你应该用em代替像素来定义样式表中的大小和距离。 所以问题是为什么在css中定义样式时应该使用em而不是px? 有没有一个很好的例子说明这一点?


I heard you should define sizes and distances in your stylesheet with em instead of in pixels. So the question is why should I use em instead of px when defining styles in css? Is there a good example that illustrates this?


原文:https://stackoverflow.com/questions/609517
更新时间:2024-03-19 19:03

最满意答案

递归通常要慢得多,因为所有的函数调用必须存储在堆栈中才能返回到调用者函数。 在许多情况下,必须分配和复制内存才能实现范围隔离。

一些优化,如尾呼叫优化 ,使递归更快,但并不总是可能,并且不会以所有语言实现。

使用递归的主要原因是

  • 在许多情况下,当它模拟我们的问题的方法时,它更直观
  • 某些数据结构(如树)更容易使用递归来进行探索(或者在任何情况下都需要堆栈)

当然每个递归都可以被建模为一种循环:这就是CPU最终会做的。 而递归本身,更直接的意思是将函数调用和范围放在堆栈中。 但是将递归算法更改为循环算法可能需要大量工作,并使您的代码不太可维护:对于每个优化,只有当某些分析或证据显示出必要时才应该尝试。


Recursion is usually much slower because all function calls must be stored in a stack to allow the return back to the caller functions. In many cases, memory has to be allocated and copied to implement scope isolation.

Some optimizations, like tail call optimization, make recursions faster but aren't always possible, and aren't implemented in all languages.

The main reasons to use recursion are

  • that it's more intuitive in many cases when it mimics our approach of the problem
  • that some data structures like trees are easier to explore using recursion (or would need stacks in any case)

Of course every recursion can be modeled as a kind of loop : that's what the CPU will ultimately do. And the recursion itself, more directly, means putting the function calls and scopes in a stack. But changing your recursive algorithm to a looping one might need a lot of work and make your code less maintainable : as for every optimization, it should only be attempted when some profiling or evidence showed it to be necessary.

相关问答

更多