How to inline font-faces inside a CSS file
Hello, today I will show you how to quickly inline your font-face files inside a CSS file. The following example only explains the process for a woff file but the process is the same for all font-face formats:
- The first step is to get your woff file converted into a base64 string and afterwards make sure you trim those newline characters
- Create a new font-face @rule and insert the base64 string inside the font-face src but first make sure you've included
data:application/x-font-woff; base64,
which will tell the CSS parser that the string that follows is an inline base64-encoded .woff file.
Here is a simple example:
CSS
@font-face{
font-family: 'Open Sans';
font-style: normal;
font-weight: 400;
src: url(data:application/x-font-woff;base64,d09GRgABAAAAAAuEABEAAAAADxwAAQABAAAAAAAAAAAAAAAAAAAAAAAAAABHREVGAAABgAAAABYAAAAWABAABUdQT1MAAAGYAAAADAAAAAwAFQAKR1NVQgAAAaQAAAAMAAAADAAVAApPUy8yAAABsAAAAGAAAABgoT6eyWNtYXAAAAIQAAAARAAAAEQA9wEhY3Z0IAAAAlQAAABZAAAAog9NGKRmcGdtAAACsAAABKkAAAe0fmG2EWdhc3AAAAdcAAAAEAAAABAAFQAjZ2x5ZgAAB2wAAAGqAAABxqmuXS1oZWFkAAAJGAAAADYAAAA293bipmhoZWEAAAlQAAAAJAAAACQNzAXVaG10eAAACXQAAAAUAAAAFBUOAwdsb2NhAAAJiAAAAAwAAAAMAO8Bbm1heHAAAAmUAAAAIAAAACABngIKbmFtZQAACbQAAAC3AAABMhTcL0pwb3N0AAAKbAAAACAAAAAg/2kAZnByZXAAAAqMAAAA+AAAAQlDt5akAAEAAAAMAAAAAAAAAAIAAQAAAAQAAQAAAAEAAAAKAAoACgAAAAEAAAAKAAoACgAAAAMEtgGQAAUACAWaBTMAAAEfBZoFMwAAA9EAZgHxCAICCwYGAwUEAgIE4AAC70AAIFsAAAAoAAAAADFBU0MAQAAg//0GH/4UAIQIjQJYIAABnwAAAAAESAW2AAAAIAADAAAAAQADAAEAAAAMAAQAOAAAAAoACAACAAIAZQBoAGwAb///AAAAZQBoAGwAb////5z/mv+X/5UAAQAAAAAAAAAAAAB4nGMTYRBn8GPdBiRLWbexnmVA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GAQVlJjckUFGgMjCLAIDMzBTuZJenfp3eTn9Qw8rx7xsj/Z++KxYtXrly6eBmT6r8v/y53MTKtZeRh1P537t/vS3duX7548xoAtmNskwAAAAEAAAABGZqQdHC8Xw889QAJCAAAAAAAyTUxiwAAAADJ6ExM+5r91QmiCGIAAAAJAAIAAAAAAAAAAQAACI39qAAACaz7mv57CaIAAQAAAAAAAAAAAAAAAAAAAAUEzQDBBH0AcwTpALACBgCwBNUAcwAAAAAATQCLAKIA4wABAAAABQCKABYAVgAFAAIAEAAvAFwAAAEOAPgAAwABeJxljr0KwkAQhL/4i00QK8u8gD+xFBsVC0EUVCzERkyIQjCSi/gCPrjDeQFBlr2b2ZlZFmhypIpXa+HRAYcr+GJfXGVC4HDtx1Ony8nhBm0Kh332vNnwIOau5I6zfsNWPOFJKp4z1eRiHZHeXL6e+j8VMFPmplQkHDJUHWzCaJpZb0jfKmW6zF51U6HZmIHqZasv7aG+SI3FMm1KpKba9r3H6DWarFgyZ8Fa+xa6biT38AM20ShDAAADAAAAAAAA/2YAZgAAAAAAAAAAAAAAAAAAAAAAAAAAeJxNi7tOw0AQRXe8TqJUY4iwiMAe83Ca7Vj6RClMwoJ4mJHiREpFT2FTQ4OUJoiWr/C6y1/wIRR8gnGoOMXVPbq6o8/u0STiCEKmiWQKAcNhWIby2gzoymgySUyDc49jfcr9Xk0dt6a2rOlyqmnabD29yy2Q7OrmLQHlUJZSXiR9+k7gRB/zoT5gX+/xDiB7GhnxBh3CL3QQa3TaDggGLfhJvIhS/AjXE/DqQws28FE9pEqZTae+N7Z7u7CwsnG6zdHd3LZXVvB8MasA3rO39VqMA2PP0pmNgszYx6Z4QeWLcZbnSi3z4lltKVReqP/86f7yF3E7QQY=);
}
p{
font-family: 'Open Sans';
}
HTML
<p>Hello</p>
Credits go to the Twitter team and Sime Vidas for letting me know via this tweet