iPhone/iPad浏览网站时div之间的空白/间隙问题

12

我在iPhone和iPad上查看网站时遇到了一些困难。尽管网站还远没有完成,但是在我使用的每个桌面浏览器上(如safari、chrome、firefox、IE)都能正常显示。然而,在iPhone/iPad上,"内容" div、页眉和页脚之间存在微小的间隙/空白。我在页眉、内容和页脚 div 中使用背景图像来创建页面的轮廓。除了 div 之间的间隙之外,内容背景图像有时会被“缩小”一点,以致于它不能与屏幕右侧的页眉和页脚对齐。但如果我放大屏幕,一切都会完美地对齐。这是从iPhone上拍摄的屏幕截图...

http://s1139.photobucket.com/albums/n545/lhkimbrough/?action=view&current=7198e49b.jpg&evt=user_media_share

仔细看右侧蓝色导航栏下面。

这是 CSS(我知道它非常粗糙,我正在学习)。

@charset "UTF-8";
body {
 margin: 0;
 padding: 0;
 color: #000;
 font-family: Arial, Helvetica, sans-serif;
 font-size: 100%;
 line-height: 1.4;
 background-color: #CCC;
}
.TableAppName {
 color: #007EB4;
}

/* ~~ Element/tag selectors ~~ */
ul, ol, dl { /* Due to variations between browsers, it's best practices to zero padding and margin on lists. For consistency, you can either specify the amounts you want here, or on the list items (LI, DT, DD) they contain. Remember that what you do here will cascade to the .nav list unless you write a more specific selector. */
 padding: 0;
 margin: 0;
}
h1, h2, h3, h4, h5, h6, p {
 margin-top: 0;  /* removing the top margin gets around an issue where margins can escape from their containing div. The remaining bottom margin will hold it away from any elements that follow. */
 padding-right: 15px;
 padding-left: 15px; /* adding the padding to the sides of the elements within the divs, instead of the divs themselves, gets rid of any box model math. A nested div with side padding can also be used as an alternate method. */
}
a img { /* this selector removes the default blue border displayed in some browsers around an image when it is surrounded by a link */
 border: none;
}
/* ~~ Styling for your site's links must remain in this order - including the group of selectors that create the hover effect. ~~ */
a:link {
 color: #42413C;
 text-decoration: underline; /* unless you style your links to look extremely unique, it's best to provide underlines for quick visual identification */
}
a:visited {
 color: #6E6C64;
 text-decoration: underline;
}
#h-navbar {
 font-size: 95%;
 background-color: #CCC;
 background-image: url(images/navBarBlue.png);
 background-repeat: no-repeat;
 text-align: right;
 padding-right: 20px;
 color: #FFF;
 height: 39px;
 padding-top: 6px;
}
a:hover, a:active, a:focus { /* this group of selectors will give a keyboard navigator the same hover experience as the person using a mouse. */
 text-decoration: none;
}

/* ~~ this fixed width container surrounds the other divs ~~ */
.container {
 width: 874px; /* the auto value on the sides, coupled with the width, centers the layout */
 margin-top: 0;
 margin-right: auto;
 margin-bottom: 0;
 margin-left: auto;
 background-color: #CCC;
}

/* ~~ the header is not given a width. It will extend the full width of your layout. It contains an image placeholder that should be replaced with your own linked logo ~~ */
.header {
 background-color: #CCC;
 background-image: url(images/BannerFadedBlueAndOrangeText.png);
 background-repeat: no-repeat;
 height: 165px;
}

/* ~~ This is the layout information. ~~ 

1) Padding is only placed on the top and/or bottom of the div. The elements within this div have padding on their sides. This saves you from any "box model math". Keep in mind, if you add any side padding or border to the div itself, it will be added to the width you define to create the *total* width. You may also choose to remove the padding on the element in the div and place a second div within it with no width and the padding necessary for your design.

*/

.content {
 background-color: #CCC;
 background-image: url(images/contentFadedDark4.png);
 background-repeat: repeat-y;
 padding-left: 15px;
}
.contentBottom {
 padding: 0px;
 margin: 0px;
}

/* ~~ The footer ~~ */
.footer {
 background-color: #CCC;
 background-image: url(images/FooterFadedDark.png);
 background-repeat: no-repeat;
 text-align: right;
 font-size: 80%;
}

/* ~~ miscellaneous float/clear classes ~~ */
.fltrt {  /* this class can be used to float an element right in your page. The floated element must precede the element it should be next to on the page. */
 float: right;
 margin-left: 8px;
}
.fltlft { /* this class can be used to float an element left in your page. The floated element must precede the element it should be next to on the page. */
 float: left;
 margin-right: 8px;
}
.clearfloat { /* this class can be placed on a <br /> or empty div as the final element following the last floated div (within the #container) if the #footer is removed or taken out of the #container */
 clear:both;
 height:0;
 font-size: 1px;
 line-height: 0px;
}
#h-navbar a, #h-navbar a:visited {
 text-decoration: none;
 color: #FFF;
}
#h-navbar a:hover {
 color: #ea6828;
}
.footer p {
 color: #0075A7;
}
.content h1 {
 color: #0074A9;
 font-weight: normal;
 font-size: 200%;
 font-style: normal;
 padding-top: 15px;
 padding-left: 23px;
}
.graytext {
 color: #333;
}

这里是HTML代码

<!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"><!-- InstanceBegin template="/Templates/LZKConcepts_Template.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>LZK Concepts</title>
<!-- InstanceEndEditable -->
<link href="LZKConcepts_styles.css" rel="stylesheet" type="text/css" />
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>

<div class="container">
  <div class="header"><!-- end .header --></div>
  <div id="h-navbar"><a href="index.html"> Home </a> | <a href="#"> QuickGrader </a> | <a href="#"> Wish On A Star </a> | <a href="AboutUs.html"> About Us </a> | <a href="#"> Contact Us </a></div>
  <!-- InstanceBeginEditable name="MainContent" -->
  <div class="content">
    <h1>
      <!-- end .content -->
    iPhone Apps</h1>
    <table width="844" border="0">
      <tr>
        <td width="160" rowspan="2" valign="top"><a href="#"><img src="images/QuickGraderIconReflected140.png" alt="QuickGrader" width="120" height="154" hspace="20" /></a></td>
        <td width="482" class="TableAppName">QuickGrader</td>
        <td width="188">&nbsp;</td>
      </tr>
      <tr>
        <td height="127" valign="top" class="graytext">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris neque quam, facilisis sed iaculis tempor, iaculis vitae purus. Mauris eros lacus, commodo ut feugiat vel, sollicitudin et elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur urna diam, iaculis ut commodo molestie, congue non lacus. </td>
        <td align="right" valign="top"><br />
        <a href="http://itunes.apple.com/app/quickgrader/id392532471?mt=8" target="_new"><img src="images/App_Store_Badge.png" alt="AppStore" width="155" height="54" hspace="18" vspace="0" align="right" /></a></td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <table width="844" border="0">
      <tr>
        <td width="160" rowspan="2" valign="top"><a href="#"><img src="images/Wish On A Star Reflected.png" alt="QuickGrader" width="120" height="154" hspace="20" /></a></td>
        <td width="482" class="TableAppName">Wish On A Star</td>
        <td width="188">&nbsp;</td>
      </tr>
      <tr>
        <td height="130" valign="top">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris neque quam, facilisis sed iaculis tempor, iaculis vitae purus. Mauris eros lacus, commodo ut feugiat vel, sollicitudin et elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur urna diam, iaculis ut commodo molestie, congue non lacus. </td>
        <td align="right" valign="top"><br />
          <a href="http://itunes.apple.com/app/wish-on-a-star/id366753945?mt=8" target="_blank"><img src="images/App_Store_Badge.png" alt="AppStore" width="155" height="54" hspace="18" align="right" /></a></td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <table width="844" border="0">
      <tr>
        <td width="160" rowspan="2" valign="top"><a href="#"><img src="images/Wish On A Star Free Reflected.png" alt="QuickGrader" width="120" height="154" hspace="20" /></a></td>
        <td width="482" class="TableAppName">Wish On A Star Free</td>
        <td width="188">&nbsp;</td>
      </tr>
      <tr>
        <td height="130" valign="top">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris neque quam, facilisis sed iaculis tempor, iaculis vitae purus. Mauris eros lacus, commodo ut feugiat vel, sollicitudin et elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur urna diam, iaculis ut commodo molestie, congue non lacus. </td>
        <td align="right" valign="top"><br />
          <a href="http://itunes.apple.com/app/wish-on-a-star-free/id388860624?mt=8"><img src="images/App_Store_Badge.png" alt="AppStore" width="155" height="54" hspace="18" align="right" /></a></td>
      </tr>
    </table>
  </div>
  <!-- InstanceEndEditable -->
  <div class="footer">
    <p><br />
<br />
      <br />
    Copyright © 2010, LZK Concepts. All rights reserved.</p>
</div>
  <!-- end .container --></div>
</body>
<!-- InstanceEnd --></html>

有人知道为什么iPhone和iPad会以这种方式渲染页面吗?


1
这是与+1有关的编程内容——不仅在iPad上,而且在其他支持全页缩放的浏览器中也会出现。在iPad上看到ING银行网站首页出现此效果后,我搜索了一下此问题。 - Patrick Harrington
2个回答

15
在iPad上,当网站缩放到100%以下时,有时会出现一些伪影。其中一个尤其明显:在菜单下方的div之间有一条1像素的线,就像你的网站一样。
我找到的解决方法是在div之间添加“margin: -1px”。 div的背景与1像素重叠,如果按照相应的切片进行划分,则对于经典浏览器而言是不可见的,并且可以在iPad上纠正问题。

6
很遗憾我们必须使用这么多浏览器Hack :-( 标准需要更加得到尊重。 - Bojangles
我完全同意你的观点!但是另一方面,要想在不偶尔出现奇怪问题的情况下扩展网页并不是那么简单的事情... - plang
Plang提供的绝妙解决方案也为我解决了这个问题!只是想知道是否有另一种不那么“hack-y”的方法来避免“scaling artefacts”。 - pvandenberk
问题在于,如果您的div的不透明度设置小于1,则如果在iPad Safari以外的浏览器上查看网站,则重叠部分将可见。 - 123
谢谢,这是一个非常简单的解决方案,节省了我很多时间并修复了问题。 - Scumble373
如果这是两个图像,-1像素的边距将在除了iPad设备之外的所有其他设备上产生可见的扭曲。这解决了什么问题吗? - undefined

4

使用:

meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0;"

当网站在平板电脑或其他设备上显示时,应该去除您看到的像素差异。现在,网站应该被设计成语义可伸缩的,以实现最大的跨设备访问。

至于我,如果绝对需要解决问题,我通常会构建一个php设备检测器。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接