Looking to incorporate two @return statements in a Sass function

I have a very straightforward Sass function that converts a pixel value to a rem value:

@function to-rem($pxval) {
    @return #{$pxval / $base-font-size}rem;
}

For example, using to-rem(24) would output 1.5rem.

However, I recently ran into an issue where older versions of IE do not support rem units. To overcome this, I added a flag for old IE and adjusted the function accordingly:

@function to-rem($pxval) {
    @if $old-ie {
        @return #{$pxval}px;
    } @else {
        @return #{$pxval / $base-font-size}rem;
    }
}

Now, I need to consider Opera Mini as well, which also does not support rem units. Unfortunately, I don't have a filter specifically for Opera Mini like I do for old IE. Any suggestions on how I can handle this without resorting to a mixin?

While I do have a mixin that achieves the same result, I prefer using the function for single values as it's more concise. For instance, using the function for padding:

padding: to-rem($spacing-base) 0;

is simpler than using the mixin:

@include to-rem(padding, $spacing-base 0);

Answer №1

If you're wondering how to get two values returned in a programming language, the answer lies in using a mixin. You can achieve this by returning a list or array.

@function calculate-values($value) {
    @return $value * 1px, $value / $base-font-size * 1rem;
}

.example {
    $results: calculate-values(36);
    padding-left: nth($results, 1);
    padding-right: nth($results, 2);
}

In this case, the mixin is clearly the superior choice.

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

Adjust the source of the HTML5 video tag based on the orientation of an iPad

I am currently coding an HTML5 page specifically for iPad Mobile Safari, which includes a video tag for embedding video files. I have successfully implemented CSS3 media queries to determine the orientation of the iPad. My next challenge is to dynamically ...

How to Style a Diamond Shape with Content Inside Using CSS

Can someone help me figure out how to create a diamond-shaped background in HTML? I want the diamond shape to contain text and images, similar to what's shown in this screenshot. The example image uses a background image of a diamond, but I'm loo ...

Blurred background images following the upgrade to Internet Explorer 11

This morning, I received an automatic update to IE 11 and noticed that some of my background images appeared blurry. After confirming that the images were not the issue, I opened Chrome and saw that they were crisp again. This left me completely puzzled. ...

Twice the clicking actions triggered by the event

Whenever I try to trigger a function by clicking on the label text, the click event seems to be firing twice. HTML <label class="label_one"> Label One <input type="checkbox"/> </label> However, if I modify the HTML code as f ...

What could be causing the appearance of lines above and below my SVG element in the html?

I'm using an SVG to create a curved look on my web page. The SVGs are already in place and look great on desktop screens, but when viewed on mobile, I see small lines appearing above and below the SVG that I want to remove. Here are some pictures of ...

How can I dynamically alter the width of a Bootstrap progress bar using the {{$percentage}} variable in a Laravel stylesheet?

I am currently working on a project where I aim to display the percentage of marks obtained using a progress bar. I have the percentage value and I am trying to pass this value in the style: "width:{{$percentage}}" so that the progress bar width changes a ...

Article that fills the entire screen

I am currently in the process of developing a mobile application. The issue I am facing is that the text is not aligning properly and the images are not displaying fullscreen. Additionally, I want the images to adjust automatically based on whether you are ...

How can I remove the excess space above and below tables that have differing numbers of rows?

Forgive me if my question seems basic or if there are any errors in it. I am new to HTML/CSS and collaboration tools like this platform. While I understand that these are not your typical HTML tables, they are what I've found to be the most effective ...

Creating responsive images in Bootstrap columns

I've found a solution to my issue with large images, but I am still struggling with smaller images not fitting well into larger spaces. Essentially, I created a CMS for a user who required templates with various bootstrap columns as content areas. Cu ...

The jQuery slider comes to a gradual halt

I can't figure out why my jQuery slider doesn't stop right away after a mouseover event. There seems to be a delay that I can't resolve on my own. Can someone lend a hand with this problem? Here is the code I'm working with: https://js ...

How can you align an icon to the right in a Bootstrap4 navbar while keeping it separate from the toggle menu?

Looking to utilize the Twitter Bootstrap framework for structuring my navbar with distinct "left", "middle", and "right" sections, where the middle portion collapses beneath the navbar-toggler (burger menu) when space is limited. For a self-contained exam ...

Bootstrap columns stacking in a disorganized manner

I'm struggling to correctly stack my columns using bootstrap. The image shows that I need the black box to be positioned below the green box, but I can't seem to get it right: https://i.sstatic.net/7bvN6.png This is the code I have been using: ...

React-Tooltip trimming

Currently, I am facing a dilemma that requires some resolution. The issue at hand is related to the placement of React-Tooltip within the List element. Whenever it's positioned there, it gets clipped. On the other hand, placing it at the bottom isn&a ...

Android failing to adapt to font size changes set by media queries in HTML code

Recently, I've been facing an unusual issue on a Nexus 7 where changing the font-size on the HTML element seems to be ignored. Removing the smaller font size resolves the problem but creates new issues for phone displays. CSS: html { font-size: 10px ...

Issues with zoom functionality not functioning properly within IE11

I am currently developing an application with Angular that is designed to be compatible with tablets and touch-enabled devices. One of the key features I want to implement is the ability for users to zoom/scale up the app, especially for those with visual ...

What is the best way to implement the :focus pseudo-class on various identical selectors simultaneously

I am attempting to modify the color of the bottom border when input type number is focused. I have partially succeeded, but the CSS is not being applied to all selectors. Here is the issue: https://i.sstatic.net/OOBVj.png The left side (peso) is not in f ...

What is the best way to maintain two distinct background effects that each require different pixel sizes?

I am attempting to create a gradient that goes from the top-right corner to the bottom-left corner, and I also want to add a grid of dots. However, the method I am using to create the grid of dots is through a radial gradient which requires a small backgro ...

The Issue of Horizontal Scrolling - None of the elements are out of

I am facing an issue with an unwanted horizontal scroll on my website and I can't seem to identify the cause. The header of my site is quite simple, utilizing display: flex, justify-content: center, marin-top: 5vh, along with some padding on the logo ...

Collapsed ReactJS: Techniques for compressing the Material UI TreeView following expansion

My issue involves a Material UI treeView. After expanding it, an API call is made, but the node in the treeView remains open. I have attempted to use onNodeToggle without success. Below is my code: <TreeView className={classes.root1} defaultExpandI ...

Trigger the phone to vibrate once the animation has completed. This feature will only activate upon clicking the button

After the image completes its animation by sliding in from the left, I would like it to vibrate for 2 seconds using the HTML 5 vibrate API: navigator.vibrate(2000); An on-click event successfully triggers the vibration when a button is clicked: <butt ...