CSS\HTML - 如何将Ionicons添加到CSS的“content”属性中?

15

我正在使用自定义的switch切换(来自这里),我想要添加一个图标而不是普通文本。当我将Ionicons代码粘贴到CSS的content属性中时,它显示为一个矩形(表示字符\字体未找到)。我该如何解决这个问题?

编辑: 我忘记添加了,我已经包含了来自CDN的CSS。

JSfiddle

切换CSS:

.onoffswitch {
    position: relative; 
    width: 108px;
    -webkit-user-select:none; 
    -moz-user-select:none; 
    -ms-user-select: none;
}
.onoffswitch-checkbox {
    display: none;
}
.onoffswitch-label {
    display: block; 
    overflow: hidden; 
    cursor: pointer;
    border: 2px solid #999999; 
    border-radius: 30px;
}
.onoffswitch-inner {
    display: block; 
    width: 200%; 
    margin-left: -100%;
    -moz-transition: margin 0.3s ease-in 0s; 
    -webkit-transition: margin 0.3s ease-in 0s;
    -o-transition: margin 0.3s ease-in 0s; 
    transition: margin 0.3s ease-in 0s;
}
.onoffswitch-inner:before, .onoffswitch-inner:after {
    display: block; 
    float: left; width: 50%; 
    height: 40px; padding: 0; 
    line-height: 38px;
    font-size: 14px; 
    color: white; 
    font-weight: bold;
    -moz-box-sizing: border-box; 
    -webkit-box-sizing: border-box; 
    box-sizing: border-box;
}
.onoffswitch-inner:before {
    content: "ON";
    padding-left: 10px;
    background-color: #FFFFFF; 
    color: #BABABA;
}
.onoffswitch-inner:after {
    content: "\f425"; /* <--------THE ICON GOES HERE */
    padding-right: 10px;
    background-color: #8035A7; 
    color: #FFFFFF;
    text-align: right;
}
.onoffswitch-switch {
    display: block; 
    width: 41px; margin: -1.5px;
    background: #FFFFFF;
    border: 2px solid #999999; 
    border-radius: 50px;
    position: absolute; 
    top: 0; 
    bottom: 0; 
    right: 66px;
    -moz-transition: all 0.3s ease-in 0s; 
    -webkit-transition: all 0.3s ease-in 0s;
    -o-transition: all 0.3s ease-in 0s; 
    transition: all 0.3s ease-in 0s; 
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-inner {
    margin-left: 0;
}
.onoffswitch-checkbox:checked + .onoffswitch-label .onoffswitch-switch {
    right: 0px; 
}

HTML:

    <div class="onoffswitch">
        <input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked>
        <label class="onoffswitch-label" for="myonoffswitch">
            <p id="impressions_meter_text">Impressions meter</p>
            <span class="onoffswitch-inner"></span>
            <span class="onoffswitch-switch"></span>
        </label>
    </div>

看了node_modules\ionicons\dist\scss的源代码,不幸的是开发者在这里做得很糟糕,没有使用任何SCSS函数和mixin来允许您所要求的操作。 - vsync
4个回答

20

您缺少一个导入“Ionicons”字体的@font-face声明,另外.onoffswitch-inner:after声明没有指定font-family: " Ionicons";

我从他们的 CSS 中提取了 Ionicons 字体声明,并将其添加到a forked jsFiddle中。


7
您在使用字体图标时,需要在CSS的content属性中添加font-family属性,示例如下:
.custom-icon:after {
    content: "\f425";
    font-family: "the name of the font icon you are using";
}

And that's it :P


这是最糟糕的事情。如果某个图标被删除并且相应的unicode值不同,那么这将会出错。 - vsync

6

使用Ionic V2

如果你想从https://ionicframework.com/docs/v2/ionicons/使用ionicons,你只需要按照以下方式在css中进行操作即可。

.yourclass {
        font-family: "Ionicons";
        content: "\f3d1";
}

这将打印: <ion-icon name="arrow-forward"></ion-icon>

您可以在以下位置找到十六进制内容:

node_modules/ionic-angular/css/ionic.css:

node_modules/ionic-angular/css/ionic.dark.css

3
您需要导入ion icon css文件以加载图标字体,请查阅文档:https://github.com/driftyco/ionicons
  1. 下载并解压缩字体包
  2. 将ionicons.css复制到您的项目中
  3. 将fonts文件夹复制到您的项目中
  4. 确保ionicons.css中的字体URL正确引用了您项目中的字体路径。
  5. 从每个需要使用它的网页中包含对ionicons.css文件的引用。
然后,您可以使用框架提供的类名来代替直接放置"content",如:

2
那不是问题。 - Adam Jenkins
1
你错了,Adam。如果你不导入 CSS 字体,你永远无法加载图标。因此,在用户提供的示例中,图标字体不存在。 - Jose Mato
2
OP并没жңүиҜўй—®еҰӮдҪ•дҪҝз”Ёiж Үзӯҫ - OPиҜўй—®зҡ„жҳҜдёәд»Җд№ҲеңЁcontent规еҲҷдёӯеҢ…еҗ«еӯ—з¬Ұж—¶еӯ—дҪ“жІЎжңүжҳҫзӨәеҮәжқҘ - иҝҷжҳҜеӣ дёә1пјүеӯ—дҪ“жІЎжңүиў«еҢ…еҗ«пјҲжӯЈеҰӮжӮЁеңЁеё–еӯҗдёӯжүҖиҝ°пјүпјҢ2пјүеӣ дёәOPжІЎжңүеЈ°жҳҺfont-familyгҖӮ - Adam Jenkins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接