利用@media screen實現網頁布局的自適應

來源:互聯網
上載者:User

標籤:style   class   blog   code   java   http   

經常為不同解析度裝置或不同視窗大小下布局錯位而頭疼,可以利用@media screen實現網頁布局的自適應,
優點:無需外掛程式和手機主題,對行動裝置友好,能夠適應各種視窗大小
只需在CSS中添加@media screen屬性,根據瀏覽器寬度判斷並輸出不同的長寬值
以下是針對自用主題而寫的css,對寬度768以下裝置只保留主要文章架構,以便在有限的空間裡獲得最佳閱讀體驗

@media screen and (min-width:1200px){    #page{ width: 1100px; }
   #content,.div1{width: 730px;}
#secondary{width:310px}}@media screen and (min-width: 960px) and (max-width: 1199px) { #page{ width: 960px; }
#content,.div1{width: 650px;}
#secondary{width:250px}
  select{max-width:200px}}@media screen and (min-width: 768px) and (max-width: 959px) { #page{ width: 900px; }
  #content,.div1{width: 620px;}
  #secondary{width:220px}
  select{max-width:180px}}@media only screen and (min-width: 480px) and (max-width: 767px){ #page{ width: 450px; }
  #content,.div1{width: 420px;position: relative; }
  #secondary{display:none}
  #access{width: 450px; }
  #access a {padding-right:5px}
  #access a img{display:none}
  #rss{display:none}
  #branding #s{display:none}}@media only screen and (max-width: 479px) { #page{ width: 300px; }#content,.div1{width: 300px;}#secondary{display:none}#access{width: 330px;} #access a {padding-right:10px;padding-left:10px}#access a img{display:none}#rss{display:none}#branding #s{display:none}#access ul ul a{width:100px}}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.