A guide to adjusting font sizes using Sass in Bootstrap 4

I'm feeling a bit confused as I've come across conflicting information regarding font sizes. Some sources suggest changing the font size from the HTML element like this:

html { font-size: 14px }

Then Bootstrap 4 rem values will adjust the text accordingly.

However, I noticed that if I leave the html element's font size at its default and only change the bootstrap variables to alter the font size, is that incorrect?

For instance:

html { font-size: 16px }

// Changing bootstrap variable to 
$font-size-base: 0.875rem //instead of 1rem

Answer №1

After encountering numerous conflicting answers, I decided to reach out to the developers of bootstrap for clarification once and for all.

It has become apparent that modifying $font-size-base is essential as it directly impacts the root font-size.

Their contributor also advised against manipulating the font-size using the html element and recommended adjusting bootstrap's variable instead.

Reference: https://github.com/twbs/bootstrap/pull/24060

Warning regarding sole CSS overrides

Simply applying css overrides may not suffice for all aspects of bootstrap sizing.

The compiled scss result heavily relies on the $font-size-base variable for sizing various elements, which could potentially expand to more areas in the future.

List of elements affected by size changes

  • dropdown font
  • button font
  • popover font
  • input-group font
  • forms font
  • reboot font

-------------- Sample SCSS --------------

Below is a snippet for your scss files; remember to define the $font-size-base BEFORE incorporating bootstrap into your main scss file. In this scenario, the app.scss file is being compiled.

app.scss

// Fonts
@import url("https://fonts.googleapis.com/css?family=Raleway:300,400,600");

// Variables -- where you specify the variables 
//              PRIOR to importing bootstrap, which utilizes its  
//              defaults if the variable is unspecified.
@import "variables";
// Bootstrap
@import '~bootstrap/scss/bootstrap';
// DataTables https://datatables.net/download/npm#DataTables-core
// @import "datatables";
@import "datatables.min";

_variables.scss

// Body
$body-bg: #ffffff;

// Typography
$font-size-base: 12px; // adjusts the font-size across bootstrap
$font-family-sans-serif: "Raleway", sans-serif;
$font-size-base: 0.9rem;
$line-height-base: 1.6;

Answer №2

Within Bootstrap's _reboot.scss file, the base font-size is defined as follows:

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;
    background-color: #fff;
}

REMINDER: It is advisable to avoid altering default values directly in framework files; instead, make modifications using custom CSS or JS files.

To adjust your font size to 14px, include this snippet in your own style.css (custom CSS file):

body {
    font-size: 0.875rem;
 }

Check out the provided fiddle for a demonstration.

If necessary, utilize !important to ensure your changes are prioritized and override any defaults.

In your custom.scss file, follow this structure:

$custom-variable: 0.875rem;

Then implement it like so:

@import "./src/scss/_modules/custom"; 
@import "./bower_components/bootstrap/scss/bootstrap";

body {
font-size: $custom-variable;
}

Answer №3

My recommendation would be to update the html {font-size: 16px} property directly instead of adjusting Bootstrap's $font-size-base, for a few reasons:

  1. The font size specified in html is used directly in determining the final font size. There is no advantage in using an "intermediary" like body that is influenced by Bootstrap's $font-size-base.

  2. Bootstrap's SCSS system calculates heading sizes based on $font-size-base, so any CSS rules affected by Bootstrap will also be impacted, including body (which is set to $font-size-base$).
    On the other hand, browsers calculate font sizes in rem relative to html {font-size}, not from elements affected by $font-size-base (like body).

  3. Specifying html {font-size} in units of pixels will impact any rem values, whether inside or outside of Bootstrap-affected elements, if that is your goal.

Alternatively, if you only want to adjust sizes for Bootstrap-affected elements, it is advisable to set the $font-size-base relative to html {font-size}, so CSS elements outside of Bootstrap are not affected. However, this scenario is more of an exception rather than the standard practice.

Note:
When setting $font-size-base or other Bootstrap variables, there is no need to edit the variables.scss file. Define the variables before importing

node_modules/bootstrap/scss/variables
and other Bootstrap files to ensure your values take precedence over the default values set in Bootstrap. This approach offers the advantage of avoiding extensive modifications to the entire variables.scss file.

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

Create a personalized card featuring a captivating backdrop image

Hey everyone, I’m a newbie in the world of web development and I recently put together something using bootstrap 5. If you want to check it out, here’s the link to my project on CodePen. I have a specific request though - instead of having icons displ ...

Only import Bootstrap into a single component

I have a React website that uses a local SCSS file compiled to CSS. Now, I want to incorporate Bootstrap into just one component on the site. After following the instructions at , I managed to get Bootstrap working within my project. However, the issue ar ...

What is the method to display all items in a Vuetify Data Table rather than limiting it to 10 rows?

I have implemented a data table from Vuetify in my project: <v-data-table :ref="`sortableTable${index}`" class="items-table-container" :headers="headers" :items="category.items" hide-default-footer> ...

Enhance Animation Fluidity with HTML/CSS

I am experimenting with creating a floating animation for my iceberg logo by adjusting the top margin. Below is the CSS code I have implemented: img { height: 60px; padding: 5px; -webkit-animation: logofloat 1s ease-in-out infinite alternate; -moz ...

the eventListener is not functioning as expected when used with the 'else' keyword

Everything seems to be working fine until I click on playsong1. The code executes as expected for the first click, but when clicked again, instead of progressing to the conditions after the } else { statement, it re-triggers the same conditions (replayin ...

I need help figuring out how to get a horizontal scrollbar positioned at the top of a div to function properly once the content has been loaded using ajax

HTML CODE <div id="scrollidout" style="overflow: auto; overflow-y: hidden; "> <div id="scrollidin" style="padding-top: 1px; height: 20px; width: 1000px">&nbsp;</div> </div> <div class="resultcontent" style="overflow ...

Using CSS to Identify When an Element is Being Grabbed, Dragged, or Moved

Is it possible to detect mouse grabs in addition to changing the cursor on hover? For example, try dragging the items in the following code snippet. The goal is to change the cursor to cursor: move if an element is being moved. <script src="https:// ...

Temporary code implemented for IE8 is causing the dropdown login field to lose focus

I have encountered an issue with my code in IE8. Whenever I move the mouse around in the dropdown login field, it loses focus and disappears about 70% of the time. After debugging, I discovered that this problem is related to a certain placeholder code: U ...

Adjusting the width of Bootstrap 4 form validation messages for improved layout

Having some issues with Bootstrap 4 form validation and jquery. When the validation message is displayed, it strangely affects the width of the entire form. Check out my HTML: <div class="container l-login-container"> <main class="l-mainConten ...

Designing with Bootstrap 4 involves incorporating a button that uses the data-toggle attribute set to "collapse" along with an href anchor linked to

I have attempted to link to an ID while also triggering a data-toggle collapse: <a href="#id" data-toggle="collapse" data-target="#collapseTwo" class="btn btn-primary" title="Something cool">Something very cool</a> for linking to this specif ...

Fluid background images frame the header on either side, with a void of background in the center

I am in need of a header that spans the entire width and is divided into 3 columns, with background images only in the 1st and 3rd columns. Cross-browser compatibility is crucial for this solution. The first column should have a background image and be ...

A guide on compiling Sass without using gulp and Laravel Elixir [SOLUTION]

If you encounter an error similar to this one: https://i.stack.imgur.com/ZqVeV.png You have come to the right place :) ...

If the picture is aligned to the side, shift the text below it

Within this section, I have included both an image and text. The image has been aligned to the left in order to have the text appear next to it. Both the image and text are enclosed within paragraph tags. Now, I am looking to add more text that will be di ...

The navigation elements in the Flexbox are behaving strangely as they overflow in an unexpected way

During my responsiveness testing, I came across a very strange issue. I have code that aligns the header as a flexbox, but when I resize the window to over 900px in width, the navbar moves out of the header and appears on top of the next element. Here&apo ...

Bootstrap 3 offset doesn't respond to dimensions

Even though I have set an offset for a medium-sized screen, the offset still appears in large screens. Here is a snippet of my HTML code. How can I resolve this issue? <div class="col-lg-6 col-sm-12"> <div class="row"> <div class="co ...

What is the method to set an element's height equivalent to the height of the entire screen?

I attempted using height: auto;, however, it did not produce the desired outcome. Do you have any suggestions or alternative solutions? ...

Angular: Creating a Sliding Menu with a Hamburger Icon

I have been attempting to implement a slide out menu from a span within my navigation bar, but I am encountering some challenges. Despite searching on Google, I have not been able to find the solution I need. Here is the code I am currently using: <div ...

Best approach for adding an image to an HTML help button: which semantic option is most effective?

Is there a way to create an HTML help button that appears as an image? What would be the most appropriate and semantic approach to achieve this? Below are three different solutions I have considered: 1st idea: Using HTML <button class='help&ap ...

`CSS3 animations utilizing keyframes and transformation`

Looking to replicate a similar effect using CSS animations: I've created a file working with keyframes for the animation, but it's not functioning as desired. My goal is to have the circles complete a full rotation on their axis, starting from ...

Ensure that columns are neatly arranged horizontally when they do not all fit in one row

I currently have a row with 6 columns, but when they can't fit next to each other on the screen, 2 of them get pushed below, resulting in a messy layout. I could use a media query to solve this issue, however, I am curious if there is a better way to ...