css实现各种0.5px的线(小结)

所属分类: 网页制作 / CSS 阅读数: 1571
收藏 0 赞 0 分享

在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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

CSS伪类对象before和after的用法实例详解

这两个伪类对象只有在清楚浮动clearfix的时候会用到哈,最近在研究css3的时候觉得它两个的搭配不仅能够减少代码量并且能整出很巴适的效果
收藏 0 赞 0 分享

CSS3 实用技巧:实现黑白图像效果示例代码

本文为大家详细介绍下CSS3实现黑白图像效果的具体思路及代码,感兴趣的朋友可以看下截图,希望对大家有所帮助
收藏 0 赞 0 分享

IE.JS解决IE兼容性问题方法汇总

正如标题所言它修复了许多的HTML和CSS问题,并使得透明PNG在IE5、IE6下正确显示,下面为大家介绍下具体针对不同浏览器的调用方法,感兴趣的朋友可以参考下哈
收藏 0 赞 0 分享

实现CSS3中的border-radius(边框圆角)示例代码

本文为大家详细介绍下如何实现CSS3中的border-radius(圆角),具体代码如下,感兴趣的朋友可以参考下哈,希望对大家有所帮助
收藏 0 赞 0 分享

CSS line-height行高上下居中垂直居中样式属性

我们在css编写中需要对大篇幅的内容显示的更好看,有些间隔,不要在挤在一起难看,就可以使用Line-Height属性进行控制
收藏 0 赞 0 分享

CSS Float布局过程与老生常谈的三栏布局

这篇文章就是总结一下怎样使用CSS中的float属性进行布局,其实网上有很多讨论这个话题的文章了,但我觉得都没说到点子上。那就来老生常谈一次吧,CSS之Float布局
收藏 0 赞 0 分享

邮箱css加载失败怎么办 网站css加载异常原因分析

造成css加载失败的原因有很多,脚本之家也遇到过,这可能跟你代码出错,浏览器、路径、编码等等都是有关联的。所以在具体情况具体分析。下面看看具体的方案
收藏 0 赞 0 分享

CSS控制样式的三种方式(优先级对比验证)

大家都知道,CSS的中文名叫做层叠样式表,而CSS在控制样式的时候,有三种引入方式,这里简单介绍下CSS控制样式的三种方式
收藏 0 赞 0 分享

meta http-equiv="X-UA-Compatible" content="IE=7" 意思是将IE8用IE7进行渲染

X-UA-Compatible是针对ie8新加的一个设置,对于ie8之外的浏览器是不识别的,这个区别与content=
收藏 0 赞 0 分享

Discuz7.2 IE9兼容性写法 杜工完全修补方案

因为Discuz7.2在IE9浏览器中有一系列的问题,所有要在以后的开发中考虑到ie9浏览器的一些问题了,这里简单介绍下,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多