How to Override Bootstrap 5 Navbar Font Family Using SASS Variables?

27 Views Asked by At

I'm working with Bootstrap 5 in a sass project handled by webpack and I want to customize the font family of the navbar using SASS variables. I've tried various approaches, but I can't seem to get it right. Here's what I've attempted so far :

in my custom.scss file :

$primary: #FFBE0B;
$secondary: #FB5607;
$success: #3A86FF;
$info: #FF006E;
$light: #8338EC;


@font-face {
    font-family: 'Inter-Bold';
    src: url('../fonts/inter/Inter-Bold.ttf') format('ttf');
    font-weight: normal;
    font-style: normal;
}


$navbar-link-font-family: 'Inter-Bold', sans-serif;
$navbar-link-font-size: 16px;
$navbar-link-font-weight: 500;
$navbar-link-color: #333;

$navbar-link-font-family: $navbar-link-font-family;
$navbar-link-font-size: $navbar-link-font-size;
$navbar-link-font-weight: $navbar-link-font-weight;
$navbar-link-color: $navbar-link-color;

$navbar-brand-font-size: 1.5rem;


$font-family-base: $navbar-link-font-family

In my styles.scss file :

@import "./custom.scss";
@import "../../node_modules/bootstrap/scss/bootstrap";

My webpack conf :

'use strict'

const path = require('path')
const autoprefixer = require('autoprefixer')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const webpack = require("webpack");

module.exports = {
  mode: 'development',
  entry: './src/js/main.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
    clean: true,
  },
  devServer: {
    static: path.resolve(__dirname, 'dist'),
    port: 8080,
    hot: false
  },
  plugins: [
    new HtmlWebpackPlugin({ template: './src/index.html' }),
    new webpack.HotModuleReplacementPlugin(),
  ],
  module: {
    rules: [
      {
        test: /\.(scss)$/,
        use: [
          {
            // Adds CSS to the DOM by injecting a `<style>` tag
            loader: 'style-loader'
          },
          {
            // Interprets `@import` and `url()` like `import/require()` and will resolve them
            loader: 'css-loader'
          },
          {
            // Loader for webpack to process CSS with PostCSS
            loader: 'postcss-loader',
            options: {
              postcssOptions: {
                plugins: [
                  autoprefixer
                ]
              }
            }
          },
          {
            // Loads a SASS/SCSS file and compiles it to CSS
            loader: 'sass-loader'
          }
        ]
      }
    ]
  }
}

How can i correctly override the font family for the navbar ?

1

There are 1 best solutions below

0
JimMorrison On

Bootstrap styling has a higher priority then regular styling you've written in scss. If you have bootstrap locally installed you can edit the files with colors yourself for best practice.

As a last resort you can use the !important tag to overwrite the bootstrap styling on your sass variables.