【问题标题】:Weird table pixel bug in ChromeChrome中奇怪的表格像素错误
【发布时间】:2017-12-02 11:44:20
【问题描述】:

我在 Google Chrome(不是 Firefox)中遇到奇怪的像素差异。请参见上图或运行下面的代码 sn-p。

在红色边框可以看到表格。橙色:td 中的 div。灰色:td。我希望两者具有相同的高度。他们为什么不呢?左边的大半个像素。如果你用 Chrome 缩放它,差异就会解决。这是 Chrome 中的某种错误吗?我是否必须使用 px 作为度量值?我不想,因为我在这里渲染一个要打印的页面。

我努力删除了所有不必要的部分。

  * {
    margin:0;
    padding:0;
  }
  #wrapper1 {
    padding-top: 5mm;
    background:lightblue;
   }
   #wrapper2 {
    display: flex;
    flex-direction: column;
    background:lightgrey;
  }
   table {
    border-collapse: collapse;
    border: 1px solid red;
    margin-top:7mm;
   } 
   td, .box {
    width:12mm;
    height:12mm;
   }
   td {
    background:grey;
   }
   .box {
    background:orange;
   }
<div id="wrapper1">
   <div id="wrapper2">
      <table>
            <tr>
               <td>
                  <div  class="box"></div>
               </td>
               <td></td>
            </tr>
      </table>
   </div>
</div>

【问题讨论】:

标签: html css


【解决方案1】:

mm 单位不会转换为整数像素。例如,12mm45.354330709px。不幸的是,自 no CSS spec is provided 以来,浏览器处理舍入的方式有所不同。

例如,我已将您的 mm 单位转换为使用 px。我将其放大/缩小了 10 倍,并且不再看到边框放置位置的差异。

* {
  margin:0;
  padding:0;
}
#wrapper1 {
  padding-top: 5mm;
  background:lightblue;
}
#wrapper2 {
  display: flex;
  flex-direction: column;
  background:lightgrey;
}
table {
  border-collapse: collapse;
  border: 1px solid red;
  margin-top:70px;
} 
td, .box {
  width:120px;
  height:120px;
}
td {
  background:grey;
}
.box {
  background:orange;
}
<div id="wrapper1">
   <div id="wrapper2">
      <table>
            <tr>
               <td>
                  <div  class="box"></div>
               </td>
               <td></td>
            </tr>
      </table>
   </div>
</div>

【讨论】:

  • 沿着同一行,看the W3C's statement on using mm for sizing on the screen(提示:不要)。
  • 那么 Chrome 通常很难用小数来渲染像素?我应该只使用整数像素吗?无论如何,我无法重现像素的初始问题,所以这很可能是正确的答案。谢谢
  • @Blauhirn 我在自己的一些具有响应式设计的工作中遇到了同样的问题。我尝试使用 JavaScript 来排列所有内容,但到目前为止还没有找到任何解决方法。老实说,我只是学会了忍受它。如果某些东西看起来真的很令人反感,我会调整我的设计以在没有部分像素的情况下工作。
猜你喜欢
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-03
  • 1970-01-01
  • 2017-06-16
  • 2016-01-27
相关资源
最近更新 更多