How can a variable be most easily divided in SASS?

Trying to perform a division operation on a SASS variable (which is a map value) by two, like this:

$grid-gutter-widths: (
  xs:  30px,
  sm:  30px
  ...
);

$col-padding-xs:  #{map-get($grid-gutter-widths, xs)/2}; // returns 15px

div {
  padding-right: $col-padding-xs / 2; // returns 15px/2
}

Expected the padding-right value to be 7.5px, but it doesn't perform the division and instead displays a string with a slash in the middle. Surprisingly, the following does work:

$col-padding-xs: 15px;

div {
  padding-right: $col-padding-xs / 2; // returns 7.5px
}

So it seems like the map value might be the wrong type. Is there an easy way to convert it to a number so that simple math operations can be performed in SASS?

Thank you for your assistance!

Answer №1

You're on the right track. The key is to remove the interpolation in the map-get function that you mentioned earlier. Check out the codepen for the compiled CSS code.

$grid-gutter-widths: (
  xs:  30px,
  sm:  30px
);

$col-padding-xs:  map-get($grid-gutter-widths, xs) / 2; // this will result in 15px

div {
  padding-right: $col-padding-xs / 2; // which will give you 7.5px
}

Answer №2

The latest update in SASS introduces the math.div function as a replacement for the division symbol (/). For example, $col-padding-xs: #{map-get(math.div($grid-gutter-widths, xs), 2)};

This new function takes two numbers and divides them accordingly.

Reference: https://sass-lang.com/documentation/breaking-changes/slash-div

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

Trouble with switching the <a> tag when utilizing ng-hide/show and a boolean

There is an up arrow that should be hidden using ng-hide if this.showUpArrow is false, and shown using ng-show if this.showUpArrow is true. Initially, this.showUpArrow is set to false when the page loads, and it only switches to true when the top of the pa ...

Recording the mouse click action following the turning over of the tile

Greetings! I have successfully implemented a zoom and flip feature on my tile. My tile now showcases two different views - the initial view displays an image, while the back view shows text. However, I am encountering an issue where multiple alert boxes ...

What is a better method for aligning an image in the middle of a floated container?

Is there a better way to center an image within a floated div without using an extra p tag? I currently have the image inside a p tag and center the p, but it annoys me having that extra tag. Any suggestions on how to center the image itself? Thanks! Below ...

Designing the style of a React datepicker

Currently on my webpage, there is a React datepicker component (https://reactdatepicker.com/). Ideally, when I click on the date, I would like the popper to function as an element within the page rather than a separate window. This is how I envision it: ...

Extracting information within a statement using regular expressions

For instance, let's consider the HTML code below: <p ><strong>PM Narendra Modi visits BJP headquarters</strong>[youtube-video id='XHDUCalVmis' width='640' height='360' ]</p><hr /> In this s ...

Display or conceal various content within div elements using multiple buttons

I have a set of 5 image buttons, each meant to trigger different content within a div. When the page loads, there should be default content displayed in the div that gets replaced by the content of the button clicked. The previously displayed content shoul ...

Styling Borders in CSS for Spaced Table Rows

I need to create a table with rounded borders between each row while maintaining spaces. Initially, I inserted an extra row <tr class='spacer'> in between each row for spacing purposes. However, after implementing sorting functionality usin ...

React does not maintain consistent scaling ratios for images when they are resized

I attempted to create a React page that is compatible with both mobile and web views. I included icons for non-veg and veg symbols in a table for rendering data. However, when viewed on screens of different sizes, the non-veg icon appears larger than the v ...

Create a stunning design with Bootstrap 4 by incorporating a full-width background for both the table header and rows,

Bootstrap 4 offers a convenient table feature, but I'm having trouble aligning it with the design provided to me. The design requires full-width background colors for both the dark header (thead) and the light body (tbody), all while keeping the eleme ...

How to customize a dropdown menu with the size property?

Can anyone help me with styling a select box that uses the size attribute? Most tutorials only cover single-item select boxes. Has anyone dealt with styling select boxes with the size attribute before? Here's an example of what I'm trying to acc ...

Column-oriented and Slim-fit packaging

I have been working on designing a layout with a fixed height that will display multiple columns of specified size to accommodate flowing text. While I have successfully implemented this, I am facing an issue where the enclosing div does not adjust its siz ...

Show only the lower left quadrant within the img tag during the prepend operation

I'm attempting to add an <img> tag in front of a <div> similar to this example on JSFiddle. However, I have a specific requirement to only display the bottom left quarter of the image instead of the entire one. HTML Markup <div id="my ...

Tips for incorporating captions into a slideshow

I've been experimenting with different methods to include captions in my slideshow, but haven't had much success. I simply want to add a div at the bottom of each image. Do I need to modify my script or should I consider adding something else? H ...

Broken href link on background image with absolute positioning

Having trouble adding a link over a background image of the head-bg-image div into ul li. The link is not functioning properly. <div id="product_banner"> <div class="head-bg"> <div class="head-bg-img"></div> <ul class="d-inline ...

Exploring the Impact of Unicode Characters on CSS Styling

Being a native Bengali writer, we rely on Unicode Bengali characters. Unicode, an extension of ASCII, includes all ASCII characters and additional glyphs from around the world for languages like Chinese, Hebrew, Japanese, and Javanese. However, when it co ...

Get rid of the drop-down shadow effect

Currently working on a website project for a client and looking to remove the blue "shadow" effect from the dropdown menus. I believe the code that needs editing is as shown below: .main_nav ul.sub-menu { position: absolute; top: 65px; width: ...

Utilize jQuery to dynamically add a CSS class to a button

When using jQuery to create dynamic buttons, is there a way to add a CSS class to the button during creation without needing an extra line of JavaScript to add the class afterwards? Below is a example code snippet: $.each(data, function (index, value) { ...

Issue with Bootstrap hover and toggle collapse menu functionality in a Ruby on Rails application

I am currently following this tutorial and have reached this particular part. However, I'm facing an issue where the mobile hamburger menu doesn't display the menu when clicked on mobile devices. Additionally, the hover effect is not working for ...

Using jQuery to change the background color of table cells to various shades

I am seeking assistance with a table I have created, and I want to implement a feature where the background color of cells with prices will change when clicked (using addClass(on)). The goal is to also change the background color of surrounding cells (usin ...

Issue with Inline forms in Ruby on Rails/HTML: The 'checked' attribute with the value of 'false' is

Desired Setting: I am trying to set the default option for this form to be "No" (or false), however, when I set it as such, my application loads with no option selected. When I use :check => true, the default option selected becomes "Yes" (or true). I ...