完成到Laravel 5的初学者,并try 使用标题中的以下代码导入自定义字体:

<style>
@font-face {
    font-family: 'Conv_OptimusPrinceps';
    src: url('fonts/OptimusPrinceps.eot');
    src: local('☺'), url('fonts/OptimusPrinceps.woff') format('woff'), url('fonts/OptimusPrinceps.ttf') format('truetype'), url('fonts/OptimusPrinceps.svg') format('svg');
    font-weight: normal;
    font-style: normal;
}

and calling it in my variables.scss. Currently my fonts are stored in my public directory:

public/fonts/OptimusPrinceps.woff
public/fonts/OptimusPrinceps.tff 
etc.

For some reason this warning appears in my dev tools

Failed to decode downloaded font: http://localhost:3000/fonts/OptimusPrinceps.tff
OTS parsing error: invalid version tag

而且我的字体加载不正确.

推荐答案

Place anything that the client browser should access into /public/. You can use the Laravel helper function public_path to build full URLs for it.
https://laravel.com/docs/5.2/helpers#method-public-path

例如,如果你把字体放进/public/fonts/OptimusPrinceps.tff(你已经做到了),你可以通过两种方式之一访问它.

In Blade:

<style type="text/css">
@font-face {
    font-family: OptimusPrinceps;
    src: url('{{ public_path('fonts/OptimusPrinceps.tff') }}');
}
</style>

在CSS中包括:

@font-face {
    font-family: OptimusPrinceps;
    src: url('/fonts/OptimusPrinceps.tff');
}

In the second example, you don't need any Laravel magic, really. Just reference the path absolutely so that it points to the correct directory.

Worth noting that this works with Bootstrap and SCSS. I usually put fonts in /public/static/fonts/.

Laravel相关问答推荐

@vite指令在使用laravel vite构建后导致错误

Laravel-如何按第三表关系对数据进行分类

Ziggy 路由默认为查询字符串

如何在 Laravel 9 中获取带分页的订单列表?

如何在 Laravel 中使用 Vue 路由?

如何在 laravel livewire 中按 bool 值排序数组

如何在 Laravel 5 中添加我自己的自定义类?

Laravel 5 如何配置 Queue 数据库驱动程序以连接到非默认数据库?

在 laravel 5.3 中截断的错误日志(log)

Laravel 6 Passport 在错误的凭证上返回 400 Bad request

Laravel Eloquent - 随叫随到的加密/解密数据

克隆后设置 Laravel 项目

PHP 5.5,什么情况下PHP会导致很高的committed memory

未定义的类常量App\Providers\RouteServiceProvider::HOME

Laravel 控制器构造

Laravel 同步错误

Laravel 5 环境配置数组?

Composer RuntimeException - 无法加载软件包 mews/purifier

Laravel Nova 显示用户友好的资源名称

如何在 Laravel 5.3 中获取当前的语言环境