How to use .less file in nuxt js - vue.js

I created a .less file in my assets/css/myfile.less Nuxt folder and I added some CSS to it.
.edit-btn-color {
background-color: #b1c646;
color: white;
}
.edit-btn-color:hover {
background-color: darken(#b1c646, 10%);
color: white;
}
and in nuxt.config.js I do the following:
export default {
less: ['#/assets/css/myfile.less'],
}
But it does not work.

Since Nuxt2 is still using Webpack4, you need to install the v7 of less-loader (v8 is using Webpack5)
yarn add less-loader#^7 less
Then, create a .less file somewhere, like /assets/css/myfile.less
And use it in nuxt.config.js with this
export default {
css: ['~/assets/css/myfile.less'],
}
The key to use here is css, it's the same for SCSS, SASS, Less, Stylus and so on, as shown in the documentation: https://nuxtjs.org/docs/2.x/features/configuration#the-css-property

The answer for Nuxt v3 would be:
Install Less: npm i less (no less-loader needed).
For Global styles add them to your nuxt.config.ts like this:
export default defineNuxtConfig({
css: ['~/assets/less/main.less'],
})
In your components you could use Less like this:
<style lang="less" scoped>
#import (reference) '../assets/less/helpers.less';
nav {
background-color: darkkhaki;
.my-great-style; /* Imported from helpers.less */
}
</style>

Related

Why do SCSS variables imported from another file doesn't work in Vue 3?

I have installed sass loader in my vue 3 project, but the issue is that I want to import variables from another file, but doesn't work
my schema is:
src
assets
scss
styles.scss
_variables.scss
styles.scss
#import url('./_variables.scss');
body{
font-family: 'Montserrat';
background-color: $primary-black;
}
.sidebar{
width: 250px;
height: 100vh;
max-height: 100vh;
background-color: $primary-black;
}
_variables.scss
$primary-black: #222222;
App.vue
Some HTML
<script>
import Sidebar from './components/Sidebar';
export default {
components: { Sidebar },
setup() {
},
}
</script>
<style lang="scss" scope>
#import url('./assets/scss/styles.scss');
</style>
And the issue is that in the browser the $primary-black variable fails, it appears background-color: $primary-black literally, I mean doesn't take de "#222222" color instead, but if I change the variable, and put it inside the styles.scss file, it works, so I'm not sure what could be the problem
Do not use the url function while importing, try the below
<style lang="scss" scope>
#import './assets/scss/styles.scss'
</style>

override scss variables in vuetify

To increase the width of vuetify's v-switch, i want to modify the value of vuetify's scss variable.
vuetify was configured through vue-cli, and the developed code is as follows.
// src/assets/css/overrides.scss
$font-size-root: 24px;
$switch-width: 400px;
$switch-track-width: 400px;
// vue.config.js
module.exports = {
transpileDependencies: ['vuetify'],
configureWebpack: {
devtool: 'source-map',
},
css: {
loaderOptions: {
scss: { // 8.0.3
prependData: `#import "#/assets/css/overrides.scss";`,
},
},
},
};
But nothing has changed. What stupid thing am i doing?
ref:
https://vuetifyjs.com/en/customization/sass-variables/
https://cli.vuejs.org/guide/css.html#css-modules
I wasted a lot of time with this problem. But later, I realized it was easy. You don't need to import files or write loaderOptions in vuetify.config.js.
In your src folder, create a scss folder
In your new src/scss folder, create a variables.scss file
📁 src
├─ 📁 scss
| └─ 📄 variables.scss
...
The vuetify-loader will automatically bootstrap your variables into Vue CLI's compilation process, overwriting the framework defaults. Following this documentation.
Example
// projectRoot/src/scss/variables.scss
$font-size-root: 24px;
$switch-width: 400px;
$switch-track-width: 400px;
After doing all this, stop your local server and restart with npm or yarn only once. After these steps, every change you make will appear automatically. So you don't need to restart the development server every change.

How do I use sass globally in my Nuxt project?

I have a Vue.js component and I would like to access my sass variables from my scss file. On the NUXT documentation https://nuxtjs.org/faq/pre-processors/ it says that you can use node sass and sass-loader to do this. So I installed both of those and they are under dev dependencies in my package.json.
After that I added the following to my nuxt.config.js
css: [
'#/assets/sass/main.scss'
],
But when I try to do this in one of my components
<style lang="scss">
html, body{
background-color: $background-color;
overflow: hidden;
}
</style>
I get the error that $background color is undefined? Why is this happening?
My main.scss located in assets/sass
$text-color:#FFFFFF;
$background-color: #FFFFFF;
$primary-color: #1F2338;
$secondary-color: #F73455;
$navigation-opacity: 0.70;

How to inject Global constant to a vue component <style>?

I'm searching how to inject a global (THEME_NAME in my example) to all the vue components:
<template>..</template>
<style lang="scss">
#import "bulmaswatch/<%=THEME_NAME=>/bulmaswatch.scss";
.foo {
color: $primary;
}
</style>
Context:
I'm using the darkly theme of bulma css in my vue application (vue Cli 3)
<template>..</template>
<style lang="scss">
#import "bulmaswatch/darkly/bulmaswatch.scss";
.foo {
color: $primary;
}
</style>
In order to switch to the cyborg theme, I will have to replace darkly with cyborg everywhere...
Is there a better way? Something like
<template>..</template>
<style lang="scss">
#import "bulmaswatch/<%=THEME_NAME=>/bulmaswatch.scss";
.foo {
color: $primary;
}
</style>
then somewhere in Webpack or vue.config.js, we can decide what is the theme
configureWebpack: () => {
return {
plugins: [
new webpack.DefinePlugin({
THEME_NAME: "cyborg"
}),
],
}
}
In order to switch to the cyborg theme, I will have to replace darkly with cyborg everywhere...
Not if you use the re-export pattern.
There's no need for any external libraries and codegen magics with webpack.
Basically, you create a file where you import your theme.
Let's call it _my-bulma-theme.scss and let's import darkly for now.
// _my-bulma-theme.scss
#import "bulmaswatch/darkly/bulmaswatch.scss";
In your code, you import this file instead of importing from Bulma directly:
// some-component.vue
<style>
#import "../../my-bulma-theme";
</style>
// some-other-component.vue
<style>
#import "../../my-bulma-theme";
</style>
Now, when you want to change the theme, you just need to change it in one place: the _my-bulma-theme.scss file.
// _my-bulma-theme.scss
#import "bulmaswatch/cyborg/bulmaswatch.scss";

Change default font in vuetify

I can't figure out how to change the default font in vuetify. I've been looking for the right variable within ./node_modules/vuetify, but I can't locate it.
I'd ideally not make any changes in the module, but would rather override such variables from outside the module.
Best way
Define (if you use google fonts)
#import url('https://fonts.googleapis.com/css? family=Oxygen:300,400,700&display=swap');
#import url('https://fonts.googleapis.com/css? family=Comfortaa&display=swap');
$body-font-family: 'Oxygen';
$title-font: 'Comfortaa';
For vuetify 2+
.v-application {
font-family: $body-font-family, sans-serif !important;
.title { // To pin point specific classes of some components
font-family: $title-font, sans-serif !important;
}
}
In app.vue or a separate scss/css file imported directly into app.vue
For vuetify 1.5.x
In your app.vue script add
.application {
font-family: "Font Family Name";
}
.headline,
.title,
.subheading{
font-family: $body-font-family !important;
}
For example, if you are using a google font, your script tag should look like
<style lang="scss">
#import url("https://fonts.googleapis.com/css?family=Questrial");
.application {
font-family: "Questrial";
}
</style>
Update 2021
In your main.scss file,
$font-family:'Ubuntu'
.v-application {
[class*='text-'] {
color: #36405a;
font-family: $font-family, sans-serif !important;
}
font-family: $font-family, sans-serif !important;
}
The easiest way would be to simply set the font-family on body. If you are using webpack and importing the Vuetify stylus entry, main.styl, you can simply overwrite the $font-family variable with whatever font you want.
What worked for me using Nuxt.js with Vuetify Module was simply setting the body font in variables.scss, like this:
$body-font-family: SofiaPro, Roboto;
All other fonts are derived from this one.
Default variables file ('~vuetify/src/styles/styles.sass') is imported automatically afterwards and ignores a variable if it was already set (thanks to !default). Therefore there's no need to change $heading-font-family and individual $headings anymore.
For this to work with Nuxt module, do not forget to set treeShake: true in nuxt.config.js file. If you are not using Nuxt.js, then probably you need to import the variables file after setting the body font.
Here's an example of my nuxt.config.js file fragment:
buildModules: [
'#nuxtjs/vuetify'
],
vuetify: {
treeShake: true,
customVariables: ['~/assets/variables.scss'],
... other Vuetify options
}
Where viariables.scss contains the above font definition.
I wrote a short article explaining this subject, containing a complete code example: https://medium.com/#jareklipski/changing-default-font-in-vuetify-js-and-nuxt-js-3894e726ff10
So vuetify provides a very simple solution.
In your src directory create a sass, scss, or styles directory and then create a file named variables.scss or variables.sass in it.
When you run yarn serve or npm run serve, vuetify will automatically
hoist the global Vuetify variables to all of your sass/scss files.
Example - src/scss/variables.scss
Below code will change the default body font
#import url('https://fonts.googleapis.com/css2family=Poppins:wght#400;700&display=swap');
$body-font-family: 'Poppins', sans-serif;
You can change many more shit in there and can change webpack settings if above doesn't work for you directly - check this link
Hope it helps!
I have noticed that, at least in recent versions of Vuetify, you need to specify both $body-font-family and $heading-font-family to change the fonts of everything from Roboto to something else in your overrides (following these instructions). The redefinition of $headings seems to be necessary since it is defined in _variables.styl and depends on $heading-font-family. Note that Vuetify will be moving to SCSS in 2.0 so there will be a new process at that point.
$body-font-family = 'Barlow Condensed', sans-serif
$heading-font-family = 'Barlow Condensed', sans-serif
$headings = {
h1: { size: 112px, weight: 300, line-height: 1, letter-spacing: -.04em, font-family: $heading-font-family },
h2: { size: 56px, weight: 400, line-height: 1.35, letter-spacing: -.02em, font-family: $heading-font-family },
h3: { size: 45px, weight: 400, line-height: 48px, letter-spacing: normal, font-family: $heading-font-family },
h4: { size: 34px, weight: 400, line-height: 40px, letter-spacing: normal, font-family: $heading-font-family },
h5: { size: 24px, weight: 400, line-height: 32px, letter-spacing: normal, font-family: $heading-font-family },
h6: { size: 20px, weight: 500, line-height: 1, letter-spacing: .02em, font-family: $heading-font-family },
subheading: { size: 16px, weight: 400 },
body-2: { size: 14px, weight: 500 },
body-1: { size: 14px, weight: 400 },
caption: { size: 12px, weight: 400 },
button: { size: 14px, weight: 500 }
}
I cannot guarantee that this is "best practice". But considering that there is no documentation on how to do this properly I am going to tell you how I accomplished this.
I am using the Nuxt webpack template so my file structure may be a bit different than yours but the concept is the same.
I have a static assets folder. Within that folder I have a global css file. I downloaded the font I was using as a file and added it to my static directory as well. But you could put it pretty much anywhere.
Here is the code that I added to my global CSS file:
#font-face{
font-family: **any name you want to give the font**;
src: url(**location in directory**) format("opentype");
}
Then you just add it to you styling rules as you normally would
*{
font-family: **the same name you gave it above**;
}
h1{
font-family: **the same name you gave it above**;
}
ect...
Remember to enter the correct format. If your file downloads as a .otf it is opentype. If it is .ttf it is truetype.
I have not yet figured out how to include a font from CDN. If I do figure that out I will let you know.
For Laravel Vuetify users, this all you need:
Update your webpack.min.js file to look like:
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/main.scss', 'public/css')
.stylus('resources/stylus/main.styl', 'public/css');
Your main.styl should be located in the path: resources\stylus\main.styl
Your main.styl file should look like this:
#import url("https://fonts.googleapis.com/css?family=Literata:400,500,600,700&display=swap");
$body-font-family = 'Literata', serif
$alert-font-size = 18px
#import '~vuetify/src/stylus/main'
// For a-la-carte
#import '~vuetify/src/stylus/app'
To prevent Vuetify from writing inline styles that could override your main.css, do:
mix.options({
extractVueStyles: true, // Extract .vue component styling to file, rather than inline.
});
And finally, ensure stylus-loader is setup already, if not run in command line:
$ yarn add stylus stylus-loader style-loader css-loader -D
// OR
$ npm i stylus stylus-loader style-loader css-loader --save-dev
You can also npm install material-design-icons-iconfont.
npm install material-design-icons-iconfont --save
This solution work on Vue-CLI>=3
First of all you must install sass-loader
npm install sass sass-loader fibers deepmerge -D
You should first create "sass" folder in the "src" directory, then
create a "variables.scss" file in this directory.
Then write the below code in this file.
$ body-font-family: Your-FontName
#import '~vuetify/src/styles/settings/_variables.scss';
You need to restart your project now.
Install your font via CDN/npm. Then just override this variable by adding this line in ./src/styles/variables.scss
//Change default font to 'Inter'
$body-font-family: "Inter" !important;
A short and simple approach.
If you are using Vuetify 2+, the process will be very similar to jeffbaumes's answer but using Sass instead of Stylus
// src/sass/main.scss
#import '~vuetify/src/styles/styles.sass';
$body-font-family: 'My Custom Font', cursive;
$heading-font-family: $body-font-family, cursive;
#each $heading, $style in $headings {
$headings: map-merge($headings, ($heading: map-merge($style, ('font-family': $heading-font-family))));
}
Or you can change the fonts of some heading styles and not others like this:
$headings: map-merge($headings, ('h3': ('font-family': 'Custom Font Name')));
Unfortunately, #alice-mx answer didn't work for me (couldn't import from node_modules).
This is how I solved this problem in my code (using Vuetify 2):
After downloading the wanted .woff2 file and put it in src/assets/fonts,
I added this code to my App.vue file (or whichever main vue file you set in your project):
// App.vue
<style>
$typoOptions: display-4 display-3 display-2 display-1 headline title subtitle-1 subtitle-2 body-1 body-2 caption overline; // all md typography options provided by vuetify
%font-choice {
font-family: "Noto Sans", sans-serif !important;
}
#mixin md-typography {
#each $typoOption in $typoOptions {
.#{$typoOption} {
#extend %font-choice;
}
}
.v-application { // This is where we'll add all the md-typography classes
font-size: 12px;
#extend %font-choice;
#include md-typography;
}
// add font-face because in this case Noto-Sans is taken from Google fonts
#font-face {
font-family: "Noto Sans";
font-style: normal;
font-weight: 400;
src: local("Noto Sans"), local("NotoSans"),
url("~#/assets/fonts/noto-sans-v9-latin-regular.woff2") format("woff2");
}
</style>
I hope this will help!
This answer helped me to form the .scss code
My solution in (latest) Nuxt:
nuxt.config:
head: {
.......
link: [
{rel: 'icon', type: 'image/x-icon', href: '/favicon.ico'},
{
rel: 'stylesheet',
href:'https://fonts.googleapis.com/css?family=Raleway:400|Roboto+Slab:200&display=swap'
}
]
},
vuetify: {
treeShake: true,
customVariables: ['~/assets/variables.scss'],
theme: {
.....
},
}
},
variables.scss
$body-font-family: Raleway, sans-serif;
$heading-font-family: Roboto Slab, serif;
#import '~vuetify/src/styles/styles.sass';
I tried everything nothing was working in my case, but the below solution will definitely work though not the best solution
Add the below scss in your variables.scss
$body-font-family: "Open Sans",sans-serif;
.v-application {
.text-h1,
.text-h2,
.text-h3,
.text-h4,
.text-h5,
.text-h6,
.text-headline,
.text-title,
.text-subtitle-1,
.text-subtitle-2,
.text-body-1,
.text-body-2,
.text-button,
.text-caption,
.text-overline {
font-family: $body-font-family !important;
}
}
I'm using webpack and the only way I could make it work was:
// webpack.config.js
...
{
test: /\.sass$/,
use: [
{
loader: 'sass-loader',
// Vuetify
// Requires sass-loader#^9.0.0
options: {
implementation: sass,
sassOptions: { fiber: fibers },
additionalData: "#import '~/src/styles/variables.scss'",
}
}
]
}
...
// src/styles/variables.scss
#import "~vuetify/src/styles/settings/_variables.scss";
#import url('https://fonts.googleapis.com/css2?family=Fredoka+One&display=swap');
$body-font-family: 'Fredoka One', cursive;
$heading-font-family: $body-font-family;
$headings: (
'h1': (
'font-family': $heading-font-family,
),
'h2': (
'font-family': $heading-font-family,
),
'h3': (
'font-family': $heading-font-family,
),
'h4': (
'font-family': $heading-font-family,
),
'h5': (
'font-family': $heading-font-family,
),
'h6': (
'font-family': $heading-font-family,
),
);
Vuetify v2.4.11
Vue v2.6.12
To configure a custom font in Vuetify you need to include this CSS in your App.vue component.
<style lang="scss">
#import url('https://fonts.googleapis.com/css2?family=New+Tegomin&display=swap');
$font-family: 'New Tegomin', serif;
.my-application {
.headline,
[class*='display-'],
[class*='text-'] {
color: #36405a;
font-family: $font-family, sans-serif !important;
}
font-family: $font-family, sans-serif !important;
}
</style>
Put a specific class in your node, something like that:
<v-app class="my-application">
This second point is important because all the font styles from Vuetify have !important and if you want to overwrite, you need to put an extra class...
I hope that this tip helps everyone!
How I achieved it: (Vuetify 2+)
1) Into your index.html, import your fonts.
<link rel="stylesheet" href="<%= BASE_URL %>fonts/<MY_CUSTOM_FONT>/fontface.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Literata:100,300,400,500,700,900">
2) Inside /src, create a styles directory and a file named variables.scss inside it
3) In that file, override some variables values:
// Globals
$body-font-family: 'MY_CUSTOM_FONT'; // Used on content
$heading-font-family: 'Literata'; // Used on helpers classes
Hope it helps someone.
References:
https://github.com/vuetifyjs/vuetify/issues/8169
https://vuetifyjs.com/en/customization/sass-variables#example-variable-file
it is a late reply but , You can add in common scss/sass/less file (which is called in App.vue or you main App file) it will implement it all over the application
.body,
.v-application{
font-family: 'Antic', sans-serif;
}
For vue 3 with vite and vuetify 3.0.6 generated by yarn create vuetify
based on #arora answer and official docs
Run npm install -D sass
In src/plugins/vuetify.js replace import 'vuetify/styles with import '../assets/main.scss' as stated here
Create main.scss file in assets folder as stated
#use 'vuetify' with (
$utilities: false,
$color-pack: false,
$body-font-family: 'Assistant'
);
I had to set utililties: yes to resolve some style issues (Do not use true).
There is a much easier and proper way than all of the methods above. Add
treeShake: true,
defaultAssets: {
font: {
family: 'Poppins'
}
}
to the vuetify object in nuxt.config.js. This should properly enable your font family throughout the application.(see defaultAssets documentation for more details and note that the treeShake option is required)
Extra helpful thing:
when changing font from Roboto to any other font, by default vuetify will still import the cdn Roboto fonts file (url) in the "head" tag. To avoid that extra thing use the "defaultAssets: false" option under vuetify settings.
EDITED
That thing only works within NUXTJS, thanks to #nlavr for pointing it out
I've run into the same problem in February 2023. This is an answer that works specifically on Vuetify 3 inside Nuxt 3.
Here is a very simple way:
Go to get the source of your fonts. eg. Google font.
Input those fonts link in your nuxt.config:
head: {
link: [
{
rel: 'stylesheet',
href: 'https://your-font-source.com'
},
]
},
Apply the font-family to the html selector inside one of the Vue Components as a none scoped style:
<style>
html {
font-family: 'Your font name';
}
</style>
If there's no override font specifically set in the components this font will be used throughout the app.