解决移动端1px 边框优化的8个方法

前言

您是否注意到 1px 边框在移动设备上有时会显得比预期的要粗?这种不一致源于移动屏幕的像素密度不同。

在 Web 开发中,我们使用 CSS 来设置页面样式。但是,CSS 中的 1px 并不总是转换为设备上的物理 1px。这种差异就是我们的“1px 边框问题”产生的原因。

罪魁祸首:像素密度

每个设备都拥有特定的像素密度,由 devicePixelRatio 测量,它告诉我们物理像素与设备独立像素之间的比率。

devicePixelRatio = 物理像素 / 独立像素

今天我就来跟你分享8 个久经考验的解决方案 。探索解决方案,我们要重点关注像素比大于或等于 2 的情况。

1.  0.5px 边框:一个简单的解决方案

此方法涉及在设备像素比为 2 或更高时有条件地应用 0.5px 边框。

// Check if devicePixelRatio exists and is greater than or equal to 2
if(window.devicePixelRatio && devicePixelRatio>=2){
  // Create a temporary div element for testing
  var testElem = document.createElement('div');
  // Apply a 0.5px transparent border to the test element
  testElem.style.border = '.5px solid transparent';
  // Append the test element to the body
  document.body.appendChild(testElem);
  // Check if the rendered height is 1px (meaning 0.5px border works)
  if(testElem.offsetHeight == 1){
    // If yes, add the 'hairlines' class to the HTML element
    document.querySelector('html').classList.add('hairlines');
  }
  // Remove the test element
  document.body.removeChild(testElem);
}
//  Place the above script inline. If it's inside a function, 
// wrap it in $(document).ready(function(){}) to ensure it runs after the DOM is ready.

// Default border style
div{
  border: 1px solid #bbb;
}
// Apply 0.5px border when 'hairlines' class is present
.hairlines div {
  border-width: 0.5px;
}

2.  边框图像:完美的边框

使用专门制作的边框图像是一种有效的方法。以下是创建底部边框的方法:

.border-bottom-1px {
  // Set other border widths to 0
  border-width: 0 0 1px 0;
  // Apply the border-image – ‘linenew.png’ 
  // (assuming you have an image for this)
  border-image: url(linenew.png) 0 0 2 0 stretch;
  // For webkit browsers
  -webkit-border-image: url(linenew.png) 0 0 2 0 stretch;
}

解释:

  • 我们只在底部设置边框(border-width:0 0 1px 0)。

  • 使用的图像(“linenew.png”)假定为 2px 高。

  • 图像顶部 1px 是透明的,底部 1px 包含实际边框颜色。

3.  Background-Image:背景技巧

与 border-image 类似,此方法利用预先准备的图像作为边框。

.backround-image-1px{
  // Set the background image, repeating it along the x-axis and positioning it at the left bottom
  background: url(../img/line.png) repeat-x left bottom;
   // Set the background size for Webkit browsers
  -webkit-background-size: 100% 1px;
  // Set the background size (1px height for the border effect)
  background-size: 100% 1px;
}

注意事项:

  • 更改颜色需要替换图像。

  • 圆角可能会显得模糊,需要额外的样式。

4.  多背景渐变:边框的错觉

我们可以使用渐变背景来模仿边框的外观。渐变的一半显示所需的颜色,而另一半保持透明。

.background-gradient-1px{
    // Create a multi-background with linear gradients for each side
    background:
      line-gradient(180deg, black, black 50%, transparent 50%) top left / 100% 1px no-repeat,
      line-gradient(90deg, black, black 50%, transparent 50%) top right / 1px 100% no-repeat,
      line-gradient(0, black, black 50%, transparent 50%) bottom right /  100% 1px no-repeat,
      line-gradient(-90deg, black, black 50%, transparent 50%) bottom left / 1px 100% no-repeat;
  }

/* Alternatively, use an older syntax for Webkit browsers*/
.background-gradient-1px{
      // Apply a linear gradient from top to bottom
      background: -webkit-gradient(linear, left top, left bottom, 
                                   color-step(.5, transparent), // Transparent at 50%
                                   color-step(.5, #c8c7cc), // Color starts at 50%
                                   to(#c8c7cc)) // End color
                                   left bottom repeat-x; 
      // Set the background size
      background-size: 100% 1px; 
}

5.  Box-Shadow:跳出框框

让我们利用 CSS 阴影来创建令人信服的边框效果。

.box-shadow-1px {
  // Apply an inset box shadow – the negative spread simulates a thin border
  box-shadow: inset 0px -1px 1px -1px #c8c7cc; 
}

6.  视口 + Rem:动态二重奏 

调整视口的 rem 基值有助于在不同设备上实现一致的 1px 边框。请记住,使用此技术修改旧项目可能需要进行重大调整。

优点:适用于各种布局的适应性解决方案。

缺点:对于遗留项目来说可能具有挑战性。

// For a device pixel ratio of 1, set the viewport as follows:
<meta name="viewport" content="initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">

// For a device pixel ratio of 2
<meta name="viewport" content="initial-scale=0.5, maximum-scale=0.5, minimum-scale=0.5, user-scalable=no">
// For a device pixel ratio of 3
<meta name="viewport" content="initial-scale=0.333333, maximum-scale=0.333333, minimum-scale=0.333333, user-scalable=no">

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8" />
  <meta name="viewport"
        content="width=device-width,initial-scale=1,user-scalable=no"
  />
  <title>rem+viewport</title> 
  <style type="text/css">
    * {
      margin: 0;
      padding: 0;
    }
    #box {
      width: 8rem;
      height: 8rem;
      border: 1px solid #000;
    }
</style>
</head>
<body>
  <div id="box"></div>

  <script type="text/javascript">
    // Get the device pixel ratio
    var dpr = window.devicePixelRatio; // Example: 2 on a Retina display 
    console.log(dpr, 'dpr+++');

    // Calculate the inverse scale 
    var scale = 1 / dpr; 
    // Get the initial viewport width – this might be inaccurate due to the dpr
    var width = document.documentElement.clientWidth; // Example: 375 on an iPhone X
    // Adjust the viewport meta tag to counteract the device pixel ratio
    var metaNode = document.querySelector('meta[name="viewport"]');
    metaNode.setAttribute('content', 'width=device-width,initial-scale=' + scale + ',user-scalable=no');
    // Recalculate the width after viewport adjustment
    var width = document.documentElement.clientWidth; // Now, it should be closer to 750
    // Dynamically set the base font size using rem units
    var styleN = document.createElement('style');
    styleN.innerHTML = 'html{font-size: ' + width / 16 + 'px !important;}'; 
    document.head.appendChild(styleN);
</script>
</body>
</html>

7.  伪元素 + 变换:传统项目英雄 

这种方法对现有项目非常方便。我们删除原始边框,并利用伪元素制作 1px 边框,将其缩小以获得像素完美的外观

.scale-1px {
  position: relative;
  border: none; // Remove any default borders
}

.scale-1px:after {
  content: '';
  position: absolute;
  bottom: 0;
  background: #000; // Set the desired border color
  width: 100%;
  height: 1px; 
  transform: scale(0.5); // Scale down to 0.5 to achieve a thinner border
  transform-origin: 0 0; 
}
.scale-1px-top {
  border: none;
  position: relative;
}
.scale-1px-top:before {
  content: '';
  position: absolute;
  display: block;
  top: 0;
  left: 0;
  width: 200%; // Stretch to cover potential scaling issues
  height: 1px;
  border-top: 1px solid #E7E7E7;
  -webkit-transform: scale(0.5, 0.5);
  transform: scale(0.5, 0.5);
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0;
}
.scale-1px-bottom {
  border: none;
  position: relative;
}
.scale-1px-bottom:before {
  content: '';
  position: absolute;
  display: block;
  bottom: -1px; // Adjust position to avoid overlapping content
  left: 0;
  width: 200%; 
  height: 1px;
  border-bottom: 1px solid #ccc; 
  -webkit-transform: scale(0.5, 0.5);
  transform: scale(0.5, 0.5); 
  -webkit-transform-origin: 0 0;
  transform-origin: 0 0; 
}
.borderRadius-1px { 
  border-radius: .16rem; 
  border: none;
  position: relative;
}
.borderRadius-1px:after { 
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  border: 1px solid #d1d1d1;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  width: 200%; // Ensure the pseudo-element covers the entire element
  height: 200%;
  -webkit-transform: scale(0.5);
  transform: scale(0.5);
  -webkit-transform-origin: left top; 
  transform-origin: left top;
  border-radius: .16rem;
}

8.  SVG:绘制线条

我们也可以使用 SVG 直接绘制 1px 线条。

<svg width="100%" height="1" style="position: absolute; bottom: 0; left: 0;">
  <line x1="0" y1="0" x2="1000" y2="0" style="stroke:#E5E5E5; stroke-width:1" /> 
</svg>

关于优联前端

        武汉优联前端科技有限公司由一批从事前端10余年的专业人才创办,是一家致力于H5前端技术研究的科技创新型公司,为合作伙伴提供专业高效的前端解决方案,合作伙伴遍布中国及东南亚地区,行业涵盖广告,教育, 医疗,餐饮等。有效的解决了合作伙伴的前端技术难题,节约了成本,实现合作共赢。可进行Web前端,微信小程序、小游戏,2D/3D游戏,动画交互与UI广告设计等各种技术研发。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/873665.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

如何在网上找客户资源

在网上寻找客户资源可以通过多种渠道和方法来实现&#xff0c;这些方法结合不同的工具和平台&#xff0c;可以帮助你快速定位目标客户。以下是一些常见且有效的途径&#xff1a; 1. 利用搜索引擎 使用搜索引擎&#xff08;如百度、Google&#xff09;通过关键词搜索目标客户或…

JAVA一键开启缘分之旅红娘相亲交友系统小程序源码

一键开启缘分之旅 —— 红娘相亲交友系统 &#x1f496; 初遇心动&#xff0c;一键启程 在这个快节奏的时代&#xff0c;找到那个对的人似乎成了一种奢侈。但别担心&#xff0c;有了“红娘相亲交友系统”&#xff0c;你的缘分之旅只需一键即可开启&#xff01;无需复杂的注册流…

张雪峰:物联网行业迎高光时刻!如何选择?我们诚聘销售工程师!

作为一间10多年的物联网公司&#xff0c;各位求职人士可以看看我们其中一个招聘要求&#xff0c;和自己需求结合分析分析&#xff0c;希望对你们有所帮助。 【公司实力底蕴】 盈电智控物联网科技&#xff08;广东&#xff09;有限公司&#xff0c;2024年7月成立&#xff0c;是…

git pull之后发现项目错误,如何回到之前的版本方法

目录 首先我们打开小程序的cmd的黑窗口&#xff0c;git reflog查看之前的版本 之后再git reset --hard main{1} 我这个就已经返回了之前的6daaa2e的版本了 首先我们打开小程序的cmd的黑窗口&#xff0c;git reflog查看之前的版本 之后再git reset --hard main{1} 我这个就已…

深度学习的发展历程

深度学习的起源 在机器学习中&#xff0c;我们经常使用两种方式来表示特征&#xff1a;局部表示&#xff08;Local Representation&#xff09;和分布式表示&#xff08;Distributed Representation&#xff09;。以颜色表示为例&#xff0c;见下图&#xff1a; 要学习到一种好…

自动驾驶ADAS算法--使用MATLBA和UE4生成测试视频

原文参考&#xff1a;金书世界 环境搭建参考&#xff1a;用MATLAB2020b和虚拟引擎&#xff08;Unreal Engine&#xff09;联合仿真输出AVM全景测试视频----Matlab环境搭建 matlab参考&#xff1a; https://ww2.mathworks.cn/help/driving/ug/simulate-a-simple-driving-sce…

分库分表核心理念

文章目录 分库&#xff0c;分表&#xff0c;分库分表什么时候分库&#xff1f;什么时候分表&#xff1f;什么时候既分库又分表&#xff1f;横向拆分 & 纵向拆分 分表算法Range 范围Hash 取模一致性 Hash斐波那契散列 严格雪崩标准&#xff08;SAC&#xff09;订单分库分表实…

导入word模板的数据到DB,偏自学,可自改套用

GetMapping("/importTestPeople")public void importTestPeople(RequestParam("file") MultipartFile multipartFile) throws IOException {InputStream inputStream null;File file null;try {// 创建临时文件file File.createTempFile("temp&quo…

从0开始深入理解并发、线程与等待通知机制

1、 从0开始深入理解并发、线程与等待通知机制 从上面两大互联网公司的招聘需求可以看到&#xff0c;大厂的Java岗的并发编程能力属于标配。 而在非大厂的公司&#xff0c;并发编程能力也是面试的极大加分项&#xff0c;而工作时善用并发编程则可以极大提升程序员在公司的技术…

前向渲染路径

1、前向渲染路径处理光照的方式 前向渲染路径中会将光源分为以下3种处理方式&#xff1a; 逐像素处理&#xff08;需要高等质量处理的光&#xff09;逐顶点处理&#xff08;需要中等质量处理的光&#xff09;球谐函数&#xff08;SH&#xff09;处理&#xff08;需要低等质量…

phpmyadmin报错mysqli::real_connect(): (HY000/1045): Access denied for user ‘

问题分析 这是因为本身还安装了MySQL&#xff0c;导致发生冲突&#xff0c;只需要找到自己安装的进行关闭即可 方法 在任务管理器(快捷键&#xff1a;ctrlaltdelete)-服务中&#xff0c;找到对应的MySQL进行关闭

爬虫 可视化 管理:scrapyd、Gerapy、Scrapydweb、spider-admin-pro、crawllab、feaplat、XXL-JOB

1、scrapyd 大多数现有的平台都依赖于 Scrapyd&#xff0c;这将选择限制在 python 和 scrapy 之间。当然 scrapy 是一个很棒的网络抓取框架&#xff0c;但是它不能做所有的事情。 对于重度 scrapy 爬虫依赖的、又不想折腾的开发者&#xff0c;可以考虑 Scrapydweb&#xff1b;…

基于springboot+vue实现的在线商城系统

系统主要功能&#xff1a; &#xff08;1&#xff09;商品管理模块&#xff1a;实现了商品的基本信息录入、图片上传、状态管理等相关功能。 &#xff08;2&#xff09;商品分类模块&#xff1a;实现了分类的增删改查、分类层级管理、商品分类的关联等功能。 &#xff08;3&…

基于 SpringBoot 的私人健身与教练预约管理系统

专业团队&#xff0c;咨询送免费开题报告&#xff0c;大家可以来留言。 摘 要 随着信息技术和网络技术的飞速发展&#xff0c;人类已进入全新信息化时代&#xff0c;传统管理技术已无法高效&#xff0c;便捷地管理信息。为了迎合时代需求&#xff0c;优化管理效率&#xff0c;…

【机器学习】高斯网络的基本概念和应用领域以及在python中的实例

引言 高斯网络&#xff08;Gaussian Network&#xff09;通常指的是一个概率图模型&#xff0c;其中所有的随机变量&#xff08;或节点&#xff09;都遵循高斯分布 文章目录 引言一、高斯网络&#xff08;Gaussian Network&#xff09;1.1 高斯过程&#xff08;Gaussian Proces…

idea如何配置模板

配置生成代码指令模板 注&#xff1a;我们常用的有sout,main等指令 第一步打开设置面板 1)按如下操作 2&#xff09;或者CtrlAltS快捷键直接弹出 第二步找 Editor>LiveTemplates 第三步创建模板 步骤如下 1&#xff09;创建分组名字 2)分组名字 3&#xff09;创建自己的模板…

计算机网络与Internet应用

一、计算机网络 1.计算机网络的定义 网络定义&#xff1a;计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备&#xff0c;通过通信线路连接起来&#xff0c;在网络操作系统&#xff0c;网络管理软件及网络通信协议的管理和协调下&#xff0c;实现资源共享…

国产芯片LT8619C:HDMI转RGB/LVDS转换器,4k x 2k 30Hz高分辨率

以下为LT8619C转换芯片的简介&#xff0c;如有不足或错误&#xff0c;请指正&#xff1a; LT8619C是一款高性能HDMI/双模DP接收器芯片&#xff0c;符合HDMI 1.4规范。支持TTL或LVDS信号输出&#xff0c;TTL输出时&#xff0c;可支持输出RGB、BT656、BT1120信号&#xff0c;输出…

深度置信网络(深度信念网络)DBN分类模型(二分类多分类)-MATLAB代码实现

一、深度置信网络DBN&#xff08;代码获取&#xff1a;底部公众号&#xff09; 深度置信网络&#xff08;Deep Belief Network&#xff0c;DBN&#xff09;是一种基于无监督学习的深度神经网络模型&#xff0c;它由多个受限玻尔兹曼机&#xff08;Restricted Boltzmann Machin…

SAP与湃睿PLM系统集成案例

一、项目背景 浙江某家用电机有限公司, 该公司的产品涵盖洗衣机、‌空调、‌冰箱及厨房用具等家电电机的制造&#xff0c;‌具备年产4600万台电机的生产能力&#xff0c;‌是中国最大的家电电机生产基地之一。 为确保工艺路线信息在设计与生产执行层面的无缝传递&#xff0…