动画复选框将SVG替换为Font Awesome

时间:2015-07-24 13:18:23

标签: css css3 font-awesome

我在这里创建了一个CodePen,它会激活一个复选框。目前正在使用SVG作为刻度,如何用字体替换它? example

input[type=checkbox] {
  opacity: 0;
  float: left;
}
input[type=checkbox] + label {
  margin: 0 0 0 20px;
  position: relative;
  cursor: pointer;
  font-size: 16px;
  font-family: monospace;
  float: left;
}
input[type=checkbox] + label ~ label {
  margin: 0 0 0 40px;
}
input[type=checkbox] + label::before {
  content: ' ';
  position: absolute;
  left: -35px;
  top: -3px;
  width: 25px;
  height: 25px;
  display: block;
  background: white;
  border: 1px solid #A9A9A9;
}
input[type=checkbox] + label::after {
  content: ' ';
  position: absolute;
  left: -35px;
  top: -3px;
  width: 23px;
  height: 23px;
  display: block;
  z-index: 1;
  background: url('data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjE4MS4yIDI3MyAxNyAxNiIgZW5hYmxlLWJhY2tncm91bmQ9Im5ldyAxODEuMiAyNzMgMTcgMTYiPjxwYXRoIGQ9Ik0tMzA2LjMgNTEuMmwtMTEzLTExM2MtOC42LTguNi0yNC04LjYtMzQuMyAwbC01MDYuOSA1MDYuOS0yMTIuNC0yMTIuNGMtOC42LTguNi0yNC04LjYtMzQuMyAwbC0xMTMgMTEzYy04LjYgOC42LTguNiAyNCAwIDM0LjNsMjMxLjIgMjMxLjIgMTEzIDExM2M4LjYgOC42IDI0IDguNiAzNC4zIDBsMTEzLTExMyA1MjQtNTI0YzctMTAuMyA3LTI1LjctMS42LTM2eiIvPjxwYXRoIGZpbGw9IiMzNzM3MzciIGQ9Ik0xOTcuNiAyNzcuMmwtMS42LTEuNmMtLjEtLjEtLjMtLjEtLjUgMGwtNy40IDcuNC0zLjEtMy4xYy0uMS0uMS0uMy0uMS0uNSAwbC0xLjYgMS42Yy0uMS4xLS4xLjMgMCAuNWwzLjMgMy4zIDEuNiAxLjZjLjEuMS4zLjEuNSAwbDEuNi0xLjYgNy42LTcuNmMuMy0uMS4zLS4zLjEtLjV6Ii8+PHBhdGggZD0iTTExODcuMSAxNDMuN2wtNTYuNS01Ni41Yy01LjEtNS4xLTEyLTUuMS0xNy4xIDBsLTI1My41IDI1My41LTEwNi4yLTEwNi4yYy01LjEtNS4xLTEyLTUuMS0xNy4xIDBsLTU2LjUgNTYuNWMtNS4xIDUuMS01LjEgMTIgMCAxNy4xbDExNC43IDExNC43IDU2LjUgNTYuNWM1LjEgNS4xIDEyIDUuMSAxNy4xIDBsNTYuNS01Ni41IDI2Mi0yNjJjNS4yLTMuNCA1LjItMTIgLjEtMTcuMXpNMTYzNC4xIDE2OS40bC0zNy43LTM3LjdjLTMuNC0zLjQtOC42LTMuNC0xMiAwbC0xNjkuNSAxNjkuNS03MC4yLTcxLjljLTMuNC0zLjQtOC42LTMuNC0xMiAwbC0zNy43IDM3LjdjLTMuNCAzLjQtMy40IDguNiAwIDEybDc3LjEgNzcuMSAzNy43IDM3LjdjMy40IDMuNCA4LjYgMy40IDEyIDBsMzcuNy0zNy43IDE3NC43LTE3Ni40YzEuNi0xLjcgMS42LTYuOS0uMS0xMC4zeiIvPjwvc3ZnPg==') no-repeat center center;
  -ms-transition: all .2s ease;
  -webkit-transition: all .2s ease;
  transition: all .3s ease;
  -ms-transform: scale(0);
  -webkit-transform: scale(0);
  transform: scale(0);
  opacity: 0;
}
input[type=checkbox]:checked + label::after {
  -ms-transform: scale(1);
  -webkit-transform: scale(1);
  transform: scale(1);
  opacity: 1;
}
<fieldset>
  <input id="ham" type="checkbox" name="toppings" value="ham">
  <label for="ham">Yay or Nay</label>
</fieldset>

2 个答案:

答案 0 :(得分:2)

您可以执行此操作,并根据需要调整lefttopfont-size值。

input[type=checkbox] + label::after {
    font-family: FontAwesome;
    content: '\f00c';
}

input[type=checkbox] {
    opacity: 0;
    float:left;
}
input[type=checkbox] + label {
    margin: 0 0 0 20px;
    position: relative;
    cursor: pointer;
    font-size: 16px;
    font-family: monospace;
    float: left;
}
input[type=checkbox] + label ~ label {
    margin: 0 0 0 40px;
}
input[type=checkbox] + label::before {
    content: ' ';
    position: absolute;
    left: -35px;
    top: -3px;
    width: 25px;
    height: 25px;
    display: block;
    background: white;
    border: 1px solid #A9A9A9;
}
input[type=checkbox] + label::after {
    font-family: FontAwesome;
    content: '\f00c';
    position: absolute;
    left: -35px;
    top: -3px;
    width: 23px;
    height: 23px;
    display: block;
    z-index: 1;
    -ms-transition: all .2s ease;
    -webkit-transition: all .2s ease;
    transition: all .3s ease;
    -ms-transform: scale(0);
    -webkit-transform: scale(0);
    transform: scale(0);
    opacity: 0;
}
input[type=checkbox]:checked + label::after {
    -ms-transform: scale(1);
    -webkit-transform: scale(1);
    transform: scale(1);
    opacity: 1;
}
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css">

<form method="post" action="/">
    <fieldset>
        <input id="ham" type="checkbox" name="toppings" value="ham">
        <label for="ham">Yay or Nay</label>
    </fieldset>
</form>

关注此link,了解如何将Font Awesome纳入您的项目。

答案 1 :(得分:2)

根据Using Font Awesome Icons without Tags,您可以这样做:

echo "<form action='activity1.php' method='post'>";
echo "<input type='checkbox' name='checkbox_test[]' value='1'>aaa    "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='2'>bbb    "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<input type='checkbox' name='checkbox_test[]' value='3'>ccc    "."<input type='submit' name='activity2' value='Activity2'><br>";
echo "<br><br>";
echo "<input type='submit' name='activity1' value='Activity1'>";
echo '</form>';

您当然必须使用input[type=checkbox] + label::after { content: '\f00c'; font-family: FontAwesome; font-size: 20px; position: absolute; left: -32px; top: 0px; width: 23px; height: 23px; display: block; z-index: 1;; -ms-transition: all .2s ease; -webkit-transition: all .2s ease; transition: all .3s ease; -ms-transform: scale(0); -webkit-transform: scale(0); transform: scale(0); opacity: 0; }

加载font-awesome css librairy