Transitioning from LESS to SASS involves adapting responsive breakpoints

Adapting from Less to Sass has been my current focus.

I have defined responsive breakpoints in Less variables:

/* Breakpoints */
@break1:~"(max-width: 570px)";
@break2:~"(min-width: 571px) and (max-width: 1002px)";
@break3:~"(min-width: 1003px)";

These breakpoints are being used by various components in the following way:

    /*component*/
    .main-header{
        /*base styles - all devices*/

        /*responsive breakpoints for component*/
        @media @break1{
            /*styles for mobile only (from 0px up to 570px)*/
        }

        @media @break2{ 
                /*styles for tablet only (from 571px up to 1002px)*/
        }
        @media @break3{
            /*styles for desktop only (from 1003px up to infinite)*/
        }
        @media @break2, @break3{
            /*styles for tablet & desktop (from 571px up to infinite)*/
        }
    }

I've noticed that there are alternative methods in Sass for handling responsive design breakpoints, but none seem to allow using two breakpoints together like in the Less example above.

        @media @break2, @break3{
            /*tablet & desktop styles (from 571px up to infinite)*/
        }

I am looking for a solution that enables the use of multiple breakpoints together without introducing new ones.

VIEW DEMO

Answer №1

replace comma with and- @media $break2 and $break3 {}

it's not allowed to store strings in variables; you must either create a mixin or update your code as shown below:

/* Breakpoints */
$break1: 570px;
$break2: 571px;
$break3: 1003px;

/*CODE EXAMPLE - for component header + include responsive*/

        .main-header{
            /*base styles - all devices*/
            height:20px;            

            /*responsive breakpoints for component*/
          @media only screen and (max-width : $break2) and (min-width: $break3) {

            }
        }

this will result in

@media (min-width: 571px) and (max-width: 1002px) and (min-width: 1003px)

a better approach would be to structure your CSS more appropriately: avoid using breakpoints like in the example, but rather override them. Start by styling for all viewports (generally for desktop), then move on to tablet viewports with @media (max-width : 1024px) to make changes specific to tablets, and finally adjust for mobile viewports by overriding desktop and tablet sizes. This way, you need less code because some properties are already inherited from desktop styling, and you only need to override what needs to change.

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

Tips on adding conditional styles in Next.js

This is my attempt: function ChangeStrokeToTextAndImage(properties: ChangeStrokeToTextAndImageProps) { return ( <div className={ styles.container0 + " " + properties.isLightGreenBackground == true ? styles.isLight ...

Is it possible to animate multiple SVGs on a webpage with just the click of a button?

Is there a way to trigger the animation in the SVG each time a next/prev button is clicked while navigating through a carousel where the same SVG is repeated multiple times? The carousel is built using PHP and a while loop. jQuery(document).ready(function ...

Steps to conceal a <select> element and reveal it by clicking a button

I'm currently working with Bootstrap 3 and I am attempting to replicate jQueryMobile's select option feature. This would involve replacing the standard select textbar and arrows with a single button that can toggle the opening of the select. Ins ...

Replacing text in the main navigation bar with sIFR

Could this menu layout be improved? <div class="navigation"> <ul id="nav-menu"> <li class="active"> <div class="sifr-r active"><a href="#" title="home" class="top-link"><span class="blue-small">01.</span& ...

Transform Table header orientation to vertical

I am struggling with a table structure similar to this <table> <thead> <tr> <th>Numbers</th> <th>Alphabet</th> </tr> </thead> <tbody> & ...

The code seems to be malfunctioning in a separate JS file, but oddly enough, it functions properly when placed within a <script> tag

I am trying to create a loader, but I have encountered an issue where the script works when placed directly in the HTML file, but not when it is in a separate JavaScript file. Here is the script: var loader = document.getElementById("ld"); w ...

What is the best way to adjust the size of a toggle button

My HTML page is equipped with a toggle button that has its own CSS properties, but I am facing an issue where the size and length of the toggle button are not aligning with the initial position set. Upon toggling the button, it tends to shift with a diffe ...

Error 404: Django failing to load image on the webpage

Just starting out with Django and I've hit a snag trying to load a static file (image). Here's the error message I'm getting: 127.0.0.1/:1707 GET http://127.0.0.1:8000/static/css/static/images/background/1.jpg 404 (Not Found) I suspect th ...

Bring the element to the top of the page by clicking on the anchor within the element or anywhere within the specified div ID

I am looking to implement a functionality where the page scrolls to the top of the navigation div ID when a link inside the navigation div is clicked, or ideally even when clicking anywhere within the div itself that contains the navigation links. After r ...

Replace the image with text inside an anchor when the anchor is being hovered

I want a logo (we'll call it .item-logo) to be shown inside a circle when not being hovered over, but when you hover over the container, the date should be displayed. Here is the HTML code: <div id="main-content" class="container animated"> ...

Reposition the jQuery tooltip arrow

I need help adjusting the position of the arrow to the left near the text box. Can anyone assist with this? Here's what I've tried so far: Check out the working example here: http://jsfiddle.net/b8fcg/ HTML: <input id="test" title="We ask ...

CSS - Achieving full width on hover even when the element has a specified width setting

I am encountering an issue with my CSS. Although I have successfully centered everything, I am facing a problem with the hover effect in CSS. Whenever I apply the active class or hover over an li element, the background color expands to 100%. Additionally, ...

The backface remains visible despite being designated as "hidden"

I have successfully created a "flip card" in CSS3, where the card flips downward to reveal the other side when a user hovers over it. I have ensured that the back face is not visible by setting backface-visibility to hidden. However, despite my efforts, th ...

What is the process for printing the CSS stylesheet for PPI::HTML highlight?

PPI::HTML has done an impressive job of formatting the HTML highlighting for my Perl code, similar to the example on CPAN. However, I am facing challenges in making the output usable without the appropriate CSS styles. Unfortunately, I am unsure of how to ...

Incorporate dynamic HTML into Angular by adding CSS styling

Just starting out with Angular and have limited front-end experience. I'm feeling a bit lost on how to proceed. Currently, I have a mat-table with a static datasource (will connect to a database in the future), and I need to dynamically change the bac ...

In the realm of HTML Canvas, polygons intricately intertwine with one another

Currently, I am attempting to create multiple polygons from a large JSON file. Instead of being separate, the polygons seem to be connected in some way. The project is being developed in next.js. Below are the relevant code snippets: Canvas.tsx // ../co ...

Is there a way to incorporate two Bootstrap navbars on a single page that are of varying heights?

Utilizing Bootstrap 3.0 with dual navbars on a single page that adjust in height using the same variable for both .navbar blocks. Despite attempting to incorporate an additional class to alter the height of the initial navbar, it remains unaffected. Check ...

Issue with Bootstrap Navbar-Toggle Functionality in ASP.NET with Visual Studio 2010 Not Resolving

After installing a NuGet package, I proceeded to install Bootstrap which created the following folders: Contents containing Bootstrap CSS files Scripts containing .js files In the header of my Master Page, I included the scripts like this: <scrip ...

When scrolling, dynamically change the background color by adding a class

I am looking to achieve a scroll effect where the color of the menu buttons changes. Perhaps by adding a class when scrolling and hitting the element? Each menu button and corresponding div has a unique ID. Can anyone suggest what JavaScript code I should ...

Steps to modify the border width upon gaining focus

I am struggling to adjust the border width when my input box is focused. Currently, it has a 1px solid border which changes to a 2px different color solid border upon focus. However, this change in border width is causing the containing div to shift by 1px ...