css实现“加号”效果的实例代码

发布时间:2022-04-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了css实现“加号”效果的实例代码脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

实现下图的加号效果:

加号

若想实现这个效果, 只需一个div元素即可搞定

需要用到css的为了before和after, 以及border特性。

先设置一个div便签

 <div class="add"></div>

再设置一个边框:

.add {
  border: 1px solid;
  width: 100px;
  height: 100px;
  color: #ccc;
  transITion: color .25s;
  position: relative;
}

此时边框是这样的:

初始图片

我们可以利用伪类before和其border-top来设置一个“横”:

.add::before{
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 80px;
  margin-left: -40px;
  margin-top: -5px;
  border-top: 10px solid;
}

注意我们使了绝对定位。 此时变成了这样:

加横

参照上面, 我们可以使用after伪类和border-bottom设置一个“竖”:

.add::after {
 content: '';
 position: absolute;
 left: 50%;
 top: 50%;
 height: 80px;
 margin-left: -5px;
 margin-top: -40px;
 border-left: 10px solid;
}

在加上hover伪类,设置鼠标悬浮上去的颜色:

.add:hover {
  color: blue;
}

最终的样式:

finally-picture

当鼠标悬浮上去是, 会变色:

change-color

大家可以在这里查看效果:

https://jsbin.com/quvidap/edit?html,css,output
&nbsp;

总结

以上所述是小编给大家介绍的css实现“加号”效果的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本宝典网站的支持!

脚本宝典总结

以上是脚本宝典为你收集整理的css实现“加号”效果的实例代码全部内容,希望文章能够帮你解决css实现“加号”效果的实例代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:实现实现