手機實現1畫素邊框顯示

煙海之藍發表於2017-07-10
問題需求:頁面設定邊框1畫素時,電腦端顯示是正常的,手機端會顯示比正常粗一點。

1、給需要顯示1畫素邊框的元素新增偽類元素,設定邊框為1px。注意:這個時候和直接在元素本身設定邊框是一樣的效果,達不到真正顯示1畫素的問題。

2、然後用media 媒體查詢,設定偽類元素的縮放比例。

  1. @media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) {
  2. .border-1px::after{
  3. -webkit-transform: scaleY(0.7);
  4. }
  5. }
  6. @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) {
  7. .border-1px::after{
  8. -webkit-transform: scaleY(0.5);
  9. }
  10. }
 3、完整示例

  1. html
  2. <div class="tab border-1px"></div>
  3. css
  4. .tab{
  5. display:flex;
  6. width:100%;
  7. height:40px;
  8. line-height:40px;
  9. position:relative;
  10. }
  11. .tab:after{
  12. display:block;
  13. position:absolute;
  14. width:100%;
  15. left:0;
  16. bottom:0;
  17. border-top:1px solid rgba(7,17,27,0.1);
  18. content:``;
  19. }
  20. base.css
  21. @media only screen and (-webkit-min-device-pixel-ratio: 1.5), only screen and (min-device-pixel-ratio: 1.5) {
  22. .border-1px::after{
  23. -webkit-transform: scaleY(0.7);
  24. }
  25. }
  26. @media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) {
  27. .border-1px::after{
  28. -webkit-transform: scaleY(0.5);
  29. }
  30. }


相關文章