How to efficiently load google fonts in Nuxt - css

I am using this google font font-family: 'Saira Semi Condensed', sans-serif;
Link: https://fonts.google.com/specimen/Saira+Semi+Condensed
I am working in on a NuxtJS project. I have to use this font in two different components but with different font-weight. I have imported all the google fonts links in Layout.vue.
For component A the font-weight is 600 & for component B the font-weight is 800. So I thought giving different font-weights in the respective component will work. But it is not working. The only basic font has applied i.e. Saira Semi Condensed, sans-serif; but the font-weight values are not reflected. To resolve this problem I need import two google font links with the same fonts but different font-weight in Layout.vue which makes it redundant.
For font-weight: 600
#import url('https://fonts.googleapis.com/css2?family=Saira+Semi+Condensed:wght#600&display=swap%27);
For font-weight: 800
#import url('https://fonts.googleapis.com/css2?family=Saira+Semi+Condensed:wght#800&display=swap%27);
I think my way of importing two links for the same fonts is not look good. Can you guys please help me to solve this issue?
Thank you in advanced.
Code:
Layout.vue
<template>
<div>
<Nuxt />
</div>
</template>
<style>
#import url('https://fonts.googleapis.com/css2?family=Nunito&display=swap');
#import url('https://fonts.googleapis.com/css2?family=Saira+Semi+Condensed:wght#600&display=swap');
#import url('https://fonts.googleapis.com/css2?family=Saira+Semi+Condensed:wght#800&display=swap');
#import url('https://fonts.googleapis.com/css2?family=Roboto:wght#700&display=swap');
html {
font-family: 'Source Sans Pro', -apple-system, BlinkMacSystemFont, 'Segoe UI',
Roboto, 'Helvetica Neue', Arial, sans-serif;
font-size: 16px;
word-spacing: 1px;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%;
-moz-osx-font-smoothing: grayscale;
-webkit-font-smoothing: antialiased;
box-sizing: border-box;
}
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
</style>
index.vue
<template>
<div>
<Navbar />
<ComponentA />
<ComponentB />
<Footer />
</div>
</template>
<script>
import Navbar from '../components/Navbar.vue'
import Clock from '../components/ComponentA.vue'
import Days from '../components/ComponentB.vue'
import Footer from '../components/Footer.vue'
export default {
components: {
Navbar,
ComponentA,
ComponentB,
Footer,
},
}
</script>
ComponentA.vue
<template>
<div>
<h1>I am component A</h1>
</div>
</template>
<script>
export default {
name: 'ComponentA',
}
</script>
<style scoped>
footer {
color: blue;
font-family: 'Saira Semi Condensed', sans-serif;
font-size: 20px;
text-align: center;
}
</style>
ComponentB.vue
<template>
<div>
<h1>I am component B</h1>
</div>
</template>
<script>
export default {
name: 'ComponentB',
}
</script>
<style scoped>
footer {
color: red;
font-family: 'Saira Semi Condensed', sans-serif;
font-size: 24px;
text-align: center;
}
</style>

You're loading your fonts from a CDN, which is not the recommended way of doing things.
Here is a quote from this awesome performance checklist 2021 written by Vitaly Friedman
Now, many of us might be using a CDN or a third-party host to load web fonts from. In general, it’s always better to self-host all your static assets if you can, so consider using google-webfonts-helper, a hassle-free way to self-host Google Fonts. And if it’s not possible, you can perhaps proxy the Google Font files through the page origin.
Looking at this, I do recommend the usage of #nuxtjs/google-fonts, this is a cool Nuxt module.
I've actually asked if my configuration of the module was okay, here is the github issue: https://github.com/nuxt-community/google-fonts-module/issues/26
And here, a usage example in nuxt.config.js
export default {
buildModules: [
[
'#nuxtjs/google-fonts',
{
families: {
Mali: {
wght: [400, 600, 700],
},
},
subsets: ['latin'],
display: 'swap',
prefetch: false,
preconnect: false,
preload: false,
download: true,
base64: false,
},
],
]
}
And don't forget to also handle the #font-face CSS side of course!
PS: in case you have some issues of specific weights not being downloaded, you can use either overwriting: true in your module configuration or bump the package version to v3.0.0-1.

Related

CSS How do you override the custom font family set in the all selector?

I downloaded two custom fonts for my website, and I am trying to make one of them the default font for almost everywhere in the website, and the other one for some certain areas. So my code look something like this:
function App() {
return (
<div className="App">
<nav>
<ul className="Navbar">
<li className="link1"><Link to="/">Home</Link></li>
<li className="link2"><Link to="/AboutMe">AboutMe</Link></li>
</ul>
</nav>
<Routes>
<Route path="/" element={<Home />}/>
<Route path="/AboutMe" element={<AboutMe />}/>
</Routes>
</div>
);
}
CSS:
*{
box-sizing: border-box;
margin: 0;
padding: 0;
line-height: 18px;
font-family: 'Montserrat-VariableFont_wght';
}
#font-face {
font-family: 'BowlbyOneSC-Regular';
src: url('../fonts/BowlbyOneSC-Regular.ttf') format('truetype');
font-weight: bold;
}
#font-face {
font-family: 'Montserrat-VariableFont_wght';
src: url('../fonts/Montserrat-VariableFont_wght.ttf') format('truetype');
}
.link1{
font-family: 'BowlbyOneSC-Regular';
}
Both link1 and link2 are now in the font of Montserrat-VariableFont_wght, and when I remove font-family: 'Montserrat-VariableFont_wght';from the * selector. Link1 will then be BowlbyOneSC-Regular and then link2 become some random default font provided by browser, which is not what I want. So, how should I do it?
You can make the
.link1{
font-family: 'BowlbyOneSC-Regular' !important;
}
notice ! important this will override the link1 css and link2 will be default one defined.
You can do something like a utility class with the font face. I usually do .ff-roboto .ff-arial and implement font family there.
.ff-BowlbyOneSC';{
font-family: 'BowlbyOneSC-Regular';
}
now you can use link2 as ff-BowlbyOneSC
<li className="link2 ff-BowlbyOneSC"><Link to="/AboutMe">AboutMe</Link></li>
* is not a good way of defining css property as it apply this font family to all the html element.
body{
box-sizing: border-box;
margin: 0;
padding: 0;
line-height: 18px;
font-family: 'Montserrat-VariableFont_wght';
}
you can also see I have removed regular from the name. It means you can also have font weight related utility functions like .fw-300 or fw-bold fw-black this how you can create reusable classes.

Vuejs - Svgs partially missing once a third is added

Not really sure how to ask this question to be honest, but recently I've started using VueJS and it's incredible!
That being said, with all the learning of Vue, I perhaps have lost a bit of my CSS skills; particularly when it comes to flex. I don't recall ever having this issue previously, but once I added a third SVG to my page, the two previous almost completely disappear, but the newest is fully visible.
Any ideas of what the heck could actually be happening here?
I'm not using SVG loader or anything, but rather a v-else-if to show svgs based on names; perhaps that's the issue? (though if that were, the two previously shown were fine until I added the third.)
Cheers!
NOTE: I'm not adding the SVG code, since I didn't want to have too much code showing in this question, but it's a simple hamburger menu svg, a search svg and the top nav stuff for an iphone.
App.vue
<template>
<div id="app">
<div class="container">
<top-nav-bar></top-nav-bar>
</div>
</div>
</template>
<script>
import TopNavBar from './components/molecules/TopNavBar.vue'
export default {
name: 'App',
components: { TopNavBar },
}
</script>
<style lang="sass">
#app
font-family: Avenir, Helvetica, Arial, sans-serif
-webkit-font-smoothing: antialiased
-moz-osx-font-smoothing: grayscale
text-align: center
color: #2c3e50
.container
position: relative
margin: 0 auto
border: 2px solid black
background-color: white
border-radius: 40px
width: 414px
height: 896px
</style>
Component:
<template>
<div class="container">
<icons name="nav-info" />
<icons name="hamburger" />
<icons name="search" />
</div>
</template>
<script>
import Icons from '../atoms/Icons.vue'
export default {
components: { Icons },
}
</script>
<style lang="sass" scoped>
.container
display: flex
flex-direction: column
align-items: center
margin: 0
</style>

Applying Primevue theme

Edit: As I was not able to figure this out, I instead dropped Vue3 and went back to Vue2 and switched to Vuetify.
I am completely new to Vue and decided to use Vue 3 for project, which is supposed to lead into my master thesis. As there are not very many UI libraries which are updated to run on Vue 3, I decided to go with Primevue.
Currently I am trying to apply one of Primevue's themes to my project, but the result is not very satisfying. I am using a dark theme, but the background is all white, while the components use the theme for the background and general styling.
I am hoping someone is able to help apply the styling correctly.
As a bonus question I am wondering whether it would be better to downgrade to Vue 2 and use a more well-established UI library such as Vuetify or BootstrapVue.
main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import TieredMenu from 'primevue/tieredmenu';
import InputSwitch from 'primevue/inputswitch';
// import 'primevue/resources/themes/bootstrap4-light-blue/theme.css';
import 'primevue/resources/themes/bootstrap4-dark-blue/theme.css';
import 'primevue/resources/primevue.min.css';
import 'primeicons/primeicons.css';
import 'primeflex/primeflex.css';
const app = createApp(App);
app.use(store)
.use(router)
.use(VueApexCharts)
.use(VueEllipseProgress)
.component('TieredMenu', TieredMenu)
.component('InputSwitch', InputSwitch)
.mount('#app')
App.vue
<template>
<div id="namegoeshere" >
<router-view/>
<div id="nav">
<!--<router-link to="/">Home</router-link> |
<router-link to="/about">About</router-link>-->
</div>
</div>
</template>
<style >
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
}
#nav {
padding: 30px;
}
#nav a {
font-weight: bold;
color: #2c3e50;
}
#nav a.router-link-exact-active {
color: #42b983;
}
</style>
I had a similar issue, having a look at the prime-vue documentation site with the devtools shows the following code is set on the body:
body {
margin: 0;
height: 100%;
overflow-x: hidden;
overflow-y: auto;
background-color: var(--surface-a);
font-family: var(--font-family);
font-weight: 400;
color: var(--text-color);
}
The variables used here come from the loaded theme. This solved the issue for me.
I couldn't find any direct information about having to set this in the docs.

How to set multiple columns with angular 4 and bootstrap 4?

Context:
I'm trying to do a basic thing:
<container>
<div class="row">
<div class="col-3">
...
</div>
<div class="col-4">
...
</div>
<div class="col-5">
...
</div>
</div>
Each column is dedicated to its own module. Let's say that first column is a vertical menu, middle column is a list of things and the last column is the detail of a thing selected in the second column. I'm using Angular4 and Bootstrap4 (with the help of ng-bootstrap). I'm new to both of them.
Issue:
First column is ok, it displays the menu with the expected size. An issue arise when trying to set the second column. This is the resulting code:
<div class="container-fluid">
<div class="row">
<div id="sidebar" class="col-md-3">
...
</div>
<router-outlet></router-outlet>
<list-of-things ng-version="4.0.3">
<div class="col-md-3">
...
</div>
</list-of-things>
...
</div>
</div>
The problem is that the component selector <list-of-things ng-version="4.0.3"> has a determined width and I don't know where this width comes from. Consequently, when I set the width col-md-3, 3 is relative to the size of <list-of-things ng-version="4.0.3"> and not the page width. So I end up with a very norrow list... Setting the value of the inner div to col-md-12 fills the <list-of-things> box.
Additional information:
Below is the CSS stack for the <list-of-things> straight from the web developer tool.
element {}
*,
*::before,
*::after {
-webkit-box-sizing: inherit;
box-sizing: inherit;
}
*,
*::after,
*::before {
-webkit-box-sizing: inherit;
box-sizing: inherit;
}
body {
font-family: "Segoe UI", "Source Sans Pro", Calibri, Candara, Arial, sans-serif;
font-size: 0.9375rem;
font-weight: normal;
line-height: 1.5;
color: #373a3c;
}
body {
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
font-size: 1rem;
font-weight: 400;
line-height: 1.5;
color: #292b2c;
}
html {
font-family: sans-serif;
line-height: 1.15;
-webkit-text-size-adjust: 100%;
}
html {
font-family: sans-serif;
line-height: 1.15;
-webkit-text-size-adjust: 100%;
}
I found the answer by my self by trial and error.
The best thing to do is to add the class "col-md-3" to the <list-of-things> tag. The hard part is that this tag is injected by angular via the component selector definition. The documentation about that selector is rather poor for the time being.
Basically, all example shows you to set the selector like this:
#Component({
selector: 'my-posts',
templateUrl: './../templates/posts.component.html',
providers: [PostService]
})
Apparently, if you put the selector value into brackets [ and ], the tag will be a <div> tag with whatever is in between the brackets.
The following example:
#Component({
selector: '[id=my-posts]',
templateUrl: './../templates/posts.component.html',
providers: [PostService]
})
generates the following tag:
<div id="my-posts" ng-version...>InnerHTML Here </div>
I want to set a css class on this div to specify the bootstrap col-md-3. But my selector can't be set on css class (that lead to an error related to recursion). It has to be set at least on an css id. With a bit of luck, I found that the following gives me what I want:
#Component({
selector: '[id=all-posts][class=col-md-6]',
templateUrl: './../templates/posts.component.html',
providers: [PostService]
})
Output:
<div class="col-md-6" id="all-posts" ng-version="4.0.3">
...
</div>
See also Can an angular 2 component be used with an attribute selector?

Can't load specific Roboto font

My problem is very simple and the symptom is weird.
In the head of my HTML, I have included the code to load the font directly from Google Font.
<link href="https://fonts.googleapis.com/css?family=Roboto:700" rel="stylesheet">
And this is my CSS:
.text {
font-family: 'Roboto', sans-serif;
color: white;
font-size: large;
}
No matter what customization I choose, the font seems to be the normal font. I tried with Roboto Thin (Roboto:100) and Roboto Thin Italic (Roboto:100i) but none of them actually change.
Is there anything that I miss in my code?
It depends on which font you have from google, in this case:
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,700" rel="stylesheet">
You approach to each font weight in CSS like that:
// 300 weight
.text {
font-family: 'Roboto', sans-serif;
font-weight: 300;
}
// 400 weight
.text {
font-family: 'Roboto', sans-serif;
font-weight: 400;
}
// 700 weight
.text {
font-family: 'Roboto', sans-serif;
font-weight: 700;
}
You are loading Roboto font with 700 font weight and trying to show it with 100 font weight. Embed it with the following URL:
<link href="https://fonts.googleapis.com/css?family=Roboto:100,100i,400,700" rel="stylesheet">
Using this link, it will work fine for you.
Update: This code snippet will explain you it in detail.
.text {
font-family: 'Roboto', sans-serif;
color: #000;
}
.text-100 {
font-weight: 100;
}
.text-100i {
font-weight: 100;
font-style: italic;
}
.text-400 {
font-weight: 400;
}
.text-700 {
font-weight: 700;
}
<link href="https://fonts.googleapis.com/css?family=Roboto:100,100i,400,700" rel="stylesheet">
<div class="text text-100">
Hello (Font weight: 100)
</div>
<div class="text text-100i">
Hello (Font weight: 100i)
</div>
<div class="text text-400">
Hello (Font weight: 400)
</div>
<div class="text text-700">
Hello (Font weight: 700)
</div>
You simply need to set the font of your weight in your CSS
.text {
font-weight: 700;
}
First of all load all the fonts-
Just use
font-weight: 100|300|700 /Any one of them/ .
Google fonts basically work with font-weight property. The google fonts are rendered according to the font weight specifications.
For example-
Case 1 - font-weight:100 then thin font will load.
Case 2 - font-weight:300 then light font will load.
Case 3 - font-weight:700 then bold font will load.
In your css file, add:
#import url('https://fonts.googleapis.com/css?family=Roboto:400,700');
at the top.
And, user it in the desired class like:
.class{
font-family: 'Roboto', sans-serif;
font-weight: 700;
}

Resources