Global media queries on a react project











up vote
1
down vote

favorite












I'm trying to generate a general structure of styles on scss with global breakpoints as media queries on a react project, It is possible to reuse an structure to follow media queries that we declare as global.
I'm a little bit lost on this one, any ideas?



When I mean global is that we can define the breakpoints at the root of the project and we can use any reference on the components.



Thanks in advance.










share|improve this question
























  • This question needs more detail. When you say "global", do you mean to imply that you want to include the style at the top level of your app only, rather than declared component by component?
    – AnonymousSB
    Nov 21 at 2:56















up vote
1
down vote

favorite












I'm trying to generate a general structure of styles on scss with global breakpoints as media queries on a react project, It is possible to reuse an structure to follow media queries that we declare as global.
I'm a little bit lost on this one, any ideas?



When I mean global is that we can define the breakpoints at the root of the project and we can use any reference on the components.



Thanks in advance.










share|improve this question
























  • This question needs more detail. When you say "global", do you mean to imply that you want to include the style at the top level of your app only, rather than declared component by component?
    – AnonymousSB
    Nov 21 at 2:56













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm trying to generate a general structure of styles on scss with global breakpoints as media queries on a react project, It is possible to reuse an structure to follow media queries that we declare as global.
I'm a little bit lost on this one, any ideas?



When I mean global is that we can define the breakpoints at the root of the project and we can use any reference on the components.



Thanks in advance.










share|improve this question















I'm trying to generate a general structure of styles on scss with global breakpoints as media queries on a react project, It is possible to reuse an structure to follow media queries that we declare as global.
I'm a little bit lost on this one, any ideas?



When I mean global is that we can define the breakpoints at the root of the project and we can use any reference on the components.



Thanks in advance.







reactjs sass






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 4:11

























asked Nov 21 at 1:39









Nestor

390213




390213












  • This question needs more detail. When you say "global", do you mean to imply that you want to include the style at the top level of your app only, rather than declared component by component?
    – AnonymousSB
    Nov 21 at 2:56


















  • This question needs more detail. When you say "global", do you mean to imply that you want to include the style at the top level of your app only, rather than declared component by component?
    – AnonymousSB
    Nov 21 at 2:56
















This question needs more detail. When you say "global", do you mean to imply that you want to include the style at the top level of your app only, rather than declared component by component?
– AnonymousSB
Nov 21 at 2:56




This question needs more detail. When you say "global", do you mean to imply that you want to include the style at the top level of your app only, rather than declared component by component?
– AnonymousSB
Nov 21 at 2:56












1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










There are three ways that come to mind:




  1. You can create a variables.scss file in which you can write the value of your breakpoints:


$sm: 576px;
$md: 768px;
$lg: 992px;
$xl: 1200px;



And the use the following variables in your scss:



@media only screen and (min-width: $sm) {
.container {
.max-width: 450px;
}
}
@media only screen and (min-width: $md) {
.container {
.max-width: 650px;
}
}
@media only screen and (min-width: $lg) {
.container {
.max-width: 900px;
}
}
@media only screen and (min-width: $xl) {
.container {
.max-width: 1000px;
}
}




  1. Or you can the mentioned variables in your mixins.scss file to create some media query mixins:



    @mixin small {
    @media only screen and (min-width: $sm) {
    @content;
    }
    }




And then, use these mixins in your main scss codes:



.container {
@include small {
max-width: 450px;
}
...
}




  1. Or if the use cases of these media queries are limited (e.g. hiding and showing elements), you can define other mixins that include all the variations:



    $displays: none inline inline-block block table table-cell table-row flex inline-flex;
    $sizes: (
    sm: $sm,
    md: $md,
    lg: $lg,
    lg: $xl
    );
    @each $display in $displays:
    @each $size-key $size in $sizes {
    .display-#{size-key}-#{display} {
    display: $display !important;
    }
    }
    }




A note on importing files: I personally would import all my helper scss (variables, mixins, etc.) in a file called styles/index.scss in the root of my project among with normalizing and other global rules that I want to define, and then import this file in my other scss files:



// styles/index.scss
@import './variables.scss';
@import './mixins.scss';
...

// container.scss
@import './styles/index.scss';





share|improve this answer



















  • 1




    I will reference this variables.scss on all my components ? or that file will be automatic be referenced on each component?
    – Nestor
    Nov 21 at 4:44










  • Nice Masious, And the last question to finish this one. Do you know if that import will be replicated for each component or it will be reduced once we go to deployment?
    – Nestor
    Nov 21 at 5:02












  • Sure yeah! you import the index.scss file in every file which wants to use these values, but when it transpiles, everything will be imported only once! Besides, there is nothing left of the variable definitions in the css output! P.S: A vote up would also be appreciated. :)
    – Masious
    Nov 21 at 5:05













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404146%2fglobal-media-queries-on-a-react-project%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










There are three ways that come to mind:




  1. You can create a variables.scss file in which you can write the value of your breakpoints:


$sm: 576px;
$md: 768px;
$lg: 992px;
$xl: 1200px;



And the use the following variables in your scss:



@media only screen and (min-width: $sm) {
.container {
.max-width: 450px;
}
}
@media only screen and (min-width: $md) {
.container {
.max-width: 650px;
}
}
@media only screen and (min-width: $lg) {
.container {
.max-width: 900px;
}
}
@media only screen and (min-width: $xl) {
.container {
.max-width: 1000px;
}
}




  1. Or you can the mentioned variables in your mixins.scss file to create some media query mixins:



    @mixin small {
    @media only screen and (min-width: $sm) {
    @content;
    }
    }




And then, use these mixins in your main scss codes:



.container {
@include small {
max-width: 450px;
}
...
}




  1. Or if the use cases of these media queries are limited (e.g. hiding and showing elements), you can define other mixins that include all the variations:



    $displays: none inline inline-block block table table-cell table-row flex inline-flex;
    $sizes: (
    sm: $sm,
    md: $md,
    lg: $lg,
    lg: $xl
    );
    @each $display in $displays:
    @each $size-key $size in $sizes {
    .display-#{size-key}-#{display} {
    display: $display !important;
    }
    }
    }




A note on importing files: I personally would import all my helper scss (variables, mixins, etc.) in a file called styles/index.scss in the root of my project among with normalizing and other global rules that I want to define, and then import this file in my other scss files:



// styles/index.scss
@import './variables.scss';
@import './mixins.scss';
...

// container.scss
@import './styles/index.scss';





share|improve this answer



















  • 1




    I will reference this variables.scss on all my components ? or that file will be automatic be referenced on each component?
    – Nestor
    Nov 21 at 4:44










  • Nice Masious, And the last question to finish this one. Do you know if that import will be replicated for each component or it will be reduced once we go to deployment?
    – Nestor
    Nov 21 at 5:02












  • Sure yeah! you import the index.scss file in every file which wants to use these values, but when it transpiles, everything will be imported only once! Besides, there is nothing left of the variable definitions in the css output! P.S: A vote up would also be appreciated. :)
    – Masious
    Nov 21 at 5:05

















up vote
1
down vote



accepted










There are three ways that come to mind:




  1. You can create a variables.scss file in which you can write the value of your breakpoints:


$sm: 576px;
$md: 768px;
$lg: 992px;
$xl: 1200px;



And the use the following variables in your scss:



@media only screen and (min-width: $sm) {
.container {
.max-width: 450px;
}
}
@media only screen and (min-width: $md) {
.container {
.max-width: 650px;
}
}
@media only screen and (min-width: $lg) {
.container {
.max-width: 900px;
}
}
@media only screen and (min-width: $xl) {
.container {
.max-width: 1000px;
}
}




  1. Or you can the mentioned variables in your mixins.scss file to create some media query mixins:



    @mixin small {
    @media only screen and (min-width: $sm) {
    @content;
    }
    }




And then, use these mixins in your main scss codes:



.container {
@include small {
max-width: 450px;
}
...
}




  1. Or if the use cases of these media queries are limited (e.g. hiding and showing elements), you can define other mixins that include all the variations:



    $displays: none inline inline-block block table table-cell table-row flex inline-flex;
    $sizes: (
    sm: $sm,
    md: $md,
    lg: $lg,
    lg: $xl
    );
    @each $display in $displays:
    @each $size-key $size in $sizes {
    .display-#{size-key}-#{display} {
    display: $display !important;
    }
    }
    }




A note on importing files: I personally would import all my helper scss (variables, mixins, etc.) in a file called styles/index.scss in the root of my project among with normalizing and other global rules that I want to define, and then import this file in my other scss files:



// styles/index.scss
@import './variables.scss';
@import './mixins.scss';
...

// container.scss
@import './styles/index.scss';





share|improve this answer



















  • 1




    I will reference this variables.scss on all my components ? or that file will be automatic be referenced on each component?
    – Nestor
    Nov 21 at 4:44










  • Nice Masious, And the last question to finish this one. Do you know if that import will be replicated for each component or it will be reduced once we go to deployment?
    – Nestor
    Nov 21 at 5:02












  • Sure yeah! you import the index.scss file in every file which wants to use these values, but when it transpiles, everything will be imported only once! Besides, there is nothing left of the variable definitions in the css output! P.S: A vote up would also be appreciated. :)
    – Masious
    Nov 21 at 5:05















up vote
1
down vote



accepted







up vote
1
down vote



accepted






There are three ways that come to mind:




  1. You can create a variables.scss file in which you can write the value of your breakpoints:


$sm: 576px;
$md: 768px;
$lg: 992px;
$xl: 1200px;



And the use the following variables in your scss:



@media only screen and (min-width: $sm) {
.container {
.max-width: 450px;
}
}
@media only screen and (min-width: $md) {
.container {
.max-width: 650px;
}
}
@media only screen and (min-width: $lg) {
.container {
.max-width: 900px;
}
}
@media only screen and (min-width: $xl) {
.container {
.max-width: 1000px;
}
}




  1. Or you can the mentioned variables in your mixins.scss file to create some media query mixins:



    @mixin small {
    @media only screen and (min-width: $sm) {
    @content;
    }
    }




And then, use these mixins in your main scss codes:



.container {
@include small {
max-width: 450px;
}
...
}




  1. Or if the use cases of these media queries are limited (e.g. hiding and showing elements), you can define other mixins that include all the variations:



    $displays: none inline inline-block block table table-cell table-row flex inline-flex;
    $sizes: (
    sm: $sm,
    md: $md,
    lg: $lg,
    lg: $xl
    );
    @each $display in $displays:
    @each $size-key $size in $sizes {
    .display-#{size-key}-#{display} {
    display: $display !important;
    }
    }
    }




A note on importing files: I personally would import all my helper scss (variables, mixins, etc.) in a file called styles/index.scss in the root of my project among with normalizing and other global rules that I want to define, and then import this file in my other scss files:



// styles/index.scss
@import './variables.scss';
@import './mixins.scss';
...

// container.scss
@import './styles/index.scss';





share|improve this answer














There are three ways that come to mind:




  1. You can create a variables.scss file in which you can write the value of your breakpoints:


$sm: 576px;
$md: 768px;
$lg: 992px;
$xl: 1200px;



And the use the following variables in your scss:



@media only screen and (min-width: $sm) {
.container {
.max-width: 450px;
}
}
@media only screen and (min-width: $md) {
.container {
.max-width: 650px;
}
}
@media only screen and (min-width: $lg) {
.container {
.max-width: 900px;
}
}
@media only screen and (min-width: $xl) {
.container {
.max-width: 1000px;
}
}




  1. Or you can the mentioned variables in your mixins.scss file to create some media query mixins:



    @mixin small {
    @media only screen and (min-width: $sm) {
    @content;
    }
    }




And then, use these mixins in your main scss codes:



.container {
@include small {
max-width: 450px;
}
...
}




  1. Or if the use cases of these media queries are limited (e.g. hiding and showing elements), you can define other mixins that include all the variations:



    $displays: none inline inline-block block table table-cell table-row flex inline-flex;
    $sizes: (
    sm: $sm,
    md: $md,
    lg: $lg,
    lg: $xl
    );
    @each $display in $displays:
    @each $size-key $size in $sizes {
    .display-#{size-key}-#{display} {
    display: $display !important;
    }
    }
    }




A note on importing files: I personally would import all my helper scss (variables, mixins, etc.) in a file called styles/index.scss in the root of my project among with normalizing and other global rules that I want to define, and then import this file in my other scss files:



// styles/index.scss
@import './variables.scss';
@import './mixins.scss';
...

// container.scss
@import './styles/index.scss';






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 at 4:57

























answered Nov 21 at 4:40









Masious

344213




344213








  • 1




    I will reference this variables.scss on all my components ? or that file will be automatic be referenced on each component?
    – Nestor
    Nov 21 at 4:44










  • Nice Masious, And the last question to finish this one. Do you know if that import will be replicated for each component or it will be reduced once we go to deployment?
    – Nestor
    Nov 21 at 5:02












  • Sure yeah! you import the index.scss file in every file which wants to use these values, but when it transpiles, everything will be imported only once! Besides, there is nothing left of the variable definitions in the css output! P.S: A vote up would also be appreciated. :)
    – Masious
    Nov 21 at 5:05
















  • 1




    I will reference this variables.scss on all my components ? or that file will be automatic be referenced on each component?
    – Nestor
    Nov 21 at 4:44










  • Nice Masious, And the last question to finish this one. Do you know if that import will be replicated for each component or it will be reduced once we go to deployment?
    – Nestor
    Nov 21 at 5:02












  • Sure yeah! you import the index.scss file in every file which wants to use these values, but when it transpiles, everything will be imported only once! Besides, there is nothing left of the variable definitions in the css output! P.S: A vote up would also be appreciated. :)
    – Masious
    Nov 21 at 5:05










1




1




I will reference this variables.scss on all my components ? or that file will be automatic be referenced on each component?
– Nestor
Nov 21 at 4:44




I will reference this variables.scss on all my components ? or that file will be automatic be referenced on each component?
– Nestor
Nov 21 at 4:44












Nice Masious, And the last question to finish this one. Do you know if that import will be replicated for each component or it will be reduced once we go to deployment?
– Nestor
Nov 21 at 5:02






Nice Masious, And the last question to finish this one. Do you know if that import will be replicated for each component or it will be reduced once we go to deployment?
– Nestor
Nov 21 at 5:02














Sure yeah! you import the index.scss file in every file which wants to use these values, but when it transpiles, everything will be imported only once! Besides, there is nothing left of the variable definitions in the css output! P.S: A vote up would also be appreciated. :)
– Masious
Nov 21 at 5:05






Sure yeah! you import the index.scss file in every file which wants to use these values, but when it transpiles, everything will be imported only once! Besides, there is nothing left of the variable definitions in the css output! P.S: A vote up would also be appreciated. :)
– Masious
Nov 21 at 5:05




















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53404146%2fglobal-media-queries-on-a-react-project%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...