Exploring the benefits of incorporating margin and padding variables in custom class Bootstrap 4 with Sass integration

Is there a way to make spacer variables more generic while using Bootstrap in a custom class?

I have a class called .box and I would like to apply the pl-3 class from Bootstrap 4 without adding it directly in the HTML, but rather styling it in sass.

I aim to utilize the spacer variables of Bootstap and avoid hardcoding them in the class itself. This will allow for easy adjustments to padding in the future by only modifying the sass file.

The following variables are defined:

$spacer: 1rem !default;    
$spacers: (
      0: 0,
      1: ($spacer * .125),      
      2: ($spacer * .25),       
      3: ($spacer * .375),     
      4: ($spacer * .5),        
      5: ($spacer * 0.75),    
      6: ($spacer * 1)          
    )

What is the most effective approach for achieving this? One method I've come across is:

.box{
   @extend .pl-3;
}

However, I find this method lacking in clarity. Are there alternative or better ways to accomplish this? For example:

.box{
       padding-left: $spacer-3;
    }

Answer №1

To effectively utilize the SCSS function map-get(), refer to the documentation provided here.

.container {
  margin-top: map-get($spacing, 3);
}

View the demonstration on Codepen

Answer №2

To simplify this process, consider using a basic function. Remember that Sass lists start at index 1, so we add $n +1 when accessing the value.

@function space($n){ 
  @return nth(0 .125 .25 .375 .5 .75 1, $n + 1) * 1rem;
}


.box{
   padding-left: space(3);  // 0.375rem;
}

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

What is the best way to show emojis in AngularJS?

Recently starting to work with angularjs, I've incorporated "emoji-min.js" and "emoji-min.css" into my project as an attempt to display emojis within a text field. Despite my efforts, the emojis are not displaying as intended. Here is a snippet of my ...

Error 404 encountered while attempting to view HTML Page on FLASK server - unable to display content

I am facing an issue with my Flask project. I can browse to my main page, index.html, but when I click a button on the index page that directs me to mytopo.html, the page loads without showing the desired topology view and I encounter an error. The error m ...

Once more, the gap between the td's is evident

After closing the last topic because I thought it was completely answered, it turns out that's not the case. Link: Space between two td or tr tags The line-height: 0; isn't very helpful in this situation. In the original work I'm doing with ...

Is there a different method like Calc() to create a static sidebar alongside content?

Is there a way to achieve a fixed sidebar on the left and a content area on the right without using calc() in CSS? I'm looking for a more universally supported method that works across different browsers. .left-sidebar { width: 160px; ...

Enhance the efficiency of AngularJS search filtering

With nearly 2000 rows on this page, I am using AngularJS filter to search for items containing the typed strings. However, I have noticed that the efficiency is poor when typing just one character into the input control. Does anyone have suggestions for im ...

Transform a dropdown menu into an inverted footer

I stumbled upon a unique dropdown menu design that I want to tweak and reverse, making it an innovative "dropup" menu that opens from the bottom to the top. Currently, this is what I have: HTML <div class="get-started"> <a href="#" id="get-s ...

How can I create numerous HTML containers using Javascript?

I've been learning from this tutorial: Instead of just displaying the last database object, I want to display all of them. I have tried outputting the database contents and it's working fine. Now, I just need to adjust the HTML. I attempted to ...

Mobile display exhibiting glitches in animation performance

I have implemented an animation in the provided code snippet. const logo = document.querySelector('.logo'); const buttons = document.querySelectorAll('.loadclass'); const html = document.querySelector('html') const cornerme ...

Minimizing Unused Space After Media Query Adjustment

Using the bootstrap grid system, I am facing some uncertainties. I would like to create a header where there is a logo on the left and links on the right. Below this header, I want a menu that spans the same width as the content above. In my current setu ...

Refresh the data in a table upon clicking the menu

I am looking to develop an accordion MENU, but unsure if I should use divs or <ul>. When a submenu is clicked, I want to execute a mysql query to update the data in the table next to the menu. I'm not sure of the best approach and whether to uti ...

The icon for the "Hamburger" menu is missing, but the text is visible

Currently, I am designing the topbar for my website using Foundation. Everything seems to be functioning correctly except for the menu icon not appearing. I have followed the documentation provided by Foundation, but I am still facing difficulties. Here is ...

What is the best way to make a multi-line group using Bootstrap?

I'm a beginner exploring Bootstrap and attempting to design an HTML form using Bootstrap input-groups with captions in the input-group-prepend. I am looking to create a setup where a single caption is displayed before multiple styled lines inside the ...

Issue with loading scripts in CodeIgniter, jQuery Mobile, and jQuery UI - scripts are not loading initially, requires a refresh to

I am currently facing issues with my codeigniter application. The problem arises when I have a view/form (view 1) that, upon submission, loads another view (view 2). When view 1 is initially loaded, it does not load the correct JavaScript and CSS. Instead ...

Style the image within the table by setting a specific width

My table contains a mix of images and text, with the images set to a specific width using the code snippet below: <img src="" width="250"> When certain large images are dynamically added to the table during runtime using jQuery, the table's wi ...

Changes on services do not affect the Angular component

Currently facing an issue with my Angular assignment where changing an element's value doesn't reflect in the browser, even though the change is logged in the console. The task involves toggling the status of a member from active to inactive and ...

Eliminate jQuery's delayed blinking effect with the use of an event

Utilizing the mouseenter and mouseleave events, I have implemented a functionality to add a button (not actually a button) to an <li>. However, there seems to be a problem with my code. The button appears and disappears on mouseleave and mouseenter, ...

Automate the login process for a website and programmatically fill out and submit a form

I am currently trying to automate the login process on Skype's website using Selenium in C#. Everything goes smoothly until I reach the password field, which seems to be elusive to Selenium. I have tried various methods to locate the password field bu ...

What is the best way to transfer a JavaScript variable through a query string from one HTML page to another?

Is there a way to pass a JavaScript variable called username from one HTML page, example1.html, to another HTML page, example2.html, using query strings? <script type="text/javascript" > $(document).ready(function() { $('#SubmitForm ...

When the browser back button is clicked, conceal the current div and reveal the previously hidden div

I'm faced with a situation where my website consists of multiple pages which I've achieved by displaying and hiding divs within a single html file. The issue I'm encountering is that the browser's back and forward buttons aren't fu ...

Each time I use console.log to display a user's radio button choice, the output is consistently lagging by one step

It seems that I am experiencing an issue where the console.log output for a user's radio button selection is always one step behind. This occurs even though I have initially set the default radio button selection to "all". For example, when a user cli ...