dongtanliefang8765 2018-09-23 03:13
浏览 194

模板中的Golang ttf字体

I'm trying to get a TTF font to work in a golang template, but it wont render the font. It shows up as regular Times New Roman. I can change fonts using the standard font-family fonts (ex verdana or 'helvetica'), but I cant import a TTF.

All I can seem to find about TTF fonts is libraries to add text to images, but I want to change web fonts. How can I achieve this?

Structure of project is

  • /html_templates/portal.html
  • /html_teplates/Comfortaa-Regular.ttf
  • main.go

Here is the relevant golang code:

import (
type Portal struct{
    Title string
func main(){
    //Create MUX Routing handlers
    http.HandleFunc("/", portal)

    //Start WebServer
    if err := http.ListenAndServe(":1234", nil); err != nil{ panic(err) }
func portal(w http.ResponseWriter, r *http.Request){
    //Create template
    tmpl, _ := template.ParseFiles("./html_templates/portal.html")

    //Populate struct
    portal := Portal{
        Title: "title",

    //Execute template with struct data
    tmpl.Execute(w, portal)

And the relevant HTML:

    <title>{{ .Title }}</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

        @font-face {
            font-family: 'comfortaaRegular';
            src: url('Comfortaa-Regular.ttf');
            src: local('comfortaaRegular'), 
                 url('Comfortaa-Regular.ttf') format('truetype'),
        body{ font-family: 'comfortaaRegular' }
  • 写回答



      相关推荐 更多相似问题


      • ¥15 找个人工智能的大佬咨询一些毕设问题
      • ¥15 需要教程:GD32F450I移植无系统lwip
      • ¥15 中文语料库检索构式 求助
      • ¥20 boost升压电路频率和占空比的选择
      • ¥15 Linux gcc升级问题
      • ¥100 如何封禁域名,让app打不开呢?
      • ¥15 通过安卓命令如何打开控制中心
      • ¥15 Nginx 配合Django 丢样式
      • ¥15 求问Python学生成绩管理系统的问题
      • ¥15 求🐏了个🐏第二关的代码,c语言