无法在Bootstrap 5中使用CSS添加外部字体。

huangapple go评论56阅读模式
英文:

Impossible to add external font in CSS with Bootstrap 5

问题

抱歉,我只能为您提供翻译服务,无法处理代码部分。

英文:

Sorry I'm new here, my post might not be perfect but I'd appreciate if you could help me kindly.

So the issue is the following one : I can't make the font I download or linked work, either on my h1 font or body.

I'd like the PressGothic in the h1 and if it doesn't work, the Anton.

I've tried differents options :

  1. linked the downloaded font files with @font-face
  2. calling the front with @import url in the CSS and later in the file with font-family, even forcing it with "!important".

After many tries, only Oswald works in the body (in the footer only).

Any solution other than customizing bootstrap ? Because I will not follow the project and would prefer the other person not to have to redo the boostrap file and well, I would like to learn from my issue so I do better next time.

Thank you!

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

@font-face {
  font-family: &#39;Exo&#39;;
  src: url(&#39;assets/fonts/Exo-VariableFont_wght.ttf&#39;);
}

@font-face {
  font-family: &#39;PressGothic&#39;;
  src: url(&#39;assets/fonts/PressGothicPro.ttf&#39;) format(&#39;truetype&#39;);
}

@import url(&#39;https://fonts.googleapis.com/css2?family=Montserrat:ital,wght@0,100;0,300;0,400;0,500;0,700;1,100&amp;display=swap&#39;);
@import url(&#39;https://fonts.googleapis.com/css2?family=Oswald:wght@200;300;400;500;600&amp;display=swap&#39;);
@import url(&#39;https://fonts.googleapis.com/css2?family=Anton&amp;display=swap&#39;);
body {
  background: black;
  color: white;
  font-family: &#39;Oswald&#39;, sans-serif;
}

h1 {
  text-transform: uppercase;
  font-size: 5rem;
  font-family: &#39;Anton&#39;, sans-serif !important;
}

p {
  font-family: &#39;Montserrat&#39;, sans-serif;
}

<!-- language: lang-html -->

&lt;link href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css&quot; rel=&quot;stylesheet&quot; integrity=&quot;sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65&quot; crossorigin=&quot;anonymous&quot; /&gt;
&lt;link href=&quot;css/style.css&quot; rel=&quot;stylesheet&quot; /&gt;
&lt;/head&gt;

&lt;body id=&quot;page-top&quot;&gt;

  &lt;header class=&quot;text-white&quot;&gt;
    &lt;div class=&quot;container px-4 text-center&quot;&gt;
      &lt;h1&gt;title&lt;/h1&gt;
      &lt;p&gt;
        test font
      &lt;/p&gt;
  &lt;/header&gt;
  &lt;footer class=&quot;py-5 bg-dark&quot;&gt;
    &lt;div class=&quot;container px-4&quot;&gt;
      &lt;p class=&quot;m-0 text-center text-white&quot;&gt;
        copyright
        &lt;a class=&quot;text-light&quot; href=&quot;assets/doc/qr-code.pdf&quot;&gt;
            doc test&lt;/a
          &gt;
        &lt;/p&gt;
      &lt;/div&gt;
    &lt;/footer&gt;

<!-- end snippet -->

答案1

得分: 0

It seems like you imported the external font family correctly using @font-face. Still, after that, you are also importing the font families from Google Fonts, which causes the conflict between font families, which might be why you are not getting your font in your project.

要解决这个问题,请移除从Google Fonts导入字体的部分。

如果这没有解决您的问题,请告诉我 无法在Bootstrap 5中使用CSS添加外部字体。

英文:

It seems like you imported the external font family correctly using @font-face. Still, after that, you are also importing the font families from Google Fonts, which causes the conflict between font families, which might be why you are not getting your font in your project.

to Fix this remove the imports of the font from Google Fonts.

I hope I've been able to solve your query if not then let me know 无法在Bootstrap 5中使用CSS添加外部字体。

huangapple
  • 本文由 发表于 2023年5月10日 18:35:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定