Global media queries on a react project
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
add a comment |
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
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
add a comment |
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
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
reactjs sass
edited Nov 21 at 4:11
asked Nov 21 at 1:39
Nestor
392214
392214
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
There are three ways that come to mind:
- 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;
}
}
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;
}
...
}
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';
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 theindex.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
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
There are three ways that come to mind:
- 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;
}
}
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;
}
...
}
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';
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 theindex.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
add a comment |
There are three ways that come to mind:
- 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;
}
}
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;
}
...
}
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';
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 theindex.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
add a comment |
There are three ways that come to mind:
- 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;
}
}
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;
}
...
}
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';
There are three ways that come to mind:
- 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;
}
}
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;
}
...
}
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';
edited Nov 21 at 4:57
answered Nov 21 at 4:40
Masious
354213
354213
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 theindex.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
add a comment |
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 theindex.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
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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