简体   繁体   English

Font Awesome 5,如何设置 svg 伪元素的样式?

[英]Font Awesome 5, how to style svg pseudo element?

I'm using font awesome 5 pseudo elements to attach an :after tag to my element like this我正在使用 font awesome 5 伪元素将:after标签附加到我的元素上,如下所示

&:after {
    content: "\f068";
    font-weight:400;
    color:$brandRed;
    float:right;
    font-family: "Font Awesome 5 Pro"; 
}

Which is adding the content in fine, but the styling that I add, specifically float:right is not added to the SVG that is generated with font awesome?哪个添加内容很好,但是我添加的样式,特别是float:right没有添加到用字体真棒生成的 SVG 中?

Dom 截图

As shown in the image above, the SVG element is loaded correctly, but the :after tag has the float:right styling on it, and the SVG does not have any of the styling specified?如上图所示,SVG 元素已正确加载,但:after标签上有float:right样式,而 SVG 没有指定任何样式? Why is it not taking over the styling?为什么它不接管样式?

According to their docs , they say to add it like this, with all of your stylings根据他们的文档,他们说要像这样添加所有样式

.login::before {
  font-family: "Font Awesome 5 Free";
  font-weight: 900;
  content: "\f007";
}

Why are the stylings not carrying over?为什么样式没有延续?

When using the pseudo-element technique with the JS version, Font Awesome will use only the properties related to the icon to generate the SVG (content,font-family, font-weight, etc) and the pseudo-element will become useless.在 JS 版本中使用伪元素技术时,Font Awesome 将仅使用与图标相关的属性来生成 SVG(内容、字体系列、字体粗细等),而伪元素将变得无用。 If you check the the documentation you will find that you need to add display:none to the pseudo element:如果您查看文档,您会发现需要将display:none添加到伪元素中:

Set Pseudo Elements' display to none将伪元素的显示设置为无

Since our JS will find each icon reference (using your pseudo element styling) and insert an icon into your page's DOM automatically, we'll need to hide the real CSS-created pseudo element that's rendered .由于我们的 JS 会找到每个图标引用(使用您的伪元素样式)并自动将一个图标插入到您页面的 DOM 中,因此我们需要隐藏呈现的真实 CSS 创建的伪元素 ref 参考

If you need to apply properties like float you need to apply them to the generated SVG not the pseudo element.如果您需要应用像float这样的属性,您需要将它们应用到生成的 SVG 而不是伪元素。 So simply target the SVG:因此,只需定位 SVG:

 span:before { font-family: "Font Awesome 5 Free"; font-weight: 900; content: "\\f007"; /*display:none; I commented this mandatory property to see what is happening */ color:red; /* will do nothing*/ float:right; /* will do nothing*/ } /*target the svg for styling*/ .target-svg svg { color: blue; float:right; }
 <script data-search-pseudo-elements src="https://use.fontawesome.com/releases/v5.8.1/js/all.js"></script> <span class="target-svg">the icons is on the right --></span> <br> <span>the icons will not be styled</span>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM