【问题标题】:CSS\HTML - How to add Ionicons to CSS "content" property?CSS\HTML - 如何将 Ionicons 添加到 CSS“内容”属性?
【发布时间】:2015-07-12 18:52:38
【问题描述】:

我正在使用自定义switch 切换(来自here),我想添加一个图标而不是常规文本。当我将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 函数和 mixins 来满足您的要求

标签: html css ionicons


【解决方案1】:

您缺少导入“Ionicons”字体的@font-face 声明,以及未指定font-family: "Ionicons";.onoffswitch-inner:after 声明。

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

【讨论】:

    【解决方案2】:

    您省略 font-family 属性.. 当您将字体图标中的代码用于 CSS 内容属性时,您必须这样做:

    .custom-icon:after {
        content: "\f425";
        font-family: "the name of the font icon you are using";
    }
    

    就是这样:P

    【讨论】:

    • 这是最糟糕的事情。如果某些图标将被删除并且相应的 unicode 值将不同,这将中断。
    【解决方案3】:

    使用 Ionic V2

    要在你的css中使用来自https://ionicframework.com/docs/v2/ionicons/的离子,你只需要这样做。

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

    这将打印: &lt;ion-icon name="arrow-forward"&gt;&lt;/ion-icon&gt;

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

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

    【讨论】:

      【解决方案4】:

      你需要导入ion icon css文件来加载icon字体。请阅读https://github.com/driftyco/ionicons的文档

      1. 下载并解压字体包
      2. 将 ionicons.css 复制到您的项目中
      3. 将字体文件夹复制到您的项目中
      4. 确保 ionicons.css 中的字体 URL 正确引用了项目中的字体路径。
      5. 在您需要使用的每个网页中包含对 ionicons.css 文件的引用。

      然后,您可以使用框架提供给我们的类,而不是直接放置“内容”,如:

      &lt;i class="icon ion-home"&gt;&lt;/i&gt;

      【讨论】:

      • 这不是问题。
      • 你错了Adam,如果你不导入css字体,你永远无法加载图标,所以在用户提供给我们的例子中,图标字体不存在。
      • OP 没有询问如何使用 i 标记 - OP 询问为什么当字符包含在 content 规则中时字体不显示 - 这是因为 1 )不包括字体(如您的帖子中所述)和2)因为OP没有声明font-family
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-10
      • 1970-01-01
      • 2017-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-08-02
      • 1970-01-01
      相关资源
      最近更新 更多