Use FontAwesome icon inside a ::before pseudo element with attr()

前端 未结 2 479
南旧
南旧 2021-02-05 17:18

I am trying to insert a FontAwesome icon inside a ::before pseudo element with attr() . The original code is more complex, however this will give you a

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-05 17:38

    CSS reads \f086 as a string and not as an escape sequence. The fix is either to use it directly inside content attribute like content: '\f086'; or directly copying and pasting icon in HTML attribute (make sure you save your file as UTF-8)

    HTML:

    CSS:

    div::before {
      content: attr(data-background-icon);
      font-family: "FontAwesome";
    }
    

    OR make use of HTML entities:

    HTML:

    CSS:

    div::before {
      content: attr(data-background-icon);
      font-family: "FontAwesome";
    }
    

    Fiddle: https://jsfiddle.net/76v9e2ur/1/

提交回复
热议问题