background-clip、outline 和 box-shadow与“边框透明”

题引

最近在看《css揭秘》这本书,跟着敲了两个案例

半透明边框

场景:div设置白色背景,边框透明,父容器背景是一幅图片
起初代码是这样的

   background: white;		
	border: 10px solid hsla(0,0%,100%,.5);

在这里插入图片描述
会发现背景设置的白色会把边框设置的样式覆盖掉,在不考虑通过两层div的方式等实现方式下,可以使用background-clip

    background: white;		
	border: 10px solid hsla(0,0%,100%,.5);			
	background-clip: padding-box;

结果为NaN
在w3school中是这样描述这个属性的:规定背景的绘制区域,在这里可以理解为规定白色背景的覆盖区域
在这里插入图片描述

外方内圆

在这里插入图片描述
在不考虑两层div等情况下,可以使用box-shadow + outline

先加上outline

 background: tan;
 border-radius: .8em;
 outline: .8em solid #655;

在这里插入图片描述
通 过 outline 属 性 实 现 的“ 边框”不会贴合元素的圆角这种行为被 CSS 工作组认为是一个 bug,因此未来可能会改为贴合 border-radius 圆角。

再用box-shadow 填补四角空白部分

background: tan;
border-radius: .8em;	
outline: .8em solid #655;
box-shadow: 0 0 0 .6em #655;

在这里插入图片描述

emmm

然后,写到这里我突然发现,outline 和 box-shadow 也都可以实现边框透明

background: white;		
outline: 10px solid hsla(0,0%,100%,.5);
background: white;		
box-shadow: 0 0 0 10px hsla(0,0%,100%,.5);

效果一样

总结

借着写博客来整理巩固知识,或许还有其他的小发现

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* 全局css变量 */ $--color-primary: #409EFF; .primary-color { color: #409EFF; } .background-opacity { background: rgba(64, 158, 255, 0.6); } .form-widget-list { .ghost{ content: ''; font-size: 0; height: 3px; box-sizing: border-box; background: #409EFF; border: 2px solid #409EFF; outline-width: 0; padding: 0; overflow: hidden; } } .el-form-item--medium { .el-radio { line-height: 36px !important; } .el-rate{ margin-top: 8px; } } .el-form-item--small { .el-radio { line-height: 32px !important; } .el-rate{ margin-top: 6px; } } .el-form-item--mini { .el-radio { line-height: 28px !important; } .el-rate{ margin-top: 4px; } } .el-card { margin-top: 3px; margin-bottom: 3px; } input[type="password"]::-ms-reveal { /* 隐藏IE/Edge原生的密码查看按钮 */ display: none; } /* 滚动条样式 begin */ ::-webkit-scrollbar { width: 8px; height: 8px; } ::-webkit-scrollbar-track { width: 8px; background: rgba(#101F1C, 0.1); -webkit-border-radius: 2em; -moz-border-radius: 2em; border-radius: 2em; } ::-webkit-scrollbar-thumb { background-color: rgba(#101F1C, 0.35); background-clip: padding-box; min-height: 28px; -webkit-border-radius: 2em; -moz-border-radius: 2em; border-radius: 2em; } ::-webkit-scrollbar-thumb:hover { background-color: rgba(#101F1C, 0.85); } * {//Firefox浏览器滚动条样式 scrollbar-color: #e5e5e5 #f7f7f9; //滚动条轨道颜色、滚动条滑块的颜色 scrollbar-width: thin; //thin模式下滚动条两端的三角按钮会消失 } /* body {//IE浏览器滚动条样式 scrollbar-shadow-color: #e5e5e5; scrollbar-face-color: #e5e5e5; scrollbar-base-color: #ffffff; scrollbar-arrow-color: #444040; } */ /* 滚动条样式 end */
最新发布
06-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值