![](/img/trans.png)
[英]Why does my website looks different on mozilla firefox and chrome?
[英]My website looks different in Firefox from Chrome
我一直在研究Chrome上的網頁,但發現它在Firefox中看起來並不一樣。 我簡化了代碼以顯示問題。 Firefox中似乎沒有顯示最后一個,而Chrome中卻可見。
這是我的html文件。
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
<div id=shipBoard align="right" style="overflow: hidden">
<div id="ship" style="float: left;">
<img src="carrier.jpg" style="width:100px;height:100px;">
</div>
<div id="ship" style="float: left;">
<img src="destroyer.jpg" style="width:100px;height:100px;">
</div>
<div id="ship" style="float: left;">
<img src="test.jpg" style="width:100px;height:100px;">
</div>
<div id="ship" style="float: left;">
<img src="battleship.jpg" style="width:100px;height:100px;">
</div>
<div id="ship" style="float: left;">
<img src="sub.jpg" style="width:100px;height:100px;">
</div>
</div>
</html>
這是css文件:
#ship {
position: relative;
width: 4cm;
height: 5cm;
margin-left: auto;
margin-right: auto;
background-color: white;
}
#shipBoard {
position: relative;
width: 20cm;
height: 5cm;
margin-left: auto;
margin-right: auto;
background-color: green;
}
我該怎么做才能使它們相同?
亞像素定位很棘手。 cm
單位未映射到整數像素(定義為1cm = 96px/2.54
)。
在Chrome中:
> getComputedStyle(document.querySelector('#shipBoard')).width
"755.891px"
> getComputedStyle(document.querySelector('#ship')).width
"151.172px"
在Firefox中:
> getComputedStyle(document.querySelector('#shipBoard')).width
"755.9px"
> getComputedStyle(document.querySelector('#ship')).width
"151.183px"
CSS布局中的值通常從雙精度轉換為浮點數並返回,或者舍入到小數點后幾位。 這種布局很脆弱,因為它取決於5 * width(ship)≤width(shipBoard)。 浮標旨在在必要時進行包裹。 CSS中的某些長度(特別是邊框)有時會捕捉到全部像素,而不管縮放級別如何。
使用Flexbox或grid代替浮動控件是個不錯的選擇,但是在CSS中將width屬性更改為151px
和755px
可以解決此問題。
另外,請勿在多個元素上使用相同的id=
,這是無效的。
你不能 通常,在HTML中,它可以在不同的瀏覽器上顯示不同。 Firefox在這方面不如Chrome先進。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.