Customize the font size in Bootstrap 5 using REM units

When it comes to setting font sizes for easier calculations in REM values, I currently use this approach:

html { font-size: 62.5%; } 
body { font-size: 1.6rem; } /* 16px */

However, Bootstrap 5 uses the root value (which is typically set at 62.5%) to calculate all sizes. It provides variables like $font-size-root and $font-size-base, and I've tried manipulating those to achieve accurate calculations.

Initially, I attempted setting the root to match HTML value and the base to align with body value, but that resulted in incorrect outcomes. Adjusting either the base or the root to 1.6rem or 1rem also produced wrong results.

I'm curious if there's a way to customize Bootstrap 5 so that when I specify a value like 3.2rem for $h1-font-size, it will output a result equivalent to 32px (computed style).

This would streamline my calculations significantly, ensuring Bootstrap adheres to the same standards as the rest of the CSS.

Thank you in advance for any suggestions! I've conducted some searches here, but unfortunately, didn't find relevant questions.

Answer №1

rem units are calculated based on the font size of the html element, with a default size of 16px.

To achieve this effect, simply set the font-size: 10px for the html element. For example:

html { font-size: 10px; }
body { font-size: 1.6rem; } /* This will be equivalent to 16px */

In Bootstrap 5, making this change requires adjusting the SCSS/CSS3 variable for the body font size. Here is an example of how you can do it using CSS:

html { font-size: 10px; }

:root {
  --bs-body-font-size: 1.6rem;
  font-size: var(--bs-body-font-size);
}
<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d4f4242595e595f4c5d6d18031f031d">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>
<html>
  <body>
    <p>This text is now styled as 1.6rem or 16px.</p>
  </body>
</html>

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

Jekyll adjusting the starting line of code snippet formatting

Currently, I'm in the process of setting up code highlighting for my blog using Jekyll and Pygments. However, I'm facing an issue where the first line of every code snippet seems to be slightly offset. To create a gutter effect, I'm utilizin ...

Using JQuery to trigger CSS3 animations on one element when clicking on another

My Rails app has a feature similar to reddit where users can upvote or downvote content. I'm currently working on creating a CSS3 animation that triggers when a user clicks the downvote button. Here's how I'm using JQuery: <div class="a ...

Is it possible to apply a margin to <details><summary> elements?

I am struggling with adding a margin to each of the children in my nested <details> elements .container { margin: 20px; } .parent, .child { outline: none; border: none; user-select: none; cursor: pointer; } <div class="container"> ...

Why isn't it working? Looking for a script that adds a class when the element is empty?

I have a script that should add the class "leftmax" to the li element with id "spect" when the div element with id "tab2" is empty, but it doesn't seem to be working. Can someone provide some assistance? <script type="text/javascript"> $(funct ...

How to ensure the footer stays at the bottom of the page even when the Treeview control expands

Greetings Everyone! I'm facing an issue with keeping the footer pinned at the bottom of the page. The treeview control I'm using overlaps the footer when expanded. Can anyone assist in ensuring the footer remains at the bottom of the page? < ...

Guide to adding a CSS class to an Ionic2 toast

i found a helpful resource on applying cssClass to my toast component. within my HTML, I have buttons: <button ion-button (click)="presentToast()"> toast</button> Here is the code snippet from my .ts file: presentToast() { let toast = t ...

The switch switches on yet another switch

Greetings everyone, Currently, I am in the midst of my exam project and creating a mobile menu. The functionality is there, but unfortunately, when closing the menu, it also triggers the search toggle which displays an unwanted div, becoming quite botherso ...

`The height attribute of the textarea element is not being accurately adjusted`

When I tried to match the width(178px) and height(178px) of my table to the width and height of a text area upon clicking a button, I encountered an issue. While setting the width works perfectly fine, the height is being set to only 17px. It seems like ...

How can text be concealed within an input field?

My degrees converter code is functioning well, but I have an issue. When I input a number for one temperature type, the conversion for the other two types is displayed correctly. However, if I then enter a number for another temperature type, the previous ...

Is there a way to completely define CSS animation using only JavaScript?

I am looking to implement CSS animation on HTML elements that are generated entirely through a JavaScript function. The approach I am taking involves customizing settings and functions for each part of the animation, which is why this method is necessary. ...

Div Boxes at the Center

I've searched extensively for a solution to my unique problem, but haven't been able to find one that fits. I have 4 div boxes with a max width of 50% and a min width of 400px. When the site is viewed on a smaller screen, I want the boxes to alig ...

What's the best way to update the menu element colors as I scroll down the page?

I am looking to update the color scheme of menu elements as the page scrolls using the provided template. I tried a few methods, but they lack dynamism. How can I make it more dynamic? $(document).ready(function(){ //scroll animation $('.navigati ...

How can I code a div to automatically scroll to the top of the page?

I'm attempting to implement something similar in WordPress - I want a div containing an advertisement to initially start 300px down the page, then scroll up with the page until it reaches the top, and finally stay there if the user continues to scroll ...

The background-clip property in CSS3 is failing to apply to borders with a transparent

I am currently learning how to create transparent borders by following the tutorial on CSS-Tricks website. Unfortunately, my code is not producing the desired transparent border effect. I have double-checked my code and everything seems correct to me. For ...

The Heroku application is rendering with different CSS and positioning compared to the local development environment

My locally hosted site has correct CSS and positioning, but once deployed to Heroku, it appears to lose some of its CSS styles. I am using the MERN Stack for this project. I suspect that the issue may be related to my node_modules, even though I have them ...

How can I address the problem of adjusting the title to match the size of the image in question?

I need help creating a design where an image and h2 title appear on top of the image... Below is a reference screenshot: I am looking to achieve something similar. CSS: h2 { position: relative; letter-spacing: 1px; display: inline-block; positi ...

Bootstrap 5 NavBar Toggle hamburger Menu not displaying menu items

I attempted to create a navigation bar using the navbar instructions in Bootstrap 5. I included a Toggle hamburger Menu and linked it to the list that should be displayed. However, when I decrease the screen size, the Toggle feature does not function as ex ...

Enhancing jQuery Mobile listview with voting buttons on each item row

I am looking to incorporate 2 vote buttons within a jQuery mobile listview, positioned on the left-hand side and centered within each list item. While I have managed to achieve this using javascript, my goal is to accomplish it without any additional scrip ...

Having trouble with CSS transitions not showing up correctly in Chrome. Any suggestions on how to resolve this issue?

While working on a static website, I attempted to apply a background-color transition to a button. Despite reviewing my code multiple times, I discovered that the issue was not with my coding. It seems like the browser is having trouble displaying CSS tran ...

Eliminate the horizontal scrollbar generated by a certain div

I'm working on the following HTML structure: <div id="container"> <div id="content"></div> </div> The container div is set at a width of 1050px and has a shadow background that repeats vertically. The content div is 950px ...