Search code examples
cssmedia-queries

Questions about CSS media-queries


I am trying to learn media query in css and I have few questions about some of the examples that I have come across. The queries are mentioned below:

  1. I have seen a variable was declared in the following format in a .scss file which is used in a react component:

    $screen-xs-max: ($screen-sm-min - 1);
    

    Why is -1 used here?

  2. The second question that I have is about this:

    $large-screens-up: "(min-width: #{$screen-lg-min})";
    

    I have 2 questions about these lines of code:

    • Why is the variable declared within the " ", doesn't that make the variable a string?

    • Why is # used here? I guess it is to find the variable $screen-lg-min in the path from where it is imported, but I am not sure if its correct. I just want to confirm if that's the correct thing or correct me if I am wrong.

Can anyone please help me with these doubts? I am sorry if this is too simple. I tried getting the answers myself, but couldn't find it.


Solution

  • In SCSS

    Consider $screen-sm-min:546px; which will be declared in scss variables file in your project or the node modules folder.

    $screen-xs-max: ($screen-sm-min - 1); means that the value of $screen-xs-max will be 1 less than $screen-sm-min that is 545px.

    $large-screens-up: "(min-width: #{$screen-lg-min})";
    

    Varible in scss can be used directly using $varible-name ,

    But when you want to use the same variable inside a string in scss u will have to follow this

    #{$variable-name} method

    Why -1

    Consider extra small devices width to be 0 to 545px(maxvalue).

    Consider small devices width to be 546px(minvalue) to 768px(maxvalue)

    Therefore the max width of the extra small devices will be

    (min value of small devices) - 1
    

    This method is used to avoid harcoded values in scss file,

    For example if you decide to change the values of the width, you can change it in only one place and let the formulae handle the remaining calculation of the widths