本地Bootstrap文件字体图标引入却无法显示问题的解决方法

你在使用bootstrap字体图标的时候,是否遇到引用本地Bootstrap文件无法显示字体图标,而使用CDN服务器上bootstrap却能正常显示的问题。

在不能正常显示的时候,比如我要在一个按钮中使用一个√的字体图标,我的代码是这样子的:

1

2

3

4

5

6

7

8

9

10

11

12

13

<!doctype html>

<html lang="en">

<head>

 <meta charset="UTF-8">

 <title>bootstrap字体图标</title>

 <link rel="stylesheet" href="../libs/bootstrap.css">

</head>

<body>

 <div class="container">

  <button class="btn"> <span class="glyphicon glyphicon-ok"></span></button>

 </div>

</body>

</html>

但是他的显示却是这个样子的:

 

找了很多解决办法,说法不一。下面来看看我是如何解决的。

发现不能显示之后我使用了goole cdn上的地址引入bootstrap文件,发现可以正常显示。所以问题应该出现在引入文件这里。
ctrl+左键进入glyphyicon,发现实现的代码是这样的:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

@font-face {

 font-family: 'Glyphicons Halflings';

 

 src: url('../fonts/glyphicons-halflings-regular.eot');

 src: url('../fonts/glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), url('../fonts/glyphicons-halflings-regular.woff2') format('woff2'), url('../fonts/glyphicons-halflings-regular.woff') format('woff'), url('../fonts/glyphicons-halflings-regular.ttf') format('truetype'), url('../fonts/glyphicons-halflings-regular.svg#glyphicons_halflingsregular') format('svg');

}

.glyphicon {

 position: relative;

 top: 1px;

 display: inline-block;

 font-family: 'Glyphicons Halflings';

 font-style: normal;

 font-weight: normal;

 line-height: 1;

 

 -webkit-font-smoothing: antialiased;

 -moz-osx-font-smoothing: grayscale;

}

在idea中就会发现@font-face这部分报红,提示can not resolve file glyphicons-halflings-regular.eot和glyphicons-halflings-regular.eot,意思是找不到文件。
所以glyphyicon这个样式,是关联着这些文件的,进入到下载的整个的压缩包,进入这个文件bootstrap-3.3.7-dist\fonts,就会发现如下文件:

所以glyphyicon这个样式,必须要关联到glyphicons-halflings-regular.eot等文件才能正常使用。
而在我的引用bootstrap文件中,我是这样引用的,可能你也正在犯跟我一样的错误:
<link rel="stylesheet" href="../libs/bootstrap.css">

在webstrom中看到我的libs目录是这样的:

是的,在使用bootstrap的大多样式的时候,单单是bootstrap.css这个文件就够了,不必引入全部的,这样可以让我们的项目没那么臃肿。但是在我们使用字体图标的时候,是需要关联到字体图标相关的文件才得以实现的,所以当我引入整个bootstrap-3.3.7(您也可以部分引入,只要将你想要的功能的相关文件全部引入且目录无误即可),然后再在我的html中这样引入:
<link rel="stylesheet" href="../libs/bootstrap-3.3.7/css/bootstrap.css">

这样就能够正常显示字体图标:

总结:分析了那么多,意思就是字体图标这个样式的实现,需要关联到glyphyicon相关文件,你在引入bootstrap.css文件时,你要确保在与bootstrap.css的相对路径下,能够让他找到这些关联文件,而CDN服务器上的正式如此,如此才能让图标正常显示。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Bootstrap 字体图标是通过使用字体文件和相应的 CSS 类来实现的。你可以通过以下两种方法来使用 Bootstrap 字体图标方法一: 1. 下载字体文件:打开 Bootstrap 的官方网站并浏览到图标库页面,点击安装按钮,页面将跳转并显示一个复制内容的链接。 2. 引用字体文件:将复制的内容粘贴到你的 HTML 文件中,确保将其放在你的 CSS 文件之后。这将引用所需的字体文件。 3. 使用图标:从图标库中选择你喜欢的图标,点击复制样式按钮。然后将复制的样式粘贴到你的代码中,并保存文件。 4. 打开浏览器查看:你将能够在浏览器中看到你选择的 Bootstrap 字体图标方法二: 1. 下载字体文件:打开 Bootstrap 的官方网站并浏览到图标库页面,点击安装按钮,页面将跳转并显示一个下载 ZIP 的链接。 2. 下载并解压 ZIP 文件:点击链接下载 ZIP 文件,并将其解压。你将会找到一个包含 font 文件夹的文件夹。 3. 引入字体文件:将刚才的 font 文件夹复制到你希望引入的位置,并引入以下文件。 4. 使用图标:从图标库中选择你喜欢的图标,点击复制样式按钮。然后将复制的样式粘贴到你的代码中,并保存文件。 5. 打开浏览器查看:你将能够在浏览器中看到你选择的 Bootstrap 字体图标。 通过以上步骤,你就可以成功地使用 Bootstrap 字体图标了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [详解Bootstrap glyphicons字体图标](https://download.csdn.net/download/weixin_38716872/13139767)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [bootstrap字体图标的使用](https://blog.csdn.net/danniyedan/article/details/120518081)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [Bootstrap V5 图标字体引入以及使用方法](https://blog.csdn.net/m0_60273757/article/details/121406485)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值