Using a custom font in a Django project without relying on the Google Fonts API

I've hit a roadblock in my programming journey and I'm seeking help. As a newcomer to both programming and Django, I've been following a helpful tutorial up until part 4.

Everything was going well until I stumbled upon a new font online called Crimson Foam that I'd like to use for the Django "logo" in the top bar, instead of the Google font used in the tutorial.

The issue is, I'm unsure how to incorporate this font into my base.html file and what steps to take with my CSS files (I know they are required). Here's how my static files are organized:

-- static
 |--fonts
 | |--crimson_foam.ttf
 |
 |--css
   |-- app2.css
   |-- bootstrap files

I attempted the following setup:

templates/base.html

{% load static %}<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>{% block title %}Django Boards{% endblock %}</title>
    <link href="{% static 'fonts/crimson_foam.ttf' %}" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}">
    <link rel="stylesheet" href="{% static 'css/app2.css' %}">
  </head>
Rest of the html file

I experimented with two versions of the .css file:

static/css/app2.css

Version 1:

title {
    color:#FAB4FF;
    font-family: "Crimson foam";      #didnt know what to put there
}

Version 2:

.navbar_brand {
    font-family: "Crimson foam", regular;
}

To be honest, I am feeling overwhelmed by the vast amount of information related to Django. While there are similar threads discussing this topic, they don't specifically address the HTML base file, which is where I'm struggling. Any assistance would be greatly appreciated!

Thank you all in advance for taking the time to read through this lengthy explanation!

Answer №1

For anyone searching for the solution to my query, I have found an answer:

The technique I used involves a css method known as @font-face.

To achieve the desired outcome, we need to include the following code in our css file:

css/app2.css

@font-face {
    font-family: "Choose any name for your font";
    src: url("either an absolute path or a relative path from your directory");
}

# In my example, the code appeared like this:

@font-face {
    font-family: "Crimson Foam";
    src: url("../fonts/crimson_foam.ttf);
}

This way, you add the font named "Crimson Foam" to your list of available fonts. Multiple @font-face methods can be included in one .css file.

To use the newly declared font, you can do either of the following:

  1. Assign it to a specific html tag:
html-tag.your_method_name {
    font-family: "One of the fonts assigned with @font-face", style (such as regular or cursive);
    font-size: A percentage (e.g., 150%) or pixel value like "36px";
}

# In my case, it looked like this:

h1.crimson_method {
    font-family: Crimson Foam, regular;
    font-size: 30px;
}
  1. You can also create a font method without associating it with a specific tag, which may appear as follows:
.method_name {
    font-family: "";
    font-size: "";
}

Once you have done that, you must also reference your method name in your html file within the class="something" section. This could look like the following:

{% load static %}<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title class="crimson_method">{% block title %}Django Boards{% endblock %}</title>
  </head>
Rest of the html file

The links mentioned in the html part of my question were used to link to a site where I obtained google fonts. There is also a method for incorporating google fonts, but there are numerous tutorials available on the web for that topic, such as this one.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to turn off and on all elements within a Div tag

Is there a way to disable all elements within a Div tag? I've managed to disable input types successfully, but I'm struggling with links. I attempted the solution provided here, but it didn't work. Here's the code snippet that worked f ...

What is the best way to position a title and subheading alongside an image?

I am currently working on developing a blog using php and html to enhance my coding skills. On my website, I have a special category where I display posts, the number of which can vary. Each post is composed of a title, a subheading, and a placeholder div ...

Checking for the Existence of a Database Table in HTML5 Local Storage

Upon each visit to my page, I automatically generate a few local database tables if they do not already exist. Subsequently, I retrieve records from the Actual Database and insert them into these newly created local tables. I am wondering if there is a me ...

Experience mind-bending timing functions in CSS animations with IE11

Despite working perfectly on Firefox, Chrome, and Edge, the animations on my website seem to have erratic timing on IE11. To see the animation as intended: However, on IE11, the animation behaves differently: I've attempted adjusting the animation- ...

Error in django boto3: NoCredentialsError - Unable to find valid credentials

I have a Django website running on an Amazon EC2 instance with Windows 10, served by IIS, and I'm using the boto3 module to send emails. I've installed awscli, set up my AWS access keys following this guide: https://i.sstatic.net/kWPta.png Here ...

Creating an element in react-draggable with two sides bound and two sides open - here's how!

At the moment, I am facing an issue where the element is restricted from moving outside of the container with the class of card-width-height. My goal is to have the right and bottom sides bounded within the container while allowing the element to move beyo ...

What is the best way to display various dialogs based on the specific button that is clicked?

Starting a new project, I foresee the need for numerous dialog boxes and modals. I've come up with some basic code that displays the dialog when a button is clicked and hides it when the cancel button is pressed. However, I'm facing the issue o ...

Customizing button appearances in the control div on Google Maps - A guide

Is there a way to manipulate elements and adjust styles by clicking buttons on the map control div? I want to achieve the same effect on the map as with the buttons in the table. Code: jsFiddle Update: Thanks to your assistance, I was able to achieve th ...

``CSS: Styling a Rounded Div with a Clipped Scrollbar

I am attempting to contain a scrollbar within a div so that it remains within the rounded corners without overflowing. Take a look at the code snippet below to see the problem in action. Is there a way for the scrollbar to be fixed in its position, while ...

Is the position relative malfunctioning when using display table-cell?

I have designed a horizontal menu consisting of buttons that I want to resize in width so that together they occupy 100% of the menu container. I need them to function like TD elements inside a TABLE. Here is the code snippet I have developed: <div id ...

Tips for making radio button selection mandatory in an HTML5 form

How can I require a user to select one radio button in an HTML 5 form before submitting it? I attempted to use the required attribute, but since there is no radio button group, how can I apply this requirement? <form action="/Home/Sendtitle" method=" ...

The z-index of two elements seems to be behaving differently than anticipated

I am encountering an issue with the code provided below, which is a simplified version of a larger problem. Can you explain why, if: .div_A {z-index: 10;} < .div_C {z-index: 20;} the button inside div_C appears behind div_A? I need to understand the ...

The Bootstrap Navbar appears hidden beneath other elements on mobile devices

While using bootstrap to style my header contents, I encountered a strange issue. The navbar that appears after clicking on the hamburger menu is displaying behind all the components. Even though I've set the z-index to the maximum value, it still doe ...

Unexpected problem with redrawing HTML application in Android WebView

I created a basic nixie clock app using HTML and wrapped it in a WebView for use on Android. It consists of one HTML file, a JS file, a CSS file, and ten digit images. After a few hours, I noticed stripes appearing in the center of the screen where the an ...

Can I use the mouseover event on an image to display a sibling div?

Hi everyone! I'm fairly new to web development and I could really use some advice. I'm trying to implement a mouseenter event on an img tag to show a sibling div, but I'm encountering some strange behavior. The mouseenter event seems to be w ...

Exploring Angular2's ability to interpret directive templates using the ng-container

Recently delving into angular2, I ventured into creating dynamic forms and generating fields by following the guide provided in this URL. The result was as expected. The dynamic form component renders each field one by one using ng-container, like shown b ...

Checking the status of a checkbox after submitting using AngularJs

For my first application, I am utilizing AngularJs and JavaScript to display information from an API as checkboxes. Currently, the functionality is working well, but I am unsure how to validate if any checkbox options are checked with a submit button. Aft ...

The div element is finally loading properly after multiple clicks

I need some assistance with loading dynamic divs in Angular. I have created a button that adds new divs each time it is clicked in a specific area. However, once these new divs are added, they appear incorrectly: After adding the divs, the editor and cale ...

The transparency feature using rgba is not supported in the Chrome browser for Android

Have you noticed the transparency effect in certain divs using the rgba CSS property? Strangely, Chrome on my ASUS tablet and Samsung S3 mini Galaxy smartphone does not seem to support this feature. Surprisingly, Internet Explorer does! Any insights on w ...

How can you incorporate a custom button into the controlBar on videoJS in responsive mode?

The video player I have created using videoJS includes custom buttons in the control bar. These buttons are not clickable when viewed on mobile or tablet devices without forcing them to work. let myButton = player?.controlBar.addChild('button'); ...