常见Icon 图标css实现

本文深入探讨了使用纯CSS实现各种图标的设计方法,包括加号、箭头、搜索、警告等图标,通过精妙的CSS属性如transform和伪元素::before与::after,展示了如何创建动态且响应式的图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

.icon {
  position: relative;

  font-style: normal;
  //   padding: 10px;
  box-sizing: border-box;
}
//--------------------------plus--------------------//
.icon-plus::before,
.icon-plus::after {
  content: "";
  width: 16px;
  height: 2px;
  background-color: currentColor;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate3d(-50%, -50%, 0);
}
.icon-plus::after {
  width: 2px;
  height: 16px;
}
//--------------------------right--------------------//
.icon-right::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 14px;
  height: 8px;
  border-bottom: 2px solid currentColor;
  border-left: 2px solid currentColor;
  transform: translate(-50%, -50%) rotate(-52deg);
  box-sizing: border-box;
  margin-top: -1px;
}
//--------------------------cross--------------------//
.icon-cross::before,
.icon-cross::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 2px;
  height: 14px;
  transform: translate(-50%, -50%) rotate(45deg);
  background-color: currentColor;
}
.icon-cross::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}
//--------------------------arrow-left--------------------//
.icon-arrow-left::after {
  content: "";
  display: inline-block;
  vertical-align: middle;
  width: 10px;
  height: 10px;
  border-left: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: 2px;
  -webkit-transform: translate(-50%, -50%) rotate(45deg);
  -ms-transform: translate(-50%, -50%) rotate(45deg);
  transform: translate(-50%, -50%) rotate(45deg);
  box-sizing: border-box;
}
//--------------------------arrow-right--------------------//
.icon-arrow-right::after {
  content: "";
  display: inline-block;
  vertical-align: middle;
  width: 10px;
  height: 10px;
  border-top: 2px solid currentColor;
  border-right: 2px solid currentColor;
  -webkit-transform: rotate(45deg);
  -ms-transform: rotate(45deg);
  transform: rotate(45deg);
  position: absolute;
  left: 50%;
  top: 50%;
  margin-left: -2px;
  -webkit-transform: translate(-50%, -50%) rotate(45deg);
  -ms-transform: translate(-50%, -50%) rotate(45deg);
  transform: translate(-50%, -50%) rotate(45deg);
  box-sizing: border-box;
}
//--------------------------search--------------------//
.icon-search::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  border: 2px solid currentColor;
  height: 12px;
  width: 12px;
  box-sizing: border-box;
  border-radius: 50%;
  margin-left: -2px;
  margin-top: -2px;
}
.icon-search::after {
  content: "";
  background: currentColor;
  height: 6px;
  width: 2px;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-left: 4px;
  margin-top: 4px;
  -webkit-transform: translate(-50%, -50%) rotate(-45deg);
  -ms-transform: translate(-50%, -50%) rotate(-45deg);
  transform: translate(-50%, -50%) rotate(-45deg);
}
//--------------------------warning ,info,question,ok--------------------//
.icon-warning,
.icon-info,
.icon-question,
.icon-ok {
  display: inline-block;
  vertical-align: middle;
  position: relative;
  width: 18px;
  height: 18px;
  border: 1px solid currentColor;
  border-radius: 50%;
  line-height: 16px;
  text-align: center;
}
.icon-warning {
  color: red;
}
.icon-warning::after {
  content: "!";
}
.icon-info {
  color: red;
}
.icon-info::after {
  content: "i";
}
.icon-question {
  color: red;
}
.icon-question::after {
  content: "?";
}
.icon-ok {
  color: red;
}
.icon-ok::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 5px;
  height: 2px;
  border-bottom: 1px solid currentColor;
  border-left: 1px solid currentColor;
  -webkit-transform: translate(-50%, -50%) rotate(-52deg) scale(1.5);
  -ms-transform: translate(-50%, -50%) rotate(-52deg) scale(1.5);
  transform: translate(-50%, -50%) rotate(-52deg) scale(1.5);
  margin-top: -1px;
}
//--------------------------checkbox,radio-------------------//
.icon-checkbox,
.icon-radio {
  display: inline-block;
  vertical-align: middle;
  width: 16px;
  height: 16px;
  color: #999;
  border: 1px solid currentColor;
  background-color: #fff;
  position: relative;
  top: -1px;
  margin-right: 5px;
  box-sizing: border-box;
}
//--------------------------checkbox--------------------//
.icon-checkbox::before {
  content: "";
  width: 5px;
  height: 6px;
  background-color: #fff;
  position: absolute;
  right: -1px;
  top: -1px;
}
.icon-checkbox::after {
  content: "";
  height: 4px;
  width: 10px;
  border: 1px solid currentColor;
  border-width: 0 0 2px 2px;
  position: absolute;
  top: 1px;
  left: 3px;
  -webkit-transform: rotate(-52deg);
  -ms-transform: rotate(-52deg);
  transform: rotate(-52deg);
}
//--------------------------radio--------------------//
.icon-radio {
  border-radius: 10px;
}
.icon-radio::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  position: absolute;
  left: 50%;
  top: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
}
//--------------------------switch--------------------//
.icon-switch {
  height: 30px;
  width: 50px;
  position: relative;
  display: inline-block;
  vertical-align: middle;
  background-color: #ccc;
  border-radius: 15px;
  overflow: hidden;
  z-index: 1;
}
.icon-switch::before {
  content: "";
  height: 30px;
  width: 50px;
  position: absolute;
  left: -50px;
  background-color: #099fde;
  border-radius: 15px;
  -webkit-transition: left 0.2s ease 0s;
  transition: left 0.2s ease 0s;
  z-index: 2;
}
.icon-switch::after {
  content: "";
  height: 26px;
  width: 26px;
  background-color: #fff;
  border-radius: 15px;
  position: absolute;
  left: 2px;
  top: 2px;
  -webkit-transition: left 0.2s ease 0s;
  transition: left 0.2s ease 0s;
  z-index: 3;
}
.icon-switch.active::before {
  left: 0;
}
.icon-switch.active::after {
  left: 22px;
}
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值