Struggling with the complexity of SASS gradients mixin

After spending over two hours chasing my tail, I'm still struggling to create a cross-browser gradient @mixin.

My desired result is:

-moz-linear-gradient(left, #fff 0%, #000 100%);

Using the default arguments in:

@mixin gradient($type:linear, $gradient:(#fff 0%, #000 100%), $shape:vertical, $repeat:false){

  background:nth(nth($gradient, 1), 1);

  $background:null;

  $vendors:null;

  @if $shape == vertical {
    $vendors: (
            mozilla: (-moz-, top),
            webkit: (-webkit-, top),
            opera: (-o-, top),
            standard: ("", to bottom)
    );
  }@else if $shape == horizontal {
    $vendors: (
            mozilla: (-moz-, left),
            webkit: (-webkit-, right),
            opera: (-o-, right),
            standard: ("", to right)
    );
  }@else{
    $vendors: (
            mozilla: (-moz-, $shape),
            webkit: (-webkit-, $shape),
            opera: (-o-, $shape),
            standard: ("", to $shape)
    );
  }


  @if $repeat == true{
    $background:repeating-+$type+-gradient;
  }@else if $repeat == false {
    $background:$type+-gradient;
  }

  @if $type == linear {
    @each $vendor in $vendors {
      background:[?????];
    }
  }@elseif $type == radial {
    @each $vendor in $vendors {
      background:[?????];
    }
  }

}

I'm in desperate need of assistance before I resort to smashing my laptop against my head!

Answer №1

After taking a break over the weekend to let my thoughts settle, I came to the realization that I was overcomplicating the maps for the code. I decided to switch the nested $vendors maps to simple lists, streamlined the conditionals, and introduced a $fallback variable for easier supply as an argument instead of adding another conditional:

@mixin gradient($gradient:(#fff 0%, #000 100%), $type:linear, $shape:vertical, $repeat:false, $fallback:#fff){
  // Setting the background color as the fallback value
  background:$fallback;
  // Initializing $vendors
  $vendors:null;
  // Determining shape or direction
  @if $shape == horizontal {
    // Vendor-specific horizontal directions
    $vendors: (
            (-moz-, left),
            (-webkit-, right),
            (-o-, right),
            ("", to right)
    );
  }@elseif $shape != vertical{
    // Declaring diagonal linear gradient angle or radial gradient shape
    $vendors: (
            (-moz-, $shape),
            (-webkit-, $shape),
            (-o-, $shape),
            ("", $shape)
    );
  }@else{
    // Default vertical linear gradient
    $vendors: (
            (-moz-, top),
            (-webkit-, top),
            (-o-, top),
            ("", to bottom)
    );
  }
  // Checking for a repeating gradient
  @if $repeat == true{
    $type:repeating-+$type+-gradient; // Yes
  }@else {
    $type:$type+-gradient; // No
  }
  // Outputting vendor-prefixed gradients
  @if str-index($type, linear){
    @each $vendor in $vendors {
      background:unquote(nth($vendor, 1)) + $type + unquote("(" + $gradient + ")");
    }
  }@else {
    @each $vendor in $vendors {
      background:unquote(nth($vendor, 1)) + $type + unquote("(" + $shape + ", " + $gradient + ")");
    }
  }
}

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

I exclusively use CSS for my tooltips, no images involved

I created some CSS code for a tooltip element .toolTip{ display:inline; position:relative; } .toolTip:hover:after{ background: #333; background: rgba(0,0,0,.8); border-radius: 5px; bottom: 26px; color: #fff; content: attr( ...

Trouble with saving the positioning after drag and drop

I am currently facing an issue with my drag-and-drop script where the coordinates of positioned relative divs are not being saved in the same position when I reload. These divs are contained within a container with specific height and width, restricting t ...

Experience the ultimate in slider automation with the cutting-edge power of

Here is an example of a website plugin I used on my site. You can check it out by clicking here. However, I'm having trouble automating the events that occur when the item slider is clicked. I would like to make these events happen automatically with ...

Font malfunctioning on Microsoft Edge and Internet Explorer

Apologies if this seems like a silly question, but I recently imported a font from my computer into a CSS file. While it displays correctly in Chrome, it's not working in Microsoft Edge or Internet Explorer. Unfortunately, I don't have access to ...

Placing an eye icon on the password input field for optimal visibility and positioning

I am currently working on a Node.js project and I am attempting to incorporate an eye icon using Bootstrap into the password input field for visibility. Below is the code snippet of my input field: <div class="container"> <form ...

Using various span elements with distinct alignment within an h1 tag in Bootstrap 5

How can I nest three span tags inside an h1 tag, aligning them differently? The first on the left, the second in the center, and the third on the right. Can this be achieved using Bootstrap or does it require custom CSS? ...

Creating a clickable image overlay card in Bootstrap 4 - A simple tutorial

I'm trying to use the Bootstrap 4 card element to create a grid of images with overlaid headings, but I'm having trouble linking the image itself. I attempted the stretched link method without success. I also tried placing the entire card in an ...

Using jQuery to apply a CSS border to an image within a textarea

Working with TinyIMCE, I am looking to incorporate a border around all images within the textarea whenever a user submits the form. $("form").submit(function() { var content = $("#edit-message1-value").val(); // Afterwards, I want to manipulate the cont ...

Applying CSS borders with circular corners

Can this unique border style be achieved using only CSS? https://i.sstatic.net/wMtbG.png ...

How to center text in a DIV using CSS Bootstrap?

I'm a beginner in front-end development and I'm struggling to center the text below the image within this div. I want the text centered just below the image. I'm using Bootstrap for this project, and here's the code snippet I'm wor ...

Whenever the user hits the "Enter" key, a new element will be generated and added to the bottom of an existing element

I am in need of creating a new element at the end of another element. For instance, let's say I have this element: <div id="elmtobetrig">Element that should be followed by another element when the user hits enter after this text. <! ...

Representation of a family tree in CSS showcasing multiple sub-parents (both many to one and one to many relationships)

I've come across various family tree presentations. I'm curious if it's possible to display a one-to-many and then many-to-one structure? Currently, the flow is linear stop after stop, but I'd like to incorporate multiple steps that con ...

Menu with a carousel feature in between each item

I am experiencing an issue with a carousel embedded within a menu using Twitter Bootstrap. <nav class="navbar navbar-default"> <div class="container-fluid"> <div class="carousel slide" id="CarouselTest"> <div class="carousel-i ...

Issue with toggling in react js on mobile devices

Currently, I am working on making my design responsive. My approach involves displaying a basket when the div style is set to "block", and hiding it when the user browses on a mobile device by setting the display to "none". The user can then click on a but ...

Manipulating text alignment and positioning in CSS

Can someone help me achieve this CSS result? img1 This is what I have so far: img2 I'm struggling to center the elements and add a line in CSS. Any advice? I thought about using margin: auto, but I'm not sure if that's the best approach ...

Is there a way to make the <iframe> adjust its size as the element it contains grows, without

Seeking assistance with a specific issue. My goal is to have an iframe appear on hover of another element and expand with it. The problem I'm facing is that the iframe shows up quickly and extends beyond its parent container. Here's what I have: ...

CSS: The calc() function does not take into account the bottom padding when used in Firefox and Internet

I am encountering an issue with the calc() function in Firefox 32 and IE 11, where the bottom padding of a div is not being respected. However, this problem does not occur in Chrome and Opera. The main content section should have a fixed height while allo ...

Utilize CSS Steps to incorporate the initial position

I'm experimenting with CSS steps to create a unique visual effect resembling a "light board". The setup involves a column of 18 divs acting as individual "bulbs", with an animation that positions a div behind each one to simulate a "lit up bulb." The ...

Transforming the initial character of a label element into uppercase

When I receive an external HTML page, all the data comes in lowercase. I attempted to capitalize the first letter of each label tag using CSS, but it ended up making the entire text uppercase instead. Here is what I tried: .fontmodal { text-transform ...

Encountering a node-sass problem during npm installation in my Angular project on Mac OS

While attempting to install node_modules for my Angular project on Mac OS, I encountered an issue with node-sass. My Node.js version is v16.13.2 and the node-sass version in the project is ^4.14.1. The package.json files can be viewed in image1 and image2. ...