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

实现下图的加号效果:

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;
}

此时边框是这样的:

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

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

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

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

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

参照上面, 我们可以使用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;
}

最终的样式:

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

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

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

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

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

总结

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