CSS3 Media Queries的介绍在W3CPlus上的介绍已有好几篇文章了,但自己碰到的问题与解决的文章还是相对的较少。前几天在《修复iPhone上submit按钮bug》上介绍了修复form中sumit按钮的bug。同一个项目,为了实现iPhone和iPad横板与竖板的风格,让我还是头疼了一翻。

一开始按照CSS3 Media Queries模板中的介绍来运用,虽然帮我解决了iPad的横板与竖板的风格渲染问题,但在iPhone4上还是存在问题的。后来结过网上的查找,总算是问题解决了,下面就来看看问题是如何解决的。

在具体开始之前,先来看看他的源码:

  1. <!DOCTYPE HTML>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  6. <title>login form</title>
  7. <style type="text/css" media="screen">
  8. button,
  9. input{
  10. margin: 0;
  11. font-size: 100%;
  12. vertical-align: middle;
  13. *overflow: visible;
  14. line-height: normal;
  15. }
  16. button::-moz-focus-inner,
  17. input::-moz-focus-inner {
  18. padding: 0;
  19. border: 0;
  20. }
  21. button,
  22. input[type="button"],
  23. input[type="reset"],
  24. input[type="submit"] {
  25. cursor: pointer;
  26. -webkit-appearance: button;
  27. }
  28.  
  29. body {
  30. margin: 0;
  31. padding: 58px 2% 2%;
  32. background: -webkit-gradient(
  33. linear,
  34. left top,
  35. left bottom,
  36. color-stop(0%,rgba(61,157,179,1)),
  37. color-stop(15%,rgba(61,157,179,0.3)),
  38. color-stop(40%,rgba(61,157,179,.5)),
  39. color-stop(70%,rgba(239,239,239,.4)),
  40. color-stop(100%,rgba(239,239,239,0.3)));
  41. background: -webkit-linear-gradient(
  42. top,
  43. rgba(61,157,179,1) 0%,
  44. rgba(61,157,179,0.8) 15%,
  45. rgba(61,157,179,.5) 40%,
  46. rgba(239,239,239,.4) 70%,
  47. rgba(239,239,239,0.3) 100%);
  48. background-repeat: no-repeat;
  49. background-attachment: fixed;
  50. }
  51. form {
  52. margin: 0;
  53. padding: 0;
  54. }
  55. form fieldset {
  56. border: none;
  57. border-radius: 10px;
  58. box-shadow: inset 0 0 2px rgba(0,0,0,0.3),0 0 5px rgba(0,0,0,0.13);
  59. background-color: rgba(255,255,255,0.8);
  60. background-image: -webkit-gradient(linear, 0 0, 0 100%, from(rgba(255,255,255,0.8)), to(rgba(216,216,216,0.8)));
  61. background-image: -webkit-linear-gradient(top,rgba(255,255,255,0.8), rgba(216,216,216,0.8));
  62. background-repeat: repeat-x;
  63. padding:20px 5%;
  64. margin: 0;
  65. position: relative;
  66. border: 1px solid rgba(216,216,216,0.8);
  67. }
  68. .control-group {
  69. margin-bottom: 15px;
  70. }
  71.  
  72. .control-label {
  73. color: #405c60;
  74. display:block;
  75. line-height: 18px;
  76. font-weight: normal;
  77. font-size: 16px;
  78. display:inline-block;
  79. min-width: 80px;
  80. max-width: 80px;
  81. text-align: right;
  82. margin-top: 12px;
  83. vertical-align: top;
  84. }
  85. .controls {
  86. display: inline-block;
  87. vertical-align: top;
  88. width: 65%;
  89. }
  90. input {
  91. outline: medium none;
  92. }
  93. input:not([type="checkbox"]){
  94. width: 90%;
  95. margin-top: 4px;
  96. padding: 10px;
  97. border: 1px solid rgb(178, 178, 178);
  98. -webkit-appearance: textfield;
  99. -webkit-box-sizing: content-box;
  100. box-sizing : content-box;
  101. border-radius: 3px;
  102. box-shadow: 0px 1px 4px 0px rgba(168, 168, 168, 0.6) inset;
  103. -webkit-transition: all 0.2s linear;
  104. transition: all 0.2s linear;
  105. }
  106. input:not([type="checkbox"]):active,
  107. input:not([type="checkbox"]):focus{
  108. border: 1px solid rgba(91, 90, 90, 0.7);
  109. background: rgba(238, 236, 240, 0.2);
  110. box-shadow: 0px 1px 4px 0px rgba(168, 168, 168, 0.9) inset;
  111. }
  112. .form-actions {
  113. text-align: center;
  114. }
  115. .form-actions input{
  116. width: 30%;
  117. cursor: pointer;
  118. background: rgb(61, 157, 179);
  119. padding: 8px 5px;
  120. font-family: 'BebasNeueRegular','Arial Narrow',Arial,sans-serif;
  121. color: #fff;
  122. font-size: 24px;
  123. margin: 5px;
  124. border: 1px solid rgb(28, 108, 122);
  125. margin-bottom: 10px;
  126. text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);
  127. border-radius: 3px;
  128. box-shadow:0px 1px 6px 4px rgba(0, 0, 0, 0.07) inset,
  129. 0px 0px 0px 3px rgb(254, 254, 254),
  130. 0px 5px 3px 3px rgb(210, 210, 210);
  131. -webkit-transition: all 0.2s linear;
  132. transition: all 0.2s linear;
  133. -webkit-appearance: none;
  134. }
  135. .form-actions input:hover{
  136. background: rgb(74, 179, 198);
  137. }
  138. .form-actions input:active,
  139. .form-actions input:focus{
  140. background: rgb(40, 137, 154);
  141. position: relative;
  142. top: 1px;
  143. border: 1px solid rgb(12, 76, 87);
  144. box-shadow: 0px 1px 6px 4px rgba(0, 0, 0, 0.2) inset;
  145. }
  146. </style>
  147. </head>
  148. <body>
  149. <form name="form1" method="post" action="2.html" onsubmit="return check()" class="form-horizontal">
  150. <fieldset>
  151. <div class="control-group">
  152. <label for="text1" class="control-label">用户名:</label>
  153. <div class="controls">
  154. <input id="username_reg" type="text" name="text1" onblur="check()" required="required" />
  155. <div class="help-inline" id="div1"></div>
  156. </div>
  157. </div>
  158. <div class="control-group">
  159. <label for="text2" class="control-label">密码:</label>
  160. <div class="controls">
  161. <input id="password_reg" type="password" name="text2" onblur="check()" required="required">
  162. <div class="help-inline" id="div2"></div>
  163. </div>
  164. </div>
  165. <div class="control-group">
  166. <label for="text3" class="control-label">确认密码:</label>
  167. <div class="controls">
  168. <input id="confirm_reg" type="password" name="text3" onblur="check()" required="required" />
  169. <div class="help-inline" id="div3"></div>
  170. </div>
  171. </div>
  172.  
  173. <div class="form-actions">
  174. <input type="button" value="提交" name="tect6" class="btn btn-primary" onclick="reg()" />
  175. <input type="reset" value="重置" name="text7" class="btn" />
  176. </div>
  177. </fieldset>
  178. </form>
  179. </body>
  180. </html>

现在在iPhone4竖板是OK的,如下图所示

但现在需求是,在iPhone4的横板以及iPad的横板与竖板下,也需要让表单居中显示:

上图显示的是iPad竖板下的需求,横板下也需要类似的效果。

需求明确,做法也是有思路的,首先我按照:CSS3 Media Queries模板中的模板在样式中增加了代码:

  1. /*iPad竖板*/
  2. @media screen and (max-device-width: 768px) and (orientation: portrait) {
  3. form {
  4. margin: 0 25%;
  5. }
  6. }
  7. /*iPad横板*/
  8. @media screen and (max-device-width: 1024px) and (orientation: landscape) {
  9. form {
  10. margin: 0 25%;
  11. }
  12. }
  13. /*iPhone4*/
  14. @media only screen and (max-device-width: 480px),
  15. only screen and (-webkit-min-device-pixel-ratio: 2) {
  16. form {
  17. margin: 0 ;
  18. }
  19. .controls {
  20. width: 68%;
  21. }
  22. }

加上上面代码后在iPad下是正常了,而且不会有什么问题存在。但在iPhone4上依然存在一个怪异的现象:当你iPhone4加载页面是用横板加载,再旋转到竖板,不会存在任何问题,而且显示也是蛮正常的。但是初始加载页面是竖板,然后在旋转到横板,就有问题了,表单给放大了

初步给我感觉就是放大了,但具体是不是因为这个原因,我也不太清楚,查找了一些资料,也尝试了无解决办法。最后也从横板竖板的思路出发查找资料,经过一试,给解决了,下面来看看这段代码:

  1. /* iPhone 4 竖板 */
  2. @media
  3. only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:portrait),
  4. only screen and (min-device-pixel-ratio : 1.5) and (orientation:portrait){
  5. form {
  6. margin:0;
  7. }
  8. .controls {
  9. width: 68%;
  10. }
  11. }
  12.  
  13. /* iPhone 4 横板 */
  14. @media
  15. only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:landscape),
  16. only screen and (min-device-pixel-ratio : 1.5) and (orientation:landscape){
  17. form {
  18. margin:0 10%;
  19. }
  20. .controls {
  21. width: 68%;
  22. }
  23. }

这样一来就OK了。在iPhone4和iPad的横竖板下都能正常让表单居中显示。

那么以后大家在iPhone4和iPad设备上,就可以按照横竖板来定样式了:

1、iPhone4竖板

  1. @media
  2. only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:portrait),
  3. only screen and (min-device-pixel-ratio : 1.5) and (orientation:portrait){
  4. /*你的样式写在这里*/
  5. }

2、iPhone横板

  1. @media
  2. only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:landscape),
  3. only screen and (min-device-pixel-ratio : 1.5) and (orientation:landscape){
  4. /*你的样式写在这里*/
  5. }

3、iPad竖板

  1. @media screen and (max-device-width: 768px) and (orientation: portrait) {
  2. /*你的样式写在这里*/
  3. }

4、iPad横板

  1. @media screen and (max-device-width: 1024px) and (orientation: landscape) {
  2. /*你样式写在这里*/
  3. }

上面四种CSS3 Media Queries就是用来对付iPhone4和iPad的,至于其他的运用,大家参考下面我重新整理的CSS3 Media Queries模板:

CSS3 Media Queries 模板:

1、Smartphones (竖板和横板)

  1. @media only screen and (min-device-width : 320px) and (max-device-width : 480px) {
  2. /* 你的样式写在这里 */
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="smartphones.css" media="only screen and (min-device-width : 320px) and (max-device-width : 480px)" />

2、Smartphones (横板)

  1. @media only screen and (min-width : 321px) {
  2. /* 你的样式写在这里 */
  3. }
  4.  
  5. //===或者====//
  6. < link rel="stylesheet" href="smartphones-landscape.css" media="only screen and (min-width : 321px)" />

3、Smartphones (竖板)

  1. @media only screen and (max-width : 320px) {
  2. /* 你的样式写在这里 */
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="smartphones-portrait.css" media="only screen and (max-width : 320px)" />

4、iPad(竖板和横板)

  1. @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) {
  2. /* 你的样式写在这里 */
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="ipad.css" media="only screen and (min-device-width : 768px) and (max-device-width : 1024px)" />

5、iPad横板

  1. @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape) {
  2. /* 你的样式写在这里 */
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="ipad-landscape.css" media="only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape)" />

6、iPad的竖板

  1. @media only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait) {
  2. /* 你的样式写在这里 */
  3. }
  4.  
  5. //===或者====//
  6. < link rel="stylesheet" href="ipad-portrait.css" media="only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)" />

7、Desktops and laptops

  1. @media only screen and (min-width : 1224px) {
  2. /* 你的样式写在这里 */
  3. }
  4.  
  5. //===或者====//
  6. < link rel="stylesheet" href="desktops-and-laptops.css" media="only screen and (min-width : 1224px)" />

8、Large screens

  1. @media only screen and (min-width : 1824px) {
  2. /* 你的样式写在这里 */
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="large-screens.css" media="only screen and (min-width : 1824px)" />

9、iPone4

  1. @media only screen and (-webkit-min-device-pixel-ratio : 1.5),only screen and (min-device-pixel-ratio : 1.5) {
  2. /* 你的样式写在这里 */
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="iphone4.css" media="only screen and (-webkit-min-device-pixel-ratio : 1.5),only screen and (min-device-pixel-ratio : 1.5)" />

10、iPhone4竖板

  1. @media only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:portrait), only screen and (min-device-pixel-ratio : 1.5) and (orientation:portrait){
  2. /*你的样式写在这里*/
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="iphone4-portrait.css" media="only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:portrait), only screen and (min-device-pixel-ratio : 1.5) and (orientation:portrait)" />

11、iPhone4横板

  1. @media only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:landscape),only screen and (min-device-pixel-ratio : 1.5) and (orientation:landscape){
  2. /*你的样式写在这里*/
  3. }
  4. //===或者====//
  5. < link rel="stylesheet" href="iphone4-landscape.css" media="only screen and (-webkit-min-device-pixel-ratio : 1.5) and (orientation:landscape),only screen and (min-device-pixel-ratio : 1.5) and (orientation:landscape)" />

上面罗列了常用的CSS3 Media Queries模板,特别是在移动设备上的几种,希望对大家在今后的移动开发端上的运用有所帮助。当然CSS3 Media Queries的运用条件往往不只这些,大家完全可以根据自己的需求去定义不同的条件,但个人建议,使用CSS3 Media Queries采用主流就Ok了。随着潮流走嘛。那么今天就扯到这了。如果大家有更好的经验,可以通过下面的评论与我们一起分享。

CSS3 Media Queries在iPhone4和iPad上的运用的更多相关文章

  1. [转]How To Use CSS3 Media Queries To Create a Mobile Version of Your Website

    CSS3 continues to both excite and frustrate web designers and developers. We are excited about the p ...

  2. CSS3 Media Queries 实现响应式设计

    在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...

  3. 【CSS3 入门教程系列】CSS3 Media Queries 实现响应式设计

    在 CSS2 中,你可以为不同的媒介设备(如屏幕.打印机)指定专用的样式表,而现在借助 CSS3 的 Media Queries 特性,可以更为有效的实现这个功能.你可以为媒介类型添加某些条件,检测设 ...

  4. CSS3 Media Queries 片段

    CSS3 Media Queries片段 在这里主要分成三类:移动端.PC端以及一些常见的响应式框架的Media Queries片段. 移动端Media Queries片段 iPhone5 @medi ...

  5. HTML5实践 -- 使用CSS3 Media Queries实现响应式设计

    CSS3 Media用法介绍:http://www.w3cplus.com/content/css3-media-queries 转载请注明原创地址:http://www.cnblogs.com/so ...

  6. CSS3 Media Queries模板

    使用max-width @media screen and (max-width: 600px) { //你的样式放在这里.... } 使用min-width @media screen and (m ...

  7. CSS3 Media Queries 详解

    说起CSS3的新特性,就不得不提到 Media Queries .最近 Max Design 更新的一个泛读列表里,赫然就有关于 Media Queries 的文章.同时位列其中的也有前天我刚刚翻译的 ...

  8. 【css】CSS3 Media Queries 详解【转】

    说起CSS3的新特性,就不得不提到 Media Queries .最近 Max Design 更新的一个泛读列表里,赫然就有关于 Media Queries 的文章.同时位列其中的也有前天我刚刚翻译的 ...

  9. CSS3 Media Queries模板:max-width和min-width

    CSS3 Media Queries模板 CSS3 Media Queries一般都是使用“max-width”和“min-width”两个属性来检查各种设备的分辨大小与样式表所设条件是否满足,如果满 ...

随机推荐

  1. 集群ssh服务和免密码登录的配置

    安装Hadoop之前,由于集群中大量主机进行分布式计算需要相互进行数据通信,服务器之间的连接需要通过ssh来进行,所以要安装ssh服务,默认情况下通过ssh登录服务器需要输入用户名和密码进行连接,如果 ...

  2. mysql生成varchar类型主键排序

    用uuid生成20位的主键 SELECT LEFT(REPLACE(UUID(), '-', ''),20) FROM DUAL 另一种方法: 因为数据库中有字母 需要排序的时候去除字母,重新取最大值 ...

  3. ubuntu 下wireshark 软件安装与使用

    在ubuntu下,使用wireshark也是很有必要的.虽然可以使用tcpdump等工具. ubuntu:11.10     1. sudo apt-get install wireshark     ...

  4. Stanford大学机器学习公开课(六):朴素贝叶斯多项式模型、神经网络、SVM初步

    (一)朴素贝叶斯多项式事件模型 在上篇笔记中,那个最基本的NB模型被称为多元伯努利事件模型(Multivariate Bernoulli Event Model,以下简称 NB-MBEM).该模型有多 ...

  5. Linux配置SSH公钥认证与Jenkins远程登录进行自动发布

    公钥认证,是使用一对加密字符串,一个称为公钥(public key), 任何人都可以看到其内容,用于加密:另一个称为密钥(private key),只有拥有者才能看到,用于解密. 在使用jenkins ...

  6. sizeof(class)

    //#define _REENTRANT //#define _POSIX_C_SOURCE #include <iostream> #include <string> #in ...

  7. C#正则表达式分组使用

    string s = "dfasfdasfdsa*fdasfdsafdsa"; s = "转发:dfasfdasfdsa*fdasfdsafdsa"; //s ...

  8. 利用jQuery实现CheckBox全选/全不选/反选

    转自:http://www.cnblogs.com/linjiqin/p/3148259.html jQuery有些版本中实现CheckBox全选/全不选/反选会有bug,经测试jquery-1.3. ...

  9. 输入框提示文字js

    <input style="margin-right: 0px; padding-right: 0px;" class="text" required=& ...

  10. HTTP基础04--web服务器

    单台虚拟主机实现多个域名 由于虚拟服务器的功能,一台HTTP服务器可以搭建多个Web站点: 在互联网上,域名通过 DNS 服务映射到 IP 地址(域名解析)之后访问目标网站.当请求发送到服务器时,已经 ...