CSS 背景居中次åƒ?素无法é‡?å?ˆ - 凡事预则立,ä¸?预则废

å?ˆè§?CSS BUG,昨天赶工写完一个页é?¢,专题页é?¢,å?ˆ需è¦?é‡?用,没花太多时间处ç?†细节上的东西,因为大部分模å?—都å?¯以直接替æ?¢;今天早上å?Œ事和我说:“Safari 下é?¢背景是ä¸?是有1pxå??移?â€?,å?¬完觉得挺奇怪,一般ä¸?应该存在这样的问题的,然å?Ž一看就有了这篇文章

首先é‡?现BUG(此Demo仅CSS3支æŒ?,Chrome/Safari最佳):



<div class="t_shell">
	<div class="t_wrap">
		<div class="t_panel">
			<div class="t_inner" title=".t_inner"></div>
		</div>
	</div>
</div>


.t_wrap{ background:#eee; margin:0 auto; width:401px;}
.t_panel{ background:-webkit-linear-gradient(top, #299a0b 0%,#299a0b 100%) no-repeat; background-position:50% 20px; padding:140px 0 0 0; height:140px;}
.t_inner{ background:-webkit-linear-gradient(top, #cf0404 43%,#ff3019 100%); margin:0 auto; height:120px; width:200px;}

点击“.t_wrap width +3pxâ€?按钮增加(div.t_wrap)宽度时候,能够很明显的看到绿色å?—(div.t_panel)有1px左å?³的æ?¥回摆动。

出现以上BUG的原因,国外“ACKOâ€?给出的原因如下:

  • The width of the viewport

  • The odd/even size of the box/element

  • Background image is bigger/smaller than the viewport

  • Background image is padded evenly/unevenly around the box (1px difference). (*)

(*) Note that there is a choice whether to pad more on the left or on the right. I chose the left. This means that the last 4 rows of test cases are inherently ambiguous: a browser that misaligns all of these in the same fashion is in fact being consistent, just in the opposite direction.

原文地å?€:



解决方案:

在我测试过程中,å?‘现基本上å?ªè¦?上é?¢给出原因å‰?é?¢两æ?¡ä¸?出现,å?³:宽度ä¸?跟éš?视图å?˜化,容器的宽度适中为å?¶数或者奇数;就å?¯以有效é?¿å…?

å?¦外,在查找资料的过程中,å?‘现å?¦外一个解决方案,ä¸?过我根æ?®我测试的好åƒ?ä¸?是很给力,当视图宽度å?˜化时,ä¾?然能é‡?现:


@media screen and (-webkit-min-device-pixel-ratio:0){
	.t_panel{ background-position:50.001% 20px;}
}

原文地å?€:

共0评论

å?‘表评论

最新评论å?Š回å¤?

    loading

最近å?‘表

    loading

网站分类

    loading

文章归档

    loading

å?‹情链接

Top Comments Footer