文章轉(zhuǎn)載自:http://blog.csdn.net/nongweiyilady/article/details/53611094
以下是原文,簡(jiǎn)單來(lái)說(shuō)解決辦法就是:直接在項(xiàng)目中引入fonts文件夾就可以了,無(wú)需多余操作男窟,引入/css/bootstrap.min.css樣式文件時(shí)boostrap會(huì)自動(dòng)尋找fonts文件夾下的字體圖標(biāo)
你在使用bootstrap字體圖標(biāo)的時(shí)候照筑,是否遇到引用本地Bootstrap文件無(wú)法顯示字體圖標(biāo)芹关,而使用CDN服務(wù)器上bootstrap卻能正常顯示的問(wèn)題滓玖。
在不能正常顯示的時(shí)候详拙,比如我要在一個(gè)按鈕中使用一個(gè)√的字體圖標(biāo)遍尺,我的代碼是這樣子的:
[html] view plain copy
print?
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>bootstrap字體圖標(biāo)</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>
但是他的顯示卻是這個(gè)樣子的:
網(wǎng)上查找了很多解決辦法截酷,說(shuō)法不一。下面來(lái)看看我是如何解決的乾戏。
發(fā)現(xiàn)不能顯示之后我使用了goole cdn上的地址引入bootstrap文件迂苛,發(fā)現(xiàn)可以正常顯示。所以問(wèn)題應(yīng)該出現(xiàn)在引入文件這里鼓择。
ctrl+左鍵進(jìn)入glyphyicon三幻,發(fā)現(xiàn)實(shí)現(xiàn)的代碼是這樣的:
@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中就會(huì)發(fā)現(xiàn)@font-face這部分報(bào)紅,提示can not resolve file glyphicons-halflings-regular.eot和glyphicons-halflings-regular.eot呐能,意思是找不到文件念搬。
所以glyphyicon這個(gè)樣式,是關(guān)聯(lián)著這些文件的摆出,進(jìn)入到下載的整個(gè)的壓縮包朗徊,進(jìn)入這個(gè)文件bootstrap-3.3.7-dist\fonts,就會(huì)發(fā)現(xiàn)如下文件:
偎漫,所以glyphyicon這個(gè)樣式爷恳,必須要關(guān)聯(lián)到glyphicons-halflings-regular.eot等文件才能正常使用。
而在我的引用bootstrap文件中象踊,我是這樣引用的温亲,可能你也正在犯跟我一樣的錯(cuò)誤:
<link rel="stylesheet" href="../libs/bootstrap.css">
在webstrom中看到我的libs目錄是這樣的:
是的,在使用bootstrap的大多樣式的時(shí)候杯矩,單單是bootstrap.css這個(gè)文件就夠了栈虚,不必引入全部的,這樣可以讓我們的項(xiàng)目沒(méi)那么臃腫菊碟。但是在我們使用字體圖標(biāo)的時(shí)候节芥,是需要關(guān)聯(lián)到字體圖標(biāo)相關(guān)的文件才得以實(shí)現(xiàn)的,所以當(dāng)我引入整個(gè)bootstrap-3.3.7(您也可以部分引入逆害,只要將你想要的功能的相關(guān)文件全部引入且目錄無(wú)誤即可)头镊,然后再在我的html中這樣引入:
<link rel="stylesheet" href="../libs/bootstrap-3.3.7/css/bootstrap.css">
這樣就能夠正常顯示字體圖標(biāo):
總結(jié):分析了那么多,意思就是字體圖標(biāo)這個(gè)樣式的實(shí)現(xiàn)魄幕,需要關(guān)聯(lián)到glyphyicon相關(guān)文件相艇,你在引入bootstrap.css文件時(shí),你要確保在與bootstrap.css的相對(duì)路徑下纯陨,能夠讓他找到這些關(guān)聯(lián)文件坛芽,而CDN服務(wù)器上的正式如此,如此才能讓圖標(biāo)正常顯示翼抠。