Creating a variable with varying values based on different screen sizes in CSS and SASS

Is there a way to adjust the height of an image using variables in SASS? Instead of relying on percentages, I want to set different values based on various criteria.

How can I achieve this in my SASS files?

I attempted to do this but encountered an error message:

Invalid CSS after "only screen ": expected media query list, was "{ $ratio: 24px }" on line 10

Here is my code snippet:

// Small screens
@media only screen { $ration: 24px } 

@media only screen and (max-width: 40em) { $ration: 14px }

// Medium screens
@media only screen and (min-width: 40.063em) { $ration: 22px } 
@media only screen and (min-width: 40.063em) and (max-width: 64em) {$ration: 4px }


// Large screens
@media only screen and (min-width: 64.063em) {$ration: 24px } 

@media only screen and (min-width: 64.063em) and (max-width: 90em) {$ration: 24px } 

// XLarge screens
@media only screen and (min-width: 90.063em) { $ration: 24px } 

@media only screen and (min-width: 90.063em) and (max-width: 120em) { $ration: 24px } 

// XXLarge screens
@media only screen and (min-width: 120.063em) { $ration: 24px } 

UPDATE

//mobile:  320px,
//tablet:  740px,
//desktop: 980px,
//wide:    1300px


@function test() {
    @if ($HOW_TO_Get_WIDTH == 320px ) {

        return 22px;
    } @else if ($HOW_TO_Get_WIDTH == 740px) {
        @return 24px
    }
}

Answer №1

It is not possible to achieve this through the trigger @media only screen, as it operates on the client-side. One way to work around this limitation is by manually implementing something like the following:

@media only screen and (min-width: 64.063em){
      $ration: 24px; // remember to indent for scoping purposes within this media query
      // Copy over all relevant CSS rules and properties that relate to or use $ration, such as:
      #wrapper{
          width: $ration; //or other necessary property
      }
 }

@media only screen and (min-width: 40.063em){
    $ration: 22px;
    #wrapper{
        width: $ration;
    }   
}

For an example, you can refer to this link.

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

Having trouble with showing dynamic data in column2 of my HTML layout, and the alignment doesn't look quite right in HTML

I'm working with this HTML file, attempting to create a two-column layout using HTML and CSS. I want one column to be labeled REQUEST and the other RESPONSE. When a value is entered in the text field in Column 1, it should display a response in Column ...

Showing pictures from directories outside the web root (public_html)

Situation: I have a collection of images stored in a directory named uploads or images located outside the website's main folder (outside of public_html). My goal is to display one of these images inside a file called image.php. Guidelines for achi ...

iOS devices may not display the return value of jQuery's .keypress function

My HTML table is a 9x9 grid representing a Sudoku board with input elements. To validate user input and move focus to the next box, I am utilizing jQuery's .keypress method: // Validating input for numbers 1-9 (49-57), tab key (48), and Enter key (1 ...

Suggestions on how to refactor redundant code in various peer AngularJS controllers for handling $intervals

In my compact single-page application, I have implemented multiple tabs to display operational status information for different applications. Each tab is associated with a controller that creates $interval objects to make AJAX calls to fetch status data fr ...

Trouble with CSS3 Menu Layout and Gradient Display Issues

My task is to replicate this menu: I'm having trouble creating the gradient. Completed Why can't I see it in my code? http://jsfiddle.net/Vtr6d/ Here's what I currently have: CSS: .mainOptions{ float:left; margin:0 20px 0 20px; ...

Invert the motion within a photo carousel

Is there anyone who can assist me with creating a unique photo slider using HTML, CSS, and JS? Currently, it includes various elements such as navigation arrows, dots, and an autoplay function. The timer is reset whenever the arrows or dots are clicked. Ev ...

Order of random div classes

After receiving advice from several individuals, I have revised my question by removing unnecessary code and keeping only the essential parts. I hope this simplification is clear enough for my question to be reopened. Here is the fiddle link: http://jsfid ...

The Web Browser is organizing CSS elements in an alphabetized sequence

map.css({ 'zoom': zoom, 'left': map.width()/(2*zoom) - (point[0]/100)*map.width(), 'top': map.height()/(2*zoom) - (point[1]/100)*map.height() Upon observation, it appears that Chrome adjusts the map zoom first be ...

What is the best way to send an HTML report through email with embedded images as an attachment?

Currently, I am in the process of generating a report using ExtentReports that will be distributed via email to team members who are outside of our domain. To capture screenshots of any test failures, I utilize a screenshot method which saves these images ...

Automatically showcase images from a directory upon webpage loading

Is there a way to modify my code so that the images from the first directory are displayed on the page when it loads, instead of waiting for a menu option to be clicked? The page looks empty until a menu option is selected, and I would like the images to s ...

Full replacement of a cell within an HTML table

I have a scenario like the following: <table id="myTable"> <tr> <td class="1">cell 1</td> <td class="2">cell 2</td> </tr> <tr> <td class="3">cell 3</td> &l ...

Hyperlinks are malfunctioning and the text cannot be highlighted

Here is my XML code: <!DOCTYPE HTML SYSTEM> <html> <head> <link rel="stylesheet" type="text/css" href="style.css"></link> </head> <body> <div class="header"> <img id="circle" src="circle.png" ...

`A mistake occurred while building in the package.json file`

While attempting to run all build processes by using the command npm run build:css, I encountered an error indicated below. Even after running npm cache clean --force, the issue remains unresolved. https://i.sstatic.net/4edDo.png npm ERR! code ELIFECYCLE ...

Ensure that the nested div maintains its height consistently across all three columns

In my layout, I am trying to achieve uniform height for three columns, each containing the same sections like title and address. Not only do I want the cards to have the same height, but also the nested sections should maintain equal heights. For instance, ...

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

What are the best practices for ensuring design responsiveness with the Bootstrap grid system across various screen resolutions?

<div class="row"> <div class="col-xl-4 col-md-12 col-sm-12 col-lg-4 data-one"> <one /> </div> <div class="col-xl-4 col-md-12 col-sm-12 col-lg-4 dashboard-two"> <two /> </div> <div ...

Is it possible to use Selenium with Python for copying and pasting

Is there a way to use control + A in Selenium Python to select text and then retrieve the highlighted text? ...

The true screen resolution of Safari on iPhone devices

I'm feeling a bit lost right now. Currently, I am using Jquery Mobile and here is my code: <meta content='width=device-width, minimum-scale=1.0, maximum-scale=1.0,user-scalable=no' name='viewport'> When I ran the following ...

Build within an HTML document

I am currently working on creating a new page for product registration, but I am encountering multiple errors in a specific block of code and unable to find a solution. { for (int i = 0; i < Model.Produto.ListProduto ...

Passing an undefined value to the database via AJAX upon clicking a button

Hi there, I'm currently working on a table where I'm trying to perform an inline edit and update the value in the database by clicking on a button (an image). I've attempted to use an onclick function, but it seems to show "value=undefined&a ...