CSS小技巧——画出可以自己调整间距长度的虚线/border dashed/linear-gradient

需求

浏览器提供的dashed border画出来一直是一个样式,不能个性化定制虚线的长度以及间距,所以用这个方法画出可个性化定制的虚线。

实现

  background-image: linear-gradient(to bottom, red 0%, red 80%, transparent 50%);
  background-size: 3px 18px;
  background-repeat: y-repeat

background-image中,linear-gradient的第三个参数可以调整虚线每一段的长度显示百分比,结合background-size的第二个值(可设置虚线每一段总长度)使用。两者结合就可以调整虚线每一段之间的间距大小。
background-size的第一个值可以调整虚线的宽度。(可以设为100%,直接继承元素的宽度width

案例

右边蓝色的是浏览器自带的border样式,左边红色的是自己画出来的。
在这里插入图片描述
html

<div class="box">
  <div class="line"></div>
</div>

css

.box {
  width: 200px;
  height: 600px;
  background: 	rgba(255,99,71, .1);
  padding-right: 20px;
  border-right: 3px dashed blue;
  position: relative;
}
.line {
  position: absolute;
  right: 10px;
  width: 3px;
  height: 100%;
  background-image: linear-gradient(to bottom, red 0%, red 80%, transparent 50%);
  background-size: 100% 18px;
  background-repeat: repeat-y
}
  • 14
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值