首先是em的計算方式,當本元素和父級元素沒有指定以em或者百分比為單位的font-size時,1em嚴格等於16px,當本元素和父級元素指定了以em或者百分比為單位的font-size時,則在16px的基礎上乘以所有上級fontSize的積。
由這個性質 可以通過設定line-height來代替zoom以達到想要的效果。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>CSS單位em</title> <style type="text/css"> span{ font-size:16px; } .domain{ padding:15px; } </style> </head> <body> <span>正常的1em=16px</span> <div ></div> <div class="domain" > <span>style="font-size:2em;border:solid 1px red;"這裡1em=2x16=32px</span> <div ></div> <div class="domain" > <span>style="font-size:3em;border:solid 1px blue;"這裡1em=2x3x16=96px</span> <div ></div> <div class="domain" > <span>style="font-size:50%;border:solid 1px black;"這裡1em=2x3x50%x16=48px</span> <div ></div> </div> </div> </div> </body> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]