在PC端用1px的边框线,看起来还好,但在手机端看起来就很难看了,而0.5px的分割线会有种精致的感觉。用普通写法border:solid 0.5px red;iPhone可以正常显示,android下几乎所有的浏览器都会把0.5识别为0,即无边框状态.

原理

原理就是给需要加边框的元素插入一个伪类,伪类采用绝对定位,然后对伪类添加1px边框,最后进行0.5倍缩放。transform的缩放和旋转默认都是按照元素的中心点来操作的
outline元素在缩放0.5之前尺寸就是红框元素,缩放后,位置到了红框中心,为了使之依然在左上角,缩放之前我们需进行left:-50%;top:-50%的位移

0.5px边框

<div class="first">
<div class="first-div">
HELLO WORLD
</div>
</div>
<style>
.first{
position: relative;
font-size: 16px;
}
.first .first-div:before{
content: "";
position: absolute;
top: -50%;
bottom: -50%;
left: -50%;
right: -50%;
width: 200%;
height: 200%;
-webkit-transform: scale(0.5);
transform: scale(0.5);
border: solid 1px red;
box-sizing:border-box;
}
</style>

副作用

当用伪类的绝对定位来实现了边框后,我们在first类和first-div类上的点击事件会失效,因为此时的伪类是绝对定位,而且长宽等于父类元素的长宽,是脱离了文档流覆盖在父类上的,伪类不是真实的DOM元素,没有js点击事件

  • 解决方案

再写一个绝对定位元素,覆盖在父元素上,层级优先级要高一点

<div class="first">
<div class="first-div">
HELLO WORLD
</div>
<div class="click-able" onclick="alert('click')"></div>
</div>
<style>
.first{
position: relative;
font-size: 16px;
}
.first .first-div:before{
content: "";
position: absolute;
top: -50%;
bottom: -50%;
left: -50%;
right: -50%;
width: 200%;
height: 200%;
-webkit-transform: scale(0.5);
transform: scale(0.5);
border: solid 1px red;
box-sizing:border-box;
}
.click-able{
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
z-index: 10;
}
</style>

0.5px圆角边框

<div class="round">
<div class="round-div">
HELLO WORLD
</div>
</div>
<style>
.round{
position: relative;
font-size: 16px;
}
.round .round-div:before{
content: "";
position: absolute;
top: -50%;
bottom: -50%;
left: -50%;
right: -50%;
width: 200%;
height: 200%;
-webkit-transform: scale(0.5);
transform: scale(0.5);
border: solid 1px red;
border-radius: 22px;
box-sizing:border-box;
}
</style>

0.5px左边线

<div class="left">
<div class="left-div">
HELLO WORLD
</div>
</div>
<style>
.left{
position: relative;
font-size: 16px;
}
.left .left-div:before{
content: " ";
position: absolute;
left: 0;
bottom: 0;
width: 1px;
height: 100%;
border-left: 1px solid red;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleX(0.5);
transform: scaleX(0.5);
}
</style>

0.5px右边线

<div class="right">
<div class="right-div">
HELLO WORLD
</div>
</div>
<style>
.right{
position: relative;
font-size: 16px;
display: inline-block;
}
.right .right-div:before{
content: " ";
position: absolute;
right: 0;
bottom: 0;
width: 1px;
height: 100%;
border-right: 1px solid red;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleX(0.5);
transform: scaleX(0.5);
}
</style>

0.5px底部线

<div class="bottom">
<div class="bottom-div">
HELLO WORLD
</div>
</div>
<style>
.bottom{
position: relative;
font-size: 16px;
}
.bottom .bottom-div:before{
content: " ";
position: absolute;
left: 0;
bottom: 0;
width: 100%;
height: 1px;
border-top: 1px solid red;
-webkit-transform-origin: 0 0;
transform-origin: 0 0;
-webkit-transform: scaleY(0.5);
transform: scaleY(0.5);
}
</style>

本文转载于:猿2048→https://www.mk2048.com/blog/blog.php?id=hcbka1jabaa

总结一下各种0.5px的线的更多相关文章

  1. CSS画0.5px的线

    今天遇到一个需求,画0.5px的线,查了以下资料,主要以scale方法为主.代码如下: /*0.5px上边框*/ .half_top_border_before:before{ content: &q ...

  2. 实现0.5px边框线

    实现0.5px边框方法 方案一:利用渐变(原理:高度1px,背景渐变,一半有颜色,一半透明) CSS部分 .container { width: 500px; margin: 0px auto; } ...

  3. 画一条0.5px的线

    通过伪类元素:after为其添加样式,用transform:scaleY令其在垂直方向缩小0.5倍 .div:after{ height: 1px; transform: scaleY(0.5); t ...

  4. css3写出0.5px的边框

    一说到0.5px的边框,我们一般认为是不行的,因为在ps中0.5px的线也是做不出来的,这个计算机的像素有关系. 废话不多说了,0.5px 其实用的是css3新特性,box-shadow:阴影设置 代 ...

  5. 移动端 Retina屏border实现0.5px

    首先来看一下造成Retina边框变粗的原因 其实这个原因很简单,因为css中的1px并不等于移动设备的1px,这些由于不同的手机有不同的像素密度.在window对象中有一个devicePixelRat ...

  6. CSS 0.5px 细线边框的原理和实现方式

    细线边框的具体实现方法有:伪元素缩放或渐变,box-shadow模拟,svg画线,border-image裁剪等.要实现小于1px的线条,有个先决条件:屏幕的分辨率要足够高,设备像素比要大于1,即cs ...

  7. CSS3实现0.5px的边框

    前端页面细节处理好了才会显得精致.边框在网页中是常见的一种样式了.虽然不把它处理为0.5px看上去没毛病,但是想让你做的东西征服更多的人,这些细节处理是必须的. 今天主要说一下如何让边框显示0.5px ...

  8. css 0.5px

    1. 利用css3新属性scale(好用推荐) 其实现的关键就是利用before或after伪元素,宽高同时设为200%,然后再缩放0.5,自然就是0.5px了 代码如下: div { positon ...

  9. 怎么画一条0.5px的边

    编者按:本文由人人网FED发表于掘金,并已授权奇舞周刊转载 什么是像素? 像素是屏幕显示最小的单位,在一个1080p的屏幕上,它的像素数量是1920 1080,即横边有1920个像素,而竖边为1080 ...

随机推荐

  1. Sonic+p4(1)

    知识: Docker容器(打包环境加应用) 静态镜像运行container docker run(start)形成拓扑 目的:switch1获取switch2的网络统计数据 start.sh启动拓扑, ...

  2. LeetCode-026-删除有序数组中的重复项

    删除有序数组中的重复项 题目描述:给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度. 不要使用额外的数组空间,你必须在 原地 修改输入数组 ...

  3. java中的List接口(ArrayList、Vector、LinkedList)

    一.List接口有三个常用的集合(ArrayList.Vector.LinkedList) ArrayList注意事项 ArrayList底层是用数组来实现数据存储的 底层是 transient Ob ...

  4. 探索TiDB Lightning的源码来解决发现的bug

    背景 上一篇<记一次简单的Oracle离线数据迁移至TiDB过程>说到在使用Lightning导入csv文件到TiDB的时候发现了一个bug,是这样一个过程. Oracle源库中表名都是大 ...

  5. git常用命令及问题

    Git基本操作 git init 创建新的git仓库 git clone [url] 使用 git clone 拷贝一个 Git 仓库到本地 git status 查看工作区 git stash li ...

  6. 关于linux下的open()write()read()close()函数

    http://blog.sina.com.cn/s/blog_71d1a98701010s0v.html 1.read和write函数调用时,都会记录下当前写的位置,下次调用时就会从这个位置开始读或写 ...

  7. python+pytest接口自动化(10)-session会话保持

    在接口测试的过程中,经常会遇到有些接口需要在登录的状态下才能请求,否则会提示请登录,那么怎样解决呢? 上一篇文章我们介绍了Cookie绕过登录,其实这就是保持登录状态的方法之一. 另外一种方式则是通过 ...

  8. 玩转SpringBoot之定时任务@Scheduled线程池配置

    序言 对于定时任务,在SpringBoot中只需要使用@Scheduled 这个注解就能够满足需求,它的出现也给我们带了很大的方便,我们只要加上该注解,并且根据需求设置好就可以使用定时任务了. 但是, ...

  9. Java 将CSV转为Excel

    CSV(Comma Separated Values)文件是一种纯文本文件,包含用逗号分隔的数据,常用于将数据从一个应用程序导入或导出到另一个应用程序.通过将CSV文件转为EXCEL,可执行更多关于数 ...

  10. 什么是GUI?

    图形用户界面(Graphical User Interface,简称 GUI,又称图形用户接口)是指采用图形方式显示的计算机操作用户界面.图形用户界面是一种人与计算机通信的界面显示格式,允许用户使用鼠 ...