Having trouble importing font-face in scss

Currently, I am working on incorporating a Google font into my project.

This is the desired outcome:
https://i.sstatic.net/jIO7q.png

However, the actual result is different:
https://i.sstatic.net/V2KhW.png

The code snippet in App.vue looks like this:

<template>
  <div id="app">Luckiest Guy</div>
</template>

<script>
export default {
  name: "App",
};
</script>

<style scoped lang="scss">
@import "./assets/scss/main.scss";

#app {
  font-family: "Luckiest Guy", "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

In the main.scss file, I import the fonts using the following line:

@import '../fonts/fonts.scss';

The contents of fonts.scss include:

@font-face {
  font-family: 'Luckiest Guy';
  src: url('https://fonts.googleapis.com/css2?family=Luckiest+Guy&display=swap');
}

If you want to take a look at the entire setup, here is the link to the Codesandbox:
https://codesandbox.io/s/dazzling-frost-2mbuf?file=/src/App.vue

Update 1
I have also attempted to use a downloaded otf file, but it has not been successful.

My updated fonts.scss now includes:

@font-face {
   font-family: 'Luckiest Guy';
   src: url('<The relative path of the otf>');
}

Update 2

Another attempt with a downloaded otf file:

@font-face { font-family: 'Luckiest Guy'; src: url('./LuckiestGuy-Regular.ttf'); }

To view the latest changes, you can visit the Codesandbox link:
https://codesandbox.io/s/dazzling-frost-2mbuf?file=/src/assets/fonts/fonts.scss

Answer №1

When you want to import fonts from Google Fonts, you have a couple of options. You can either use the import URL provided by Google, or you can download the font and include it in your project. If you choose to import the font using CSS code, it will look something like this:

/* latin */

@font-face {
  font-family: 'Luckiest Guy';
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url(https://fonts.gstatic.com/s/luckiestguy/v11/_gP_1RrxsjcxVyin9l9n_j2hTd52.woff2) format('woff2');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

If you encounter issues with the src property in your @font-face, there are three ways you can resolve it:

Option 1: Use the original URL

You can use the same URL that Google uses for importing the font ()

Option 2: Download the font locally

Alternatively, you can download the font files and save them within your project directory. Then, use font-face to import the font in your SCSS. To download the font, visit the Google Fonts page for the font family (), download the font files, and place them in your project folder. Instead of using an external URL in the src property, reference the local file path.

Option 3: Use Google's import URL

The simplest solution is to import the font using Google's import URL in your CSS:

@import url("https://fonts.googleapis.com/css2?family=Luckiest+Guy&display=swap");

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

What is the best way to vertically center elements within a navigation bar?

I'm currently working on a React application and I am trying to create a navigation bar. However, I am encountering some difficulties with aligning the elements in the middle of the nav bar layout. You can see the issue depicted in the image at this l ...

Interactive Autocomplete Component

I am encountering issues with passing dynamic data to my autocomplete angularjs directive, which is built using jQuery-UI autocomplete. Below is the current code I am working with: HTML: <div ng-app="peopleApp"> <div ng-controller="indexCont ...

What is the best way to access a JSON Array in php without using any specified keys?

I'm dealing with a JSON object created in JavaScript and passed to PHP via AJAX. The issue I'm facing is that I can't figure out how to assign keys to each JSON object within the JSON array. Here's an example of how the JSON array looks ...

Iterating over an array of numbers in AngularJS

I have an array of numbers $scope.N = [1,2,3,4]. I want to loop through this array in my HTML code using ng-repeat, similar to the example with JSON ng-repeat. <div ng-repeat="num in N"> {{num}} </div> How can I achieve this with a ...

Executing a function in Vue.js upon state changes

I am trying to trigger a function when the state changes in my Vue app. In my component, I can retrieve the boolean state of isOpen. The goal is to execute a function that sets focus on my form input when the modal opens and isOpen is true. I attempted ...

What is the best way to connect a series of checkboxes within a form utilizing Angular?

I created a form with checkboxes that allow users to select multiple options. However, when I submit the form, instead of receiving an array of objects representing the checked checkboxes, I'm not getting anything at all. Here is what I see in the co ...

Tips for creating an endless scrolling/loader feature in Nuxt?

Hey there! I am looking to display data after implementing infinite loading. The data is sent asynchronously using asyncData to the page with an ID. Page <script> export default { async asyncData({ $axios, store }) { const customerId = store.g ...

Tracking the frequency of text values that have been clicked on

Feeling uncertain about the best direction to take. In a table of unknown length, the first column consists of clickable links, while the second column contains corresponding text. The number of rows in this table is variable and depends on search resul ...

Is it possible to generate a component dynamically using a string template?

I have a Component called "CDataTable" that renders columns in a for loop. Inside the loop, there is a template: <template v-if="typeof col.template !== 'undefined'" #body="{data}"> <component :is="compile(co ...

The parent container is not properly wrapping its content in Internet Explorer only

Check out this code snippet I have here: https://jsfiddle.net/kimwild/mtxgtwr5/2/ main#wrapper { z-index: 1; width: 100%; position: relative; overflow: hidden; background-color: red !important; border: 10px dotted #4D37DB; } figure#about-im ...

Icon button not found

I have created a reusable hook component for input fields. The TextField renders perfectly, but the IconButton is not showing up. const InputHookComponent = (props) =>{ const [val, setval]=useState(""); const cmp = <TextField type={ ...

In IE9, the margin: auto property does not effectively center a div element

I'm trying to center a content div in the space leftover by a sidebar. Here's how I want it to look: After some trial and error, I was able to achieve this for most browsers by using margin: auto on the specific div, along with setting overflow: ...

Encountering an issue with receiving "undefined" values while utilizing WordPress post metadata in AngularJS

Utilizing the Wordpress REST API v2 to fetch data from my functional Wordpress website to an AngularJS application. Everything is functioning properly, however when I attempt to access post meta such as "_ait-item_item-data", it returns an error stating "u ...

Combining a JavaScript NPM project with Spring Boot Integration

Recently, I built a frontend application in React.js using NPM and utilized IntelliJ IDEA as my IDE for development. Additionally, I have set up a backend system using Spring Boot, which was also developed in IntelliJ IDEA separately. My current goal is t ...

Display the React component following a redirect in a Next.js application that utilizes server-side rendering

Just starting out with next.js and encountering a problem that I can't seem to solve. I have some static links that are redirecting to search.tsx under the pages folder. Current behavior: When clicking on any of the links, it waits for the API respo ...

How can I retrieve data from another module's state in Vuex?

I currently have two separate modules: module1.js export default { state: () => ({ array: [ { property: value } ] }) } module2.js export default { state: () => ({ array: [ { property: structuredCl ...

How can I send parameters to an HTML file using Node.js?

In my current code res.sendfile('./home.html',{user:req.user}), I need to figure out a way to access the user parameter directly in the HTML file without relying on a template engine. Can anyone suggest how this can be achieved? ...

Can loading HTML and js through ajax be considered secure?

I am currently utilizing the Jquery load function $('#result').load('test.php'); in order to dynamically load a page within another page when clicking on a tab. The page being loaded contains javascript, php, and a form. Upon inspecting ...

Function to save prices as cent-based figures in Javascript using Regex

Trying to extract prices from a string using regex can be tricky, as unexpected issues may arise. For example, obtaining the following values: US$1234.56 $12 $12.34usd $0.56 .56 dollars and converting them to: 123456 1200 1234 56 56 is necessary for s ...

Prevent event bubbling in Vue.js when using the data-toggle attribute from Bootstrap

How can I stop event propagation from the anchor href while using v-bind:data-toggle in Vue.js? <template> <ul class="nav navbar-nav"> <li v-for="(item, i) in items" :class="classes(item)"> ...