在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. omnet++:cMessage、cSimpleModule、cGate

    cMessage Message可以被调度(self-message).取消.从一个Gate发送出去.直接发给另一个module:所有以上,都是通过cSimpleModule来实现的. Message ...

  2. WIN10:全选一个文件夹中的所有文件

  3. 关于电脑上已安装SqlServer2005再安装SqlServer23008r2的处理情况

    安装SqlServer2008r2可参考这个回答,带图很详细  https://xinzhi.wenda.so.com/a/1518683577611182 1.先修改2005注册表.win+R打开运 ...

  4. SQL Server内置的HTAP技术

    SQL Server内置的HTAP技术 目录 背景 SQL Server在OLAP上的发展 SQL Server的初代HTAP SQL Server逐渐增强的HTAP SQL Server列存总结 H ...

  5. 面试官:Redis中集合数据类型的内部实现方式是什么?

    虽然已经是阳春三月,但骑着共享单车骑了这么远,还有有点冷的.我搓了搓的被冻的麻木的手,对着前台的小姐姐说:"您好,我是来面试的."小姐姐问:"您好,您叫什么名字?&quo ...

  6. mysql常用索引

    1.索引 在关系数据库中,索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单.索引的作用相当 ...

  7. linux signal-list

    [root@bogon ~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) ...

  8. 在数据结构与算法中 传值方式(C语言)

    传值方式 前言 当初学顺序链表的时候,书上就出现了这样的语言,如下所示: Status InitList_Sq(SqList &L) { //构造一个空的线性表L. L.elem = (Ele ...

  9. 创世区块配置文件genesis.json的格式解读

    创世区块配置文件genesis.json的格式解读 中文网站上关于genesis 的解析大多数都来自于这个Gist:Ethereum private network configuration gui ...

  10. ZYNQ 驱动初始化套路--UART串口

    @ 目录 UART 串口 GPIO之 MIO控制 LED 套路如下: 1.定义硬件信息结构体 namePs_Config *Config; 2.定义驱动信息结构体 namePs name_Ps; 3. ...