How to Include Custom Fonts Using .ttf and Ensure Cross-Browser Compatibility?

Barbara Streisand
Release: 2024-11-07 08:10:03
Original
732 people have browsed it

How to Include Custom Fonts Using .ttf and Ensure Cross-Browser Compatibility?

Including Custom Fonts Using .ttf and CSS

You want to incorporate a .ttf font into your web page but encounter difficulties. Let's examine your code:

<code class="css">@font-face {
    font-family: 'oswald';
    src: url('/font/oswald.regular.ttf');
}</code>
Copy after login

Where's the Issue?

Including only a .ttf file is insufficient for cross-browser compatibility. To cover a wide range of browsers, you need multiple font formats.

Comprehensive Solution:

<code class="css">@font-face {
    font-family: 'MyWebFont';
    src: url('webfont.eot'); /* IE9 Compat Modes */
    src: url('webfont.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
         url('webfont.woff') format('woff'), /* Modern Browsers */
         url('webfont.ttf')  format('truetype'), /* Safari, Android, iOS */
         url('webfont.svg#svgFontName') format('svg'); /* Legacy iOS */
}</code>
Copy after login

This code assumes you have .eot, .woff, .ttf and SVG formats for your web font. Web font generators like Font Squirrel or Transfonter can automate this process.

Modern Approach:

Modern browsers prefer .woff fonts, so you can also simplify your code:

<code class="css">@font-face {
    font-family: 'MyWebFont';
    src: url('myfont.woff') format('woff'), /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
         url('myfont.ttf') format('truetype'); /* Chrome 4+, Firefox 3.5, Opera 10+, Safari 3—5 */
}</code>
Copy after login

Additional Resources:

  • CSS Tricks: Using @font-face: https://css-tricks.com/snippets/css/using-font-face/
  • Can I Use fontface: https://caniuse.com/fontface

The above is the detailed content of How to Include Custom Fonts Using .ttf and Ensure Cross-Browser Compatibility?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!