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.
reactjs sass
add a comment |
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.
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 |
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.
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
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
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
up vote
1
down vote
accepted
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 |
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:
- 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 |
up vote
1
down vote
accepted
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 |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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
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 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 |
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