We'd like to be able to use a Font Awesome ( http://fortawesome.github.com/Font-Awesome/ ) icon as a bullet point for unordered lists in a CMS.
我们希望能够使用字体令人敬畏(http://fortawesome.github.com/Font-Awesome/)图标作为CMS中无序列表的项目符号。
The text editor on the CMS only outputs raw HTML so additional elements/ classes cannot be added.
CMS上的文本编辑器只输出原始的HTML,因此不能添加额外的元素/类。
This means displaying the icons when the mark up looks like this:
这意味着当标记看起来像这样时显示图标:
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
The first problem I can see if Font Awesome requires a different font-family attribute, which would require a separate element.
我可以看到的第一个问题是,Font Awesom是否需要不同的字体系列属性,这将需要一个单独的元素。
Is this possible using pure CSS? Or would I have to append the element to the beginning of each list item using something like jQuery?
使用纯CSS是否可能做到这一点?或者,我是否必须使用类似jQuery的命令将元素附加到每个列表项的开头?
I realise we can use a fall back of a background image, but it would be great to use Font Awesome if possible.
我意识到我们可以使用背景图像的后退,但如果可能的话,使用Font Awesom将是很好的。
更多回答
Solution:
解决方案:
http://jsfiddle.net/VR2hP/
Http://jsfiddle.net/VR2hP/
ul li:before {
font-family: 'FontAwesome';
content: '\f067';
margin:0 5px 0 -15px;
color: #f00;
}
Here's a blog post which explains this technique in-depth.
这里有一篇博客文章,深入解释了这项技术。
The new fontawesome (version 4.0.3) makes this really easy to do. We simply use the following classes:
新的Fontawous(4.0.3版)让这项工作变得非常容易。我们只需使用以下类:
<ul class="fa-ul">
<li><i class="fa-li fa fa-check-square"></i>List icons (like these)</li>
<li><i class="fa-li fa fa-check-square"></i>can be used</li>
<li><i class="fa-li fa fa-spinner fa-spin"></i>to replace</li>
<li><i class="fa-li fa fa-square"></i>default bullets in lists</li>
</ul>
As per this (new) url: http://fontawesome.io/examples/#list
根据此(新)URL:http://fontawesome.io/examples/#list
I'd like to build upon some of the answers above and given elsewhere and suggest using absolute positioning along with the :before pseudo class. A lot of the examples above (and in similar questions) are utilizing custom HTML markup, including Font Awesome's method of handling. This goes against the original question, and isn't strictly necessary.
我想在上面和其他地方给出的一些答案的基础上,建议使用绝对定位和:BEFORE伪类。上面的许多例子(以及类似的问题中的例子)都使用了定制的HTML标记,包括Font Awesom的处理方法。这与最初的问题背道而驰,也没有严格的必要。
DEMO HERE
演示请点击此处
ul {
list-style-type: none;
padding-left: 20px;
}
li {
position: relative;
padding-left: 20px;
margin-bottom: 10px
}
li:before {
position: absolute;
top: 0;
left: 0;
font-family: FontAwesome;
content: "\f058";
color: green;
}
That's basically it. You can get the ISO value for use in CSS content on the Font Awesome cheatsheet. Simply use the last 4 alphanumerics prefixed with a backslash. So []
becomes \f058
基本上就是这样。您可以在Font Awese小抄上获取用于css内容的ISO值。只需在最后4个字母数字前面加上一个反斜杠即可。因此[;]变成了\f058
There's an example of how to use Font Awesome alongside an unordered list on their examples page.
在他们的示例页面上,有一个如何在无序列表旁边使用Font Awesom的示例。
<ul class="icons">
<li><i class="icon-ok"></i> Lists</li>
<li><i class="icon-ok"></i> Buttons</li>
<li><i class="icon-ok"></i> Button groups</li>
<li><i class="icon-ok"></i> Navigation</li>
<li><i class="icon-ok"></i> Prepended form inputs</li>
</ul>
If you can't find it working after trying this code then you're not including the library correctly. According to their website, you should include the libraries as such:
如果您在尝试此代码后仍未发现它可以工作,则说明您没有正确包含该库。根据他们的网站,您应该包括这样的图书馆:
<link rel="stylesheet" href="../css/bootstrap.css">
<link rel="stylesheet" href="../css/font-awesome.css">
Also check out the whimsical Chris Coyier's post on icon fonts on his website CSS Tricks.
还可以看看Chris Coyier在他的网站css Tricks上关于图标字体的帖子。
Here's a screencast by him as well talking about how to create your own icon font-face.
下面是他关于如何创建自己的图标字体的截屏视频。
In Font Awesome 5 it can be done using pure CSS as in some of the above answers with some modifications.
在Font Awese5中,可以使用纯CSS来完成,就像在上面的一些答案中做了一些修改一样。
ul {
list-style-type: none;
}
li:before {
position: absolute;
font-family: 'Font Awesome 5 free';
/* Use the Name of the Font Awesome free font, e.g.:
- 'Font Awesome 5 Free' for Regular and Solid symbols;
- 'Font Awesome 5 Brand' for Brands symbols.
- 'Font Awesome 5 Pro' for Regular and Solid symbols (Professional License);
*/
content: "\f1fc"; /* Unicode value of the icon to use: */
font-weight: 900; /* This is important, change the value according to the font family name
used above. See the link below */
color: red;
}
Without the correct font-weight, it will only show a blank square.
如果没有正确的字体粗细,它将只显示一个空白正方形。
https://fontawesome.com/how-to-use/on-the-web/advanced/css-pseudo-elements#define
Https://fontawesome.com/how-to-use/on-the-web/advanced/css-pseudo-elements#define
@Tama, you may want to check this answer: Using Font Awesome icons as bullets
@Tama,你可能想检查这个答案:使用字体令人敬畏的图标作为项目符号
Basically you can accomplish this by using only CSS without the need for the extra markup as suggested by FontAwesome and the other answers here.
基本上,您只需使用CSS即可完成此任务,不需要额外的标记,如FontAwesom和这里的其他答案所建议的那样。
In other words, you can accomplish what you need using the same basic markup you mentioned in your initial post:
换句话说,你可以使用你在最初的帖子中提到的相同的基本标记来完成你需要的东西:
<ul>
<li>...</li>
<li>...</li>
<li>...</li>
</ul>
Thanks.
谢谢。
My solution using standard <ul>
and <i>
inside <li>
我的解决方案使用标准
<ul>
<li><i class="fab fa-cc-paypal"></i> <div>Paypal</div></li>
<li><i class="fab fa-cc-apple-pay"></i> <div>Apple Pay</div></li>
<li><i class="fab fa-cc-stripe"></i> <div>Stripe</div></li>
<li><i class="fab fa-cc-visa"></i> <div>VISA</div></li>
</ul>
DEMO HERE
这里演示
I like the icon with a background colour.
我喜欢背景色的图标。
ul.mylist {
list-style-type: none;
}
ul.mylist li {
margin: 0.5rem;
}
ul.mylist li:before {
font-family: 'Font Awesome 5 free';
content: "\f00c";
font-weight: 900;
color: white;
border: 1px solid #00a550;
padding: 0.2rem;
margin-right: 0.3rem;
background: #00a550;
}
更多回答
A useful list of translations from these font-awesome icons to css values is here: astronautweb.co/snippet/font-awesome
这里有一个有用的将这些字体令人敬畏的图标转换为CSS值的列表:Aeratitweb.co/Snipet/FONT-AWOW
You'll want to include this to remove the default bullet points ul { list-style-type: none; }
要删除默认项目符号ul{list-style-type:None;},您需要包括此代码
Why is this methode not working when I have two list on the same page ?
当我在同一页上有两个列表时,为什么这种方法不起作用?
Very nice, the only thing I would suggest to line things up nicer is to use margin: 0 0.2em 0 -1.2em;
just increase/decrease the two values by the same amount to get the desired spacing. If you use a fixed pixel value that doesn't correspond properly to your font you'll notice a discrepancy on multiline list items.
非常好,我建议更好地排列的唯一一件事是使用边距:0,0.2em,0-1.2em;只需将这两个值增加/减少相同的量,以获得所需的间距。如果您使用的固定像素值与您的字体不正确对应,您将注意到多行列表项上的差异。
I added a ul prefix to this otherwise it would try to do it if the client used the cms to create an ordered (numbered) list
我为此添加了一个ul前缀,否则,如果客户端使用CMS创建有序(编号)列表,它将尝试这样做
This should be the correct answer, with the new fa version. Very well done, thanks for this explanation.
对于新的FA版本,这应该是正确的答案。做得很好,谢谢你的解释。
I disagree, the question asked for a solution using a single list in pure CSS. This uses additional HTML, as well as classes. While this is definitely the way the FA docs specify to do this, it isn't technically the solution to the question, and isn't useful if you're looking to do this without modifying your HTML output.
我不同意,这个问题要求使用纯CSS中的单个列表来解决问题。这使用了额外的HTML和类。虽然这肯定是FA文档指定的方法,但从技术上讲,这不是问题的解决方案,如果您希望在不修改HTML输出的情况下执行此操作,则没有任何用处。
I know what you're going for - but people who aren't constantly writing pure CSS in situations where they can't modify their HTML???? (seriously, please look at changing your web framework) will need their google query memory refresher to link to the 'correct' font awesome docs way, juxtaposed with the 'actual' answer, next to each other to decide. I don't mean to say "there isn't just one correct answer to the problem" but there isn't...
我知道你想要什么--但是那些在不能修改他们的HTML的情况下不经常编写纯CSS的人?(说真的,请考虑改变你的网络框架)将需要他们的谷歌查询记忆刷新链接到‘正确的’字体可怕的文档方式,与‘实际’的答案并列在一起,彼此旁边决定。我的意思不是说“这个问题不只有一个正确的答案”,而是没有……
This approach doesn't work well with list elements wrapping to multiple lines, as identation on additional lines will not expand to include the width of the icon.
这种方法不适用于将列表元素换成多行,因为附加行上的标识不会扩展到包括图标的宽度。
Probably worth adding, with FA 4.3 (probably other versions too) and Bootstrap 3, the ul
and li
items need to be set position: relative
since the fa-li
item is positioned absolute. Otherwise, they will all float up to the top left.
可能值得添加的是,在FA 4.3(可能还有其他版本)和Bootstrap 3中,ul和li项需要设置为position:relative,因为fa-li项是绝对定位的。否则,它们都会浮到左上角。
You don't need the extra markup: li:before
can have a different font-family than the li
itself.
您不需要额外的标记:li:Being可以具有与li本身不同的字体系列。
@cimmanon: While you are indeed correct, the documentation for Font Awesome suggests you should have the extra markup. Technically it's not needed, however, I feel it may require some digging through the glyphs of the font to find out which key is mapped to which symbol. This would require some time and patience, but sure, you're solution would work as well.
@cimmanon:虽然您确实是正确的,但Font Awesom的文档建议您应该有额外的标记。从技术上讲,这是不必要的,然而,我觉得它可能需要一些挖掘字体的字形,以找出哪个键映射到哪个符号。这将需要一些时间和耐心,但可以肯定的是,您的解决方案也会奏效。
@cereallarceny - the problem we have is the HTML editor (TinyMCE) for the CMS produces the format I gave in my question. So from that perspective we don't have control over the list HTML. Unless we config TinyMCE to include the i element (and not delete it as an empty element).
@ereallarceny-我们遇到的问题是CMS的HTML编辑器(TinyMCE)产生了我在问题中给出的格式。因此,从这个角度来看,我们无法控制列表中的HTML。除非我们将TinyMCE配置为包括i元素(并且不将其作为空元素删除)。
So you don't have access to the HTML, do you have access to the CSS?
所以你不能访问HTML,你能访问CSS吗?
Yes, complete access to the CSS.
是,完全访问css。
In my case I had to use font family as 'Font Awesome 5 free' instead of 'FontAwesome' to make it work. Could be due to how Wordpress includes fontawesome fonts...
在我的例子中,我不得不使用字体系列作为‘Font Awese5 Free’,而不是‘FontAwese5’来使其工作。可能是因为WordPress包含了非常好用的字体。
How is this any different from the other HTML only solutions that were added in 2012 and 2014?
这与2012年和2014年添加的其他纯HTML解决方案有什么不同?
我是一名优秀的程序员,十分优秀!