7種方法實現移動端Retina屏幕1px邊框效果
在Reina(視網膜)屏幕的手機上,使用CSS設置的1px的邊框實際會比視覺稿粗很多。在之前的項目中,UI告訴我說我們移動項目中的邊框全部都變粗了,UI把他的設計稿跟我的屏幕截圖跟我看,居然真的不一樣。沒有辦法,只有在后面的版本中去修改了,但是要改的話,需要知道是為什么。所以查了很多資料,終于搞懂了這個問題,并且總結了幾種方法。
造成邊框變粗的原因
其實這個原因很簡單,因為css中的1px并不等于移動設備的1px,這些由于不同的手機有不同的像素密度。在window對象中有一個devicePixelRatio屬性,他可以反應css中的像素與設備的像素比。
devicePixelRatio的官方的定義為:設備物理像素和設備獨立像素的比例,也就是 devicePixelRatio = 物理像素 / 獨立像素。
解決邊框變粗的6種辦法
1、0.5px邊框
在2014年的 WWDC,“設計響應的Web體驗” 一講中,Ted O’Connor 講到關于“retina
hairlines”(retina 極細的線):在retina屏上僅僅顯示1物理像素的邊框,開發者應該如何處理呢。
他們曾介紹到 iOS 8 和 OS X Yosemite 即將支持 0.5px 的邊框:

額的神吶!so easy! 果真如此嗎?
這樣還不夠(WWDC幻燈片通常是“唬人”的),但是相差不多。
問題是 retina 屏的瀏覽器可能不認識0.5px的邊框,將會把它解釋成0px,沒有邊框。包括 iOS 7 和之前版本,OS X Mavericks 及以前版本,還有 Android 設備。
解決方案:
解決方案是通過 JavaScript 檢測瀏覽器能否處理0.5px的邊框,如果可以,給html標簽元素添加個class。
if (window.devicePixelRatio && devicePixelRatio >= 2) {
var testElem = document.createElement('div');
testElem.style.border = '.5px solid transparent';
document.body.appendChild(testElem);
}
if (testElem.offsetHeight == 1) {
document.querySelector('html').classList.add('hairlines');
}
document.body.removeChild(testElem);
}
// 腳本應該放在內,如果在里面運行,需要包裝 $(document).ready(function() {})
然后,極細的邊框樣式就容易了:
div {
border: 1px solid #bbb;
}
.hairlines div {
border-width: 0.5px;
}
優點:
- 簡單,不需要過多代碼。
缺點:
- 無法兼容安卓設備、 iOS 8 以下設備。
2、使用border-image實現
準備一張符合你要求的border-image:

樣式設置:
.border-bottom-1px {
border-width: 0 0 1px 0;
-webkit-border-image: url(linenew.png) 0 0 2 0 stretch;
border-image: url(linenew.png) 0 0 2 0 stretch;
}
上文是把border設置在邊框的底部,所以使用的圖片是2px高,上部的1px顏色為透明,下部的1px使用視覺規定的border的顏色。如果邊框底部和頂部同時需要border,可以使用下面的border-image:

樣式設置:
.border-image-1px {
border-width: 1px 0;
-webkit-border-image: url(linenew.png) 2 0 stretch;
border-image: url(linenew.png) 2 0 stretch;
}
到目前為止,我們已經能在iphone上展現1px border的效果了。但是我們發現這樣的方法在非視網膜屏上會出現border顯示不出來的現象,于是使用Media Query做了一些兼容,樣式設置如下:
.border-image-1px {
border-bottom: 1px solid #666;
}
@media only screen and (-webkit-min-device-pixel-ratio: 2) {
.border-image-1px {
border-bottom: none;
border-width: 0 0 1px 0;
-webkit-border-image: url(../img/linenew.png) 0 0 2 0 stretch;
border-image: url(../img/linenew.png) 0 0 2 0 stretch;
}
}
優點:
- 可以設置單條,多條邊框
- 沒有性能瓶頸的問題
缺點:
- 修改顏色麻煩, 需要替換圖片
- 圓角需要特殊處理,并且邊緣會模糊
3、使用background-image實現
background-image 跟border-image的方法一樣,你要先準備一張符合你要求的圖片。然后將邊框模擬在背景上。
樣式設置:
.background-image-1px {
background: url(../img/line.png) repeat-x left bottom;
-webkit-background-size: 100% 1px;
background-size: 100% 1px;
}
優點:
- 可以設置單條,多條邊框
- 沒有性能瓶頸的問題
缺點:
- 修改顏色麻煩, 需要替換圖片
- 圓角需要特殊處理,并且邊緣會模糊
4、多背景漸變實現
與background-image方案類似,只是將圖片替換為css3漸變。設置1px的漸變背景,50%有顏色,50%透明。
樣式設置:
.background-gradient-1px {
background:
linear-gradient(#000, #000 100%, transparent 100%) left / 1px 100% no-repeat,
linear-gradient(#000, #000 100%, transparent 100%) right / 1px 100% no-repeat,
linear-gradient(#000,#000 100%, transparent 100%) top / 100% 1px no-repeat,
linear-gradient(#000,#000 100%, transparent 100%) bottom / 100% 1px no-repeat
}
/* 或者 */
.background-gradient-1px{
background:
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) left / 1px 100% no-repeat,
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) right / 1px 100% no-repeat,
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) top / 100% 1px no-repeat,
-webkit-gradient(linear, left top, right bottom, color-stop(0, transparent), color-stop(0, #000), to(#000)) bottom / 100% 1px no-repeat
}
優點:
- 可以實現單條、多條邊框
- 邊框的顏色隨意設置
缺點:
- 代碼量不少
- 圓角沒法實現
- 多背景圖片有兼容性問題
5、使用box-shadow模擬邊框
利用css 對陰影處理的方式實現0.5px的效果
樣式設置:
.box-shadow-1px {
box-shadow: inset 0px -1px 1px -1px #c8c7cc;
}
優點:
- 代碼量少
- 可以滿足所有場景
缺點:
- 邊框有陰影,顏色變淺
6、viewport + rem 實現
同時通過設置對應viewport的rem基準值,這種方式就可以像以前一樣輕松愉快的寫1px了。
在devicePixelRatio = 2 時,輸出viewport:
<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no">
在devicePixelRatio = 3 時,輸出viewport:
<meta name="viewport" content="initial-scale=0.3333333333333333, maximum-scale=0.3333333333333333, minimum-scale=0.3333333333333333, user-scalable=no">
這種兼容方案相對比較完美,適合新的項目,老的項目修改成本過大。
對于這種方案,可以看看《使用Flexible實現手淘H5頁面的終端適配》
優點:
- 所有場景都能滿足
- 一套代碼,可以兼容基本所有布局
缺點:
- 老項目修改代價過大,只適用于新項目
7、偽類 + transform 實現
對于老項目,有沒有什么辦法能兼容1px的尷尬問題了,個人認為偽類+transform是比較完美的方法了。
原理是把原先元素的 border 去掉,然后利用 :before 或者 :after 重做 border ,并 transform 的 scale 縮小一半,原先的元素相對定位,新做的 border 絕對定位。
單條border樣式設置:
.scale-1px{
position: relative;
border:none;
}
.scale-1px:after{
content: '';
position: absolute;
bottom: 0;
background: #000;
width: 100%;
height: 1px;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
}
四條boder樣式設置:
.scale-1px{
position: relative;
margin-bottom: 20px;
border:none;
}
.scale-1px:after{
content: '';
position: absolute;
top: 0;
left: 0;
border: 1px solid #000;
-webkit-box-sizing: border-box;
box-sizing: border-box;
width: 200%;
height: 200%;
-webkit-transform: scale(0.5);
transform: scale(0.5);
-webkit-transform-origin: left top;
transform-origin: left top;
}
最好在使用前也判斷一下,結合 JS 代碼,判斷是否 Retina 屏:
if(window.devicePixelRatio && devicePixelRatio >= 2){
document.querySelector('ul').className = 'scale-1px';
}
優點:
- 所有場景都能滿足
- 支持圓角(偽類和本體類都需要加border-radius)
缺點:
- 對于已經使用偽類的元素(例如clearfix),可能需要多層嵌套
參考:
作者:山邊小溪
主站:yyyweb.com 記住啦:)
歡迎任何形式的轉載,但請務必注明出處。


浙公網安備 33010602011771號