1分鐘讀懂移動端em和rem的區別

來源:互聯網
上載者:User

標籤:根據   梳理   inf   pre   color   有一個   nta   教程   分享圖片   

 

rem和em其實都是相對單位,但rem是想對於html根項目字型大小,而em是相當於使用em單位的元素字型大小大小

教程幫你梳理清楚!

html布局:

<div class="container">
  <div class="use_em"></div>
  <div class="use_rem"></div>
</div>

 

css樣式:

html{
 font-size: 40px;
}
.container{
  font-size: 20px;
}
.use_em{
  width: 2em;
  height: 2em;
  background-color: blue;
}
.use_rem{
  width: 2rem;
  height: 2rem;
  background-color: green;
}

  

結果:

 

這時候你可以看到,使用rem單位的盒子繼承的是html根字型大小大小;在這裡em是繼承了父盒子的字型大小大小,但有一個比較普遍的誤解,認為單位就是相對於父元素的字型大小。 事實上,根據W3標準,它們是相對於使用單位的元素的字型大小。

 

我們再來驗證下這個說法,當我們在使用em的.use_em盒子上加font-size: 50px;,這時候你會發現盒子變大了,繼承的不再是父盒子container的字型大小大小,這時變成了相對於使用em單位的盒子的字型大小

html{  font-size: 40px;}.container{  font-size: 20px;}.use_em{  font-size: 50px;  width: 2em;  height: 2em;  background-color: blue;}.use_rem{  width: 2rem;  height: 2rem;  background-color: green;}

  

 

1分鐘讀懂移動端em和rem的區別

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.