1

当我在这些浏览器中增加缩放级别时,我在 Firefox 和 chrome 中都看到了一些奇怪的东西,尽管我的 CSS 没有任何问题。这是整个故事:

我有一个右浮动的顶级 div 包含三个右浮动的权利。三个内部 div 具有所有以像素为单位的盒子模型测量值,它们加起来就是封闭容器的宽度。当浏览器大小为 100% 时,一切看起来都很好,但是当我开始使用 CTRL+滚轮或 CTRL+减号使浏览器变小时,最右边的边距缩小得太快并最终变为零,迫使我最右边的浮动内部 div 下降到低于另外两个!

我无法理解这一点,几乎似乎在浏览器代码中执行了一些整数除法不正确,但唉,firefox 和 chrome 都显示相同的结果。

这是示例(只需使用 CTRL-减号缩小即可了解我的意思):

单击此处查看我在示例网站上的意思

只是为了缩小范围,感兴趣的标签如下:

div#mainContent
div#contentLeft
div#contentCenter
div#contentRight

我在 stackoverflow 中搜索了答案,发现以下帖子似乎与我的问题相关,但无法将它们应用于我遇到的问题:

http://stackoverflow.com/questions/6955313/div-moves-incorrectly-on-browser-resize

http://stackoverflow.com/questions/18246882/divs-move-when-resizing-page

http://stackoverflow.com/questions/17637231/moving-an-image-when-browser-resized

http://stackoverflow.com/questions/5316380/how-to-stop-divs-moving-when-the-browser-is-resized

为了您的方便,我复制了下面的 html 和 css 代码:

这是HTML:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pinco</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
  <div id="wrapper">
    <header>
      <div class="logo">
        <a href="http://pinco.com">
          <img class="logo" src="images/PincoLogo5.png" alt="Pinco" />
        </a>
      </div>
      <div class="titolo">
        <h1>Benvenuti!</h1>
        <h2>Siete arrivati al sito pinco.</h2>
      </div>
      <nav>
        <ul class="menu"> 
          <li><a href="#">Menù Qui</a></li>
          <li><a href="#">Menù Quo</a></li>
          <li><a href="#">Menù Qua</a></li>
        </ul>
      </nav>
    </header>
    <div id="mainContent">
      <div id="contentLeft">
        <section>
          <article>
            <p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempor turpis est, nec varius est pharetra scelerisque. Sed eu pellentesque purus, at cursus nisi. In bibendum tristique nunc eu mattis. Nulla pretium tincidunt ipsum, non imperdiet metus tincidunt ac. In et lobortis elit, nec lobortis purus. Cras ac viverra risus. Proin dapibus tortor justo, a vulputate ipsum lacinia sed. In hac habitasse platea dictumst. Phasellus sit amet malesuada velit. Fusce diam neque, cursus id dui ac, blandit vehicula tortor.

Phasellus interdum ipsum eu leo condimentum, in dignissim erat tincidunt. Ut fermentum consectetur tellus, dignissim volutpat orci suscipit ac. Praesent scelerisque urna metus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis pulvinar, sem a sodales eleifend, odio elit blandit risus, a dapibus ligula orci non augue. Nullam vitae cursus tortor, eget malesuada lectus. Nulla facilisi. Cras pharetra nisi sit amet orci dignissim, a eleifend odio hendrerit.
            </p>
          </article>
        </section>
      </div>
      <div id="contentCenter">
        <section>
          <article>
            <p>
Maecenas vitae purus at orci euismod pretium. Nam gravida gravida bibendum. Donec nec dolor vel magna consequat laoreet in a urna. Phasellus cursus ultrices lorem ut sagittis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vivamus purus felis, ornare quis ante vel, commodo scelerisque tortor. Integer vel facilisis mauris.
            </p>
            <img src="images/auto1.jpg" width="272" height="272" />
            <p>
In urna purus, fringilla a urna a, ultrices convallis orci. Duis mattis sit amet leo sed luctus. Donec nec sem non nunc mattis semper quis vitae enim. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Suspendisse dictum porta quam, vel lobortis enim bibendum et. Donec iaculis tortor id metus interdum, hendrerit tincidunt orci tempor. Sed dignissim cursus mattis.
            </p>
          </article>
        </section>
      </div>
      <div id="contentRight">
        <section>
          <article>
            <img src="images/auto2.jpg" width="272" height="272" />
            <img src="images/auto3.jpg" width="272" height="272" />
            <p>
Cras eu quam lobortis, sodales felis ultricies, rhoncus neque. Aenean nisi eros, blandit ac lacus sit amet, vulputate sodales mi. Nunc eget purus ultricies, aliquam quam sit amet, porttitor velit. In imperdiet justo in quam tristique, eget semper nisi pellentesque. Cras fringilla eros enim, in euismod nisl imperdiet ac.

Fusce tempor justo vitae faucibus luctus.
            </p>
          </article>
        </section>
      </div>
    </div>
    <footer>
      <div class="footerText">
        <p>
Copyright &copy; Pinco
<br />Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<br />Fusce ornare turpis orci, nec egestas leo feugiat ac.
<br />Morbi eget sem facilisis, laoreet erat ut, tristique odio. Proin sollicitudin quis nisi id consequat.
        </p>
      </div>
      <div class="footerLogo">
         <img class="footerLogo" src="images/auto4.jpg" width="80" height="80" />
      </div>
    </footer>
  </div>
</body>
</html>

这是CSS:

/* CSS Document */

* { margin: 0; border: 0; padding: 0; }

body { background: #8B0000; /* darkred */; }

body { margin: 0; border: 0; padding: 0; }

div#wrapper { margin: 0 auto; width: 960px; height: 100%; background: #FFC0CB /* pink */; }

header { position: relative; background: #005b97; height: 140px; }

header div.logo { float: left; width: 360px; height: 140px; }
header div.logo img.logo { width: 360px; height: 140px;  }

header div.titolo { float: left; padding: 12px 0 0 35px; color: black; }
header div.titolo h1 { font-size: 36px; font-weight: bold; }
header div.titolo h2 { font-size: 24px; font-style: italic; font-weight: bold; color: white;}

header nav { position: absolute; right: 0; bottom: 0; }

header ul.menu { background: black; }
header ul.menu li { display: inline-block; padding: 3px 15px; font-weight: bold; }

div#mainContent { float: left; width: 100%; /* width: 960px; *//* height: 860px; */ padding: 30px 0; text-align: justify; }

div#mainContent img { margin: 12px 0; }

div#contentLeft { height: 900px; float: left; margin-left: 12px; border: 1px solid black; padding: 15px; width: 272px; background: #ccc; }

div#contentCenter { height: 900px; float: left; margin-left: 12px; border: 1px solid transparent; padding: 15px; width: 272px; background: #E00; }

div#contentRight { height: 900px; float: left; margin-left: 12px; border: 1px solid black; padding: 15px; width: 272px; background: #ccc; }

footer { clear: both; padding: 12px; background: #306; color: white; height: 80px; font-style: italic; font-weight: bold; }

footer div.footerText { float: left; }

footer div.footerLogo { float: right; }

a { color: white; text-decoration: none; }

编辑1:

我再次检查了所有测量值并仔细插入数字,直到它们满足以下等式:

TEXT_AREA * 3 + MARGIN * 4 + BORDER * 6 = 960px(包装的宽度)

TEXT_AREA = 宽度 + 2 * 填充

边框 = 1

当然,要注意将边距和填充设置为合理的值,这里有一些数字似乎可以解决这些限制:

TEXT_AREA = 290 像素

边距 = 15 像素

边框 = 1 像素

填充 = 15 像素

宽度 = 268 像素

这转化为以下内容:

div#mainContent { float: left; width: 960px; padding: 15px 0; text-align: justify; }

div#contentLeft { height: 900px; float: left; margin: 0 0 0 15px; border: 1px solid black; padding: 15px; width: 268px; background: #ccc; }

div#contentCenter { height: 900px; float: left; margin: 0 0 0 15px; border: 1px solid transparent; padding: 15px; width: 268px; background: #E00; }

div#contentRight { height: 900px; float: left; margin: 0 15px 0 15px; border: 1px solid black; padding: 15px; width: 268px; background: #ccc; }

然而,即使现在一切都是统一的,我仍然遇到这样的问题,即当我缩小最右边的列时,它会落在其他列之下。一种解决方案是执行以下操作:

div#contentRight { height: 900px; float: left; margin: 0 0 /* 15px */ 0 15px; border: 1px solid black; padding: 15px; width: 268px; background: #ccc; }

所以现在右列没有右边距,但视觉结果是一样的。现在,当我缩小时,最右边的列并没有下降,但是它的右边距与其他列相比是如此之小,所以说真的,还有一个小问题。

编辑2:

好的,我进行了更多搜索并找到了更好的解决方案。解决方案包括让 div 列元素之间的边距相同,并自动调整左右边距。为了实现这一点,我不得不取消浮动,并使用“display:inline-block”,恕我直言,它比浮动更合适,并且是为手头的目的而设计的:

div#mainContent { padding: 15px 0; width: 960px; text-align: center; }

div#contentLeft { display: inline-block; vertical-align: top; height: 900px; margin: 0 0 0 0/*15px*/; border: 1px solid black; padding: 15px; width: 268px; background: #ccc; }

div#contentCenter { display: inline-block; vertical-align: top; height: 900px; margin: 0 0 0 15px; border: 1px solid transparent; padding: 15px; width: 268px; background: #E00; }

div#contentRight { display: inline-block; vertical-align: top; height: 900px; margin: 0 0/* 15px */ 0 15px; border: 1px solid black; padding: 15px; width: 268px; background: #ccc; }

div#mainContent 部分 { 文本对齐:对齐;}

现在,在正常缩放级别下,所有左边距将是 15px 加上最后一个元素的右边距也将是 15px。另一方面,如果我缩小,会有一些像素舍入,但舍入在两侧或多或少地应用,这有点好。这适用于 Firefox。

编辑3:

唉,我已经尝试稍微减少中间边距,这使得问题在 Chrome 中消失了,但是其中一个 div 元素在 IE10 中仍然下降。

div#mainContent { padding: 15px 0; text-align: center; }

div#contentLeft { display: inline-block; vertical-align: top; height: 900px; margin: 0 0 0 0/* 20px increased from 15px */; border: 1px solid black; padding: 15px; width: 268px; background: #ccc; overflow: hidden; }

div#contentCenter { display: inline-block; vertical-align: top; height: 900px; margin: 0 0 0 10px/* reduced from 15px */; border: 1px solid transparent; padding: 15px; width: 268px; background: #E00; overflow: hidden; }

div#contentRight { display: inline-block; vertical-align: top; height: 900px; margin: 0 0/* 20px increased from 15px */ 0 10px/* reduced from 15px */; border: 1px solid black; padding: 15px; width: 268px; background: #ccc; overflow: hidden; }

div#mainContent section { text-align: justify; }

编辑4:

我想出了一个适用于 Firefox、Chrome 和 IE10 的解决方案。基本上,我围绕三列创建了三个 div 包装器,并再次进行了测量,以确保所有测量都是对称的。这是源代码:

HTML 文件:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Pinco</title>
<link href="css/style.css" rel="stylesheet" type="text/css">
</head>
<body>
  <div id="wrapper">
    <header>
      <div class="logo">
        <a href="http://pinco.com">
          <img class="logo" src="images/PincoLogo5.png" alt="Pinco" />
        </a>
      </div>
      <div class="titolo">
        <h1>Benvenuti!</h1>
        <h2>Siete arrivati al sito pinco.</h2>
      </div>
      <nav>
        <ul class="menu"> 
          <li><a href="#">Menù Qui</a></li>
          <li><a href="#">Menù Quo</a></li>
          <li><a href="#">Menù Qua</a></li>
        </ul>
      </nav>
    </header>
    <div id="mainContent">
      <div id="wrapperLeft">
        <div id="contentLeft">
          <section>
            <article>
              <p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque tempor turpis est, nec varius est pharetra scelerisque. Sed eu pellentesque purus, at cursus nisi. In bibendum tristique nunc eu mattis. Nulla pretium tincidunt ipsum, non imperdiet metus tincidunt ac. In et lobortis elit, nec lobortis purus. Cras ac viverra risus. Proin dapibus tortor justo, a vulputate ipsum lacinia sed. In hac habitasse platea dictumst. Phasellus sit amet malesuada velit. Fusce diam neque, cursus id dui ac, blandit vehicula tortor.
Phasellus interdum ipsum eu leo condimentum, in dignissim erat tincidunt. Ut fermentum consectetur tellus, dignissim volutpat orci suscipit ac. Praesent scelerisque urna metus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Duis pulvinar, sem a sodales eleifend, odio elit blandit risus, a dapibus ligula orci non augue. Nullam vitae cursus tortor, eget malesuada lectus. Nulla facilisi. Cras pharetra nisi sit amet orci dignissim, a eleifend odio hendrerit.
              </p>
            </article>
          </section>
        </div>
      </div>
      <div id="wrapperCenter">
        <div id="contentCenter">
          <section>
            <article>
              <p>
Maecenas vitae purus at orci euismod pretium. Nam gravida gravida bibendum. Donec nec dolor vel magna consequat laoreet in a urna. Phasellus cursus ultrices lorem ut sagittis. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vivamus purus felis, ornare quis ante vel, commodo scelerisque tortor. Integer vel facilisis mauris.
              </p>
              <img src="images/auto1.jpg" alt="Auto 1" width="268" height="268" />
              <p>
In urna purus, fringilla a urna a, ultrices convallis orci. Duis mattis sit amet leo sed luctus. Donec nec sem non nunc mattis semper quis vitae enim. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
              </p>
            </article>
          </section>
        </div>
      </div>
      <div id="wrapperRight">
        <div id="contentRight">
          <section>
            <article>
              <img src="images/auto2.jpg" alt="Auto 2" width="268" height="268" style="margin-top: 0" />
              <img src="images/auto3.jpg" alt="Auto 3" width="268" height="268" style="margin-top: 0" />
              <p>
Cras eu quam lobortis, sodales felis ultricies, rhoncus neque. Aenean nisi eros, blandit ac lacus sit amet, vulputate sodales mi. Nunc eget purus ultricies, aliquam quam sit amet, porttitor velit. In imperdiet justo in quam tristique, eget semper nisi pellentesque. Cras fringilla eros enim, in euismod nisl imperdiet ac.
Fusce tempor justo vitae faucibus luctus.
              </p>
            </article>
          </section>
        </div>
      </div>
    </div>
    <footer>
      <img class="footerLogo" src="images/auto4.jpg" alt="Auto 4" width="80" height="80" />
      <p class="footerText">
Copyright &copy; Pinco Inc.
<br />Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<br />Fusce ornare turpis orci, nec egestas leo feugiat ac.
<br />Morbi eget sem facilisis, laoreet erat ut, tristique odio. Proin sollicitudin quis nisi id consequat.
      </p>
    </footer>
  </div>
</body>
</html>

CSS 文件:

/* CSS Document */

*, body, article, secton, p { margin: 0; border: 0; padding: 0; }

body { background: #8B0000; /* darkred */; }

body { margin: 0; border: 0; padding: 0; }

div#wrapper { margin: 0 auto; width: 960px; height: 100%; background: #FFC0CB /* pink */; }

header { position: relative; background: #005b97; height: 140px; }

header div.logo { float: left; width: 360px; height: 140px; }
header div.logo img.logo { width: 360px; height: 140px;  }

header div.titolo { float: left; padding: 12px 0 0 35px; color: black; }
header div.titolo h1 { font-size: 36px; font-weight: bold; }
header div.titolo h2 { font-size: 24px; font-style: italic; font-weight: bold; color: white;}

header nav { position: absolute; right: 0; bottom: 0; }

header ul.menu { background: black; }
header ul.menu li { display: inline-block; padding: 3px 15px; font-weight: bold; }

div#mainContent { float: left; padding: 15px 0; height: 900px; }

#wrapperLeft { float: left; margin-left: 15px; width: 305px; }

#wrapperCenter { float: left; margin: 0 15px 0 15px; width: 290px; }

#wrapperRight { float: left; margin-right: 15px; width: 305px; }

div#contentLeft { border: 1px solid black; padding: 15px; width: 273px; height: 868px; background: #ccc; overflow: hidden; }

div#contentCenter { border: 1px solid transparent; padding: 15px; width: 258px; height: 868px; background: #E00; overflow: hidden; }

div#contentRight { border: 1px solid black; padding: 15px; width: 273px; height: 868px; background: #ccc; overflow: hidden; }

div#mainContent section { text-align: justify; }

div#mainContent img { margin: 12px 0; }

footer { clear: both; padding: 12px; background: #306; color: white; height: 80px; font-size: 12px; font-style: italic; font-weight: bold; overflow: hidden; }

footer img.footerLogo { float: right; }

a { color: white; text-decoration: none; }
4

1 回答 1

0

这可能是由亚像素舍入引起的。

当您缩放时,您的浏览器可能会使用像素的分数来计算像素值。由于向上或向下舍入这些值,像素完美的布局可能会中断。(不同的浏览器以不同的方式处理这个问题。)

我很幸运将您的像素值转换为百分比值。
以下是对我有用的价值观:

div#contentLeft,
div#contentCenter,
div#contentRight {
     margin-left: 1.1%;
     padding: 1.56%;
     width: 28.3%;
}

编辑:

这是另一种方法,它基本上将三个框居中,div#mainContent而不是用边距将它们间隔得如此紧密。它允许他们在缩放时有更多的空间来弯曲。

我删除了margin-leftfromdiv#contentLeft并添加了以下 CSS 以使三个框居中:

div#wrapper {
  overflow:hidden;         /* ADDED THIS TO AVOID HORIZONTAL SCROLL */
}

div#mainContent {
     position: relative;   /* ADDED THIS */
     left: 50%;            /* ADDED THIS */
     float: left;
     padding: 30px 0px;
     text-align: justify;
}

div#contentLeft {
     position: relative;   /* ADDED THIS */
     left: -50%;           /* ADDED THIS */
     background: none repeat scroll 0% 0% #CCCCCC;
     border: 1px solid black;
     float: left;
     height: 900px;
     padding: 15px;
     width: 272px;
     /* margin-left:12px;     REMOVED THIS */
}

div#contentCenter {
     position: relative;   /* ADDED THIS */
     left: -50%;           /* ADDED THIS */
     background: none repeat scroll 0% 0% #EE0000;
     border: 1px solid transparent;
     float: left;
     height: 900px;
     margin-left: 12px;
     padding: 15px;
     width: 272px;
}

div#contentRight {
     position: relative;   /* ADDED THIS */
     left: -50%;           /* ADDED THIS */
     background: none repeat scroll 0% 0% #CCCCCC;
     border: 1px solid black;
     float: left;
     height: 900px;
     margin-left: 12px;
     padding: 15px;
     width: 272px;
}

即使 Firefox 一直放大,这些框仍然浮动在一行上。

于 2013-10-18T00:21:25.437 回答