在UnoCSS中定义1PX边框语法规则

在项目中使用UnoCSS时,实现移动端1px精细边框适配

适配规则定义:

  • 1bdr 四边框
  • 1bdr-t 上边框
  • 1bdr-b 下边框
  • 1bdr-l 左边框
  • 1bdr-r 右边框

要实现上面的规则,可以unocss中自定义规则 unocss 规则

uno.config.ts 中定义 rules

js 复制代码
import {
  type Preset,
  defineConfig,
  toEscapedSelector,
} from 'unocss'

export default defineConfig({
    ...
    rules: [
     [
      /^1bdr(-[t|b|r|l])?$/,
      ([, poi], { rawSelector }) => {
        const color = 'rgba(0, 0, 0, 0.15)'
        const positions = ['-top', '-bottom', '-right', '-left']
        const pos = positions.find((v) => v.startsWith(poi))
        const bdr = `border${pos}: 1px solid ${color};`
        // console.log(poi, bdr)

        const selector = toEscapedSelector(rawSelector)
        return `
          ${selector} {
            position: relative;
          }
          ${selector}::after {
            content: " ";
            position: absolute;
            width: 200%;
            height: 200%;
            top: 0;
            left: 0;
            ${bdr}
            transform: scale(0.5);
            transform-origin: 0 0;
            pointer-events: none;
            border-radius: inherit;
          }
        `
      },
    ],
   ]
   ...
})

使用

html 复制代码
// 可适配 border-radius
<div class="1bdr rounded-2 p-2 m-2"></div>

<div class="1bdr-b p-2 m-2"></div>
相关推荐
huohaiyu11 分钟前
从URL到页面的完整解析流程
前端·网络·chrome·url
阿星AI工作室2 小时前
一个简单Demo彻底理解前后端怎么连的丨Figma + Supabase + Vercel
前端·人工智能
aircrushin2 小时前
一拍即传的平替,完全免费的实时照片墙!
前端
鹏北海4 小时前
JSBridge 原理详解
前端
孟健4 小时前
我的网站被黑了:一天灌入 227 万条垃圾数据,AI 写的代码差点让我社死
前端
anOnion5 小时前
构建无障碍组件之Checkbox pattern
前端·html·交互设计
IT枫斗者6 小时前
IntelliJ IDEA 2025.3史诗级更新:统一发行版+Spring Boot 4支持,这更新太香了!
java·开发语言·前端·javascript·spring boot·后端·intellij-idea
N***p3656 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
享誉霸王8 小时前
15、告别混乱!Vue3复杂项目的规范搭建与基础库封装实战
前端·javascript·vue.js·前端框架·json·firefox·html5
a1117768 小时前
飞机躲避炸弹 网页游戏
前端·开源·html·threejs