首页 > 解决方案 > 字体不会从 CSS 文件更改

问题描述

目前使用带有引导程序 4 的 Django。无论出于何种原因,我都无法通过我的 main.css 文件更改字体系列。我可以更改其他属性,但字体似乎不起作用。

如果我更改其他文件中的字体系列,它会起作用。我只是无法通过我的 main.css 更改它。

我究竟做错了什么?

索引.html


{% load static %}

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'css/main.css' %}">
...

main.css

body {
   font-family: "Times New Roman", Georgia, serif !important;
}

标签: htmlcss

解决方案


您要么有 2 个选项,要么在 head 标签中创建样式标签,要么在 main.css 中选择所有内容而不是 body。例子:

索引.html

{% load static %}

<!doctype html>
<html lang="en">
<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'css/main.css' %}">
    <style>
        * {
            font-family: "Times New Roman", Georgia, serif !important;
        }
    </style>
...

或者

main.css

* {
    font-family: "Times New Roman", Georgia, serif !important;
}

推荐阅读