在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. 实现按钮跳转&下划线等

    按钮点击跳转页面:(在java文件里写) 1 public class MainActivity extends AppCompatActivity { 2 3 private Button mbtn ...

  2. Qt:QDir

    0.说明 QDir提供了访问目录及目录下内容的类. QDir既可以用于访问文件系统,也可以用于访问Qt 资源系统(Qt's resource system.). Qt用 "/" 作 ...

  3. .NetCore Web Api 利用ActionFilterAttribute统一接口返回值格式

    .Net Core 同 Asp.Net MVC一样有几种过滤器,这里不再赘述每个过滤器的执行顺序与作用. 在实际项目开发过程中,统一API返回值格式对前端或第三方调用将是非常必要的,在.NetCore ...

  4. Spring源码之九finishRefresh详解

    Spring源码之九finishRefresh详解 公众号搜索[程序员田同学],专职程序员兼业余写手,生活不止于写代码 Spring IoC 的核心内容要收尾了,本文将对最后一个方法 finishRe ...

  5. 微信小程序简易富文本

  6. Vue-表单验证-全选-反选-删除-批量删除

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 【1024打卡】C++字符串的输出((c语言风格)

    c++字符串输出(c语言风格) 文章目录 c++字符串输出(c语言风格) 杂记 代码 杂记 今天程序设计竞赛白给了,果然还是太弱了,y总带带我TAT ┭┮﹏┭┮1024快乐 代码 c语言学习 #inc ...

  8. CTS,CLS,CLR

    1)CTS通用类型系统(Common Type System) CTS不但实现了COM的变量兼容类型,而且还定义了通过用户自定义类型的方式来进行类型扩展.任何以.NET平台作为目标的语言必须建立它的数 ...

  9. DataTemplate的用法

    WPF 模板主要分为两大类:1.ControlTemplate: 控件的外观,也就是控件是什么样子.2.DataTemplate: 是数据内容的表现,一条数据显示成什么样子. (1)DataTemla ...

  10. 在Windows 10 上安装使用binwalk

    Windows 10 推出的WSL 功能可以协助我们直接使用binwalk 分析Windows 内的文件 文章更新时间:2020年3月11日 一.WSL 准备 1.打开控制面板→程序和功能→启动或关闭 ...