Laravel:如何修改仪表盘问候语div的字体样式与底部间距?
解决方案
先纠正你之前的几个错误点
public_path()不能用于前端路径:这是Laravel后端的服务器绝对路径函数,浏览器无法识别,必须用asset()生成前端可访问的URL。@font-face不需要闭合标签:语法就是@font-face { ... },IDE识别问题不影响实际运行。- HTML没有
font-size属性:要设置字体大小,必须用style属性或CSS类,不能直接写在标签上。
方法1:快速内联样式实现(适合临时调试)
在页面的 <head> 部分先正确引入自定义字体,然后给问候语div加内联样式:
<head> <!-- 其他head内容 --> @isset($user) <style> @font-face { font-family: 'Muli-Bold'; src: url('{{ asset('fonts/materialdesignicons-webfont.ttf') }}') format('truetype'); font-weight: bold; font-style: normal; } </style> @endisset </head> <body> <!-- 问候语div --> <div style="font-size: 28px; font-family: 'Muli-Bold', sans-serif; margin-bottom: 20px;"> @isset($user) Hallo {{ $user->name }}, @endisset </div> <!-- 后续内容 --> <div>Willkommen im <b class="text-gray-700">Formular-Editor!</b></div> <div>Neue Formulare kannst du über den Tab <i><b class="text-gray-700">"Editor"</b></i> oben in der Leiste anlegen. Hier kannst Du auch bestehende Formulare bearbeiten.</div> </body>
方法2:用CSS类实现(规范复用)
- 在项目的CSS文件(比如
resources/css/app.css)中添加样式:
/* 自定义字体 */ @font-face { font-family: 'Muli-Bold'; src: url('/fonts/materialdesignicons-webfont.ttf') format('truetype'); font-weight: bold; font-style: normal; } /* 问候语样式类 */ .dashboard-greeting { font-size: 28px; font-family: 'Muli-Bold', sans-serif; margin-bottom: 20px; }
- 编译CSS(如果用Vite,执行
npm run dev),然后在blade模板中使用类:
<div class="dashboard-greeting"> @isset($user) Hallo {{ $user->name }}, @endisset </div> <!-- 后续内容保持不变 -->
方法3:用Tailwind CSS实现(如果项目已使用)
看你的代码里有 text-gray-700,推测项目用了Tailwind,可直接用内置类+自定义字体:
- 在
tailwind.config.js中配置自定义字体:
module.exports = { theme: { extend: { fontFamily: { 'muli-bold': ['Muli-Bold', 'sans-serif'], }, }, } }
- 在页面
<head>引入字体:
<style> @font-face { font-family: 'Muli-Bold'; src: url('{{ asset('fonts/materialdesignicons-webfont.ttf') }}') format('truetype'); font-weight: bold; font-style: normal; font-display: swap; } </style>
- 使用Tailwind类:
<div class="text-2xl md:text-3xl font-muli-bold mb-6"> @isset($user) Hallo {{ $user->name }}, @endisset </div>
内容的提问来源于stack exchange,提问作者TraktorJack
相关产品推荐
相关产品推荐

