How to group media query for many component styled in react - css

I have not found any solution for avoid repeat media query for all styled components. I have a grid layout and i want to rearrange when display screen changes
It's very ugly to same media query repeated for any styled components
import React from "react";
import styled from "styled-components";
const Container = styled.div`
height: 100%;
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: 60% 40%;
#media screen and (max-width: 768px) {
grid-template-columns: 1fr;
grid-template-rows: 30% 20% 30% 30%;
}
`
const MainContainer = styled.div`
background-color: salmon;
grid-column: span 8;
#media screen and (max-width: 768px) {
grid-column: span 12;
}
`
const ListContainer = styled.div`
background-color: violet;
grid-column: span 4;
grid-row: span 2;
#media screen and (max-width: 768px) {
grid-column: span 12;
grid-row: 2;
}
`
const Item2 = styled.div`
background-color: fuchsia;
grid-column: span 3;
#media screen and (max-width: 768px) {
grid-column: span 12;
grid-row: 3;
}
`
const Item3 = styled.div`
background-color: lawngreen;
grid-column: span 5;
#media screen and (max-width: 768px) {
grid-column: span 12;
grid-row: 4;
}
`
const Home = () => {
return(
<Container>
<MainContainer>Main</MainContainer>
<ListContainer>List</ListContainer>
<Item2>Today</Item2>
<Item3>Forecast</Item3>
</Container>
)
}
export default Home
I want to use once
#media screen and (max-width: 768px) {
MainContainer {
/*some changes style*/
}
Item2{
/*some other changes style*/
}
/* ....and so on */
}
how can i do using styled components?
I tryed to use for each styled component same media query but i want to avoid repeated code
****Edit
i've solved this issue with #Pius Kariuki inspiration
const MainContainer = styled.div`
background-color: salmon;
grid-column: span 8;
`
const ListContainer = styled.div`
background-color: violet;
grid-column: span 4;
grid-row: span 2;
`
const Item2 = styled.div`
background-color: fuchsia;
grid-column: span 3;
`
const Item3 = styled.div`
background-color: lawngreen;
grid-column: span 5;
`
const Container = styled.div`
height: 100%;
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: 60% 40%;
#media screen and (max-width: 768px) {
grid-template-columns: 1fr;
grid-template-rows: 30% 20% 30% 30%;
${Item3} {
grid-column: span 12;
grid-row: 4;
}
${Item2} {
grid-column: span 12;
grid-row: 3;
}
${ListContainer} {
grid-column: span 12;
grid-row: 2;
}
${MainContainer} {
grid-column: span 12;
}
}
`
It's important to put Container style at the end after each child are already declared

One way to avoid repeating the media query in every styled component is by using the CSS #media rule and defining the changes for each component inside it. Here's an example:
import React from "react";
import styled from "styled-components";
const Container = styled.div`
height: 100%;
display: grid;
grid-template-columns: repeat(12, 1fr);
grid-template-rows: 60% 40%;
/* Media Query */
#media screen and (max-width: 768px) {
grid-template-columns: 1fr;
grid-template-rows: 30% 20% 30% 30%;
}
`
const MainContainer = styled.div`
background-color: salmon;
grid-column: span 8;
`
const ListContainer = styled.div`
background-color: violet;
grid-column: span 4;
grid-row: span 2;
`
const Item2 = styled.div`
background-color: fuchsia;
grid-column: span 3;
`
const Item3 = styled.div`
background-color: lawngreen;
grid-column: span 5;
`
/* Media Query for all components */
#media screen and (max-width: 768px) {
${MainContainer} {
/* changes for MainContainer */
}
${Item2} {
/* changes for Item2 */
}
${Item3} {
/* changes for Item3 */
}
}
In the example above, the #media rule is defined outside of the styled-components. Then, the selector for each styled component is added to the #media rule to define the changes that should be applied to it when the screen size is less than or equal to 768px.
This way, you can avoid repeating the media query for each styled component, and define all the changes in one place.
Let me know if this helped.

Make a parent styled component that includes your media queries, then inherit other styled components with that component
Parent media component:
const ParentMediaComponent = styled.div`
// Global Media Queries
`
Other Child compnents inherited with parent media component:
const MainContainer = styled(ParentMediaComponent)`
// Child compnents style
`
const ListContainer = styled(ParentMediaComponent)`
// Child compnents style
`

Related

5 column item listing is really bad in mobile how can i fix this?

I have a business directory here it is https://www.endustri.io/firma/
I am listing the categories in five columns. When i looked the mobile i was shocked. Really bad looking. How can fix this?
I want to see mobile version look like desktop, or better than this time.
Here is my css code
#native {
width: 100%;
height: min-content; /*your fixed height*/
display: inline-block; /*necessary*/
-webkit-column-count: 5;
-moz-column-count: 5;
column-count: 5;
}
This is desktop
And this is mobile
You could solve it by changing column-count on mobile devices to, for example, 2. You can target mobile devices with #media screen of specific range:
#media screen and (max-width: 767px) {
#native {
-webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
}
}
Always write CSS in Mobile First Methodology: And your solution for this question is below
#native {
width: 100%;
height: min-content; /*your fixed height*/
display: inline-block; /*necessary*/
-webkit-column-count: 1;
-moz-column-count: 1;
column-count: 1;
}
#media screen and (min-width: 768px) {
#native {
-webkit-column-count: 2;
-moz-column-count: 2;
column-count: 2;
}
}
#media screen and (min-width: 992px) {
#native {
-webkit-column-count: 3;
-moz-column-count: 3;
column-count: 3;
}
}
#media screen and (min-width: 1200px) {
#native {
-webkit-column-count: 5;
-moz-column-count: 5;
column-count: 5;
}
}

How to make layout so images takes remaining height [duplicate]

This question already has answers here:
CSS-only masonry layout
(4 answers)
Closed 5 months ago.
I want to build this layout so my images, that are in 2nd and 3rd row take remaining space, to go all the way to first row. I just displayed flex and set flex-wrap: wrap. But that does not solve my problem. This is what i want
use column-count for the container of images check here for more info
function getRandomSize(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
var allImages = "";
for (var i = 0; i < 30; i++) {
var width = getRandomSize(200, 400);
var height = getRandomSize(200, 400);
allImages += '<img src="https://placekitten.com/'+width+'/'+height+'?random='+i+'" alt="gallery photo">';
}
$('#photos').append(allImages);
#photos {
/* Prevent vertical gaps */
line-height: 0;
-webkit-column-count: 5;
-webkit-column-gap: 0px;
-moz-column-count: 5;
-moz-column-gap: 0px;
column-count: 5;
column-gap: 0px;
}
#photos img {
/* Just in case there are inline attributes */
width: 100% !important;
height: auto !important;
}
#media (max-width: 1200px) {
#photos {
-moz-column-count: 4;
-webkit-column-count: 4;
column-count: 4;
}
}
#media (max-width: 1000px) {
#photos {
-moz-column-count: 3;
-webkit-column-count: 3;
column-count: 3;
}
}
#media (max-width: 800px) {
#photos {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
}
}
#media (max-width: 400px) {
#photos {
-moz-column-count: 1;
-webkit-column-count: 1;
column-count: 1;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="photos"></section>

What am I doing wrong considering the divs will not scale according to my media query input?

So I am at the beginning, doing different tutorials and challenging myself with conquering the fundamentals. I know this might seem lowkey for most people but be gentle, i'm sorta new to this.
I tried using Media Queries 4 for example #media (30em <= width <= 50em ) { ... } but it jsut doesn't work for me (browser compatibility is checked btw) so I went with a classic code writing (which you may see below). Unfortunately my divs will not scale properly, I am clearly missing something like a parent-child not sharing the proper settings but I can't see it. Could you point out my mistake please? All it needs to do is scale the divs if the width is lower than 600, between 601 and 960 and above 961 (obv .px)
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Mobile Styles */
#media only screen and (max-width: 600px) {
body {
background-color: #F09A9D;
}
}
/* Tablet Styles */
#media only screen and (min-width: 601px) and (max-width: 960px) {
.sign-up,
.feature-1,
.feature-2,
.feature-3 {
width: 50%;
}
}
/* Desktop Styles */
#media only screen and (min-width: 961px) {
.page {
width: 960px;
margin: 0 auto;
}
.feature-1,
.feature-2,
.feature-3 {
width: 33.3%;
}
.header {
height: 400px;
}
}
.page {
display: flex;
flex-wrap: wrap;
}
.section {
width: 100%;
height: 300px;
display: flex;
justify-content: center;
align-items: center;
}
.menu {
background-color: #5995DA;
height: 80px;
}
.header {
background-color: #B2D6FF;
}
.content {
background-color: #EAEDF0;
height: 600px;
}
.sign-up {
background-color: #D6E9FE;
}
.feature-1 {
background-color: #F5CF8E;
}
.feature-2 {
background-color: #F09A9D;
}
.feature-3 {
background-color: #C8C6FA;
}
The html is just a bunch of divs with an img src inside them. The output is the same no matter what the size of the browser window is.
#sbrrk is right. And also, you should write your media queries at the very bottom, so they will override other rules of the same specificity

How to Convert CSS Grid Template Columns to Rows Responsively

I am trying to display a 3 column layout on desktop and a single column layout on mobile using CSS Grid.
However, when I try the following a 3 column layout persists with a viewport smaller than 650px.
.page {
display: grid;
grid-template-columns: 33% 34% 33%
}
#media (max-width: 650px) {
.page {
grid-template-columns: 100%;
}
}
How do I transition between a 3 column wide layout to single column stacked layout responsively with CSS Grid?
You can use repeat() for this and just set the columns to one frame for the media query.
.page {
display: grid;
grid-template-columns: repeat(3, 1fr);
}
#media (max-width: 650px) {
.page {
grid-template-columns: 1fr;
}
}
You can add a grid-template-area property as follows:
.page {
display: grid;
grid-template-area: "a b c";
grid-template-columns: 33% 34% 33%
}
#media (max-width: 650px) {
.page {
grid-template-columns: 100%;
grid-template-area:
"a"
"b"
"c";
}
}
Where string delimiters denote new rows.

How to utilize responsive images using CSS Grid Layout

Ive been working with the new CSS Grid Layout and one thing I can't seem to find much info on is how to make images responsive?? I've tried using media queries and setting the image width to 100% but the image still overflows outside of the grid. What am I missing?
You can use below concept :
function getRandomSize(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
var allImages = "";
for (var i = 0; i < 25; i++) {
var width = getRandomSize(200, 400);
var height = getRandomSize(200, 400);
allImages += '<img src="https://placekitten.com/'+width+'/'+height+'" alt="pretty kitty">';
}
$('#photos').append(allImages);
#photos {
/* Prevent vertical gaps */
line-height: 0;
-webkit-column-count: 5;
-webkit-column-gap: 0px;
-moz-column-count: 5;
-moz-column-gap: 0px;
column-count: 5;
column-gap: 0px;
}
#photos img {
/* Just in case there are inline attributes */
width: 100% !important;
height: auto !important;
}
#media (max-width: 1200px) {
#photos {
-moz-column-count: 4;
-webkit-column-count: 4;
column-count: 4;
}
}
#media (max-width: 1000px) {
#photos {
-moz-column-count: 3;
-webkit-column-count: 3;
column-count: 3;
}
}
#media (max-width: 800px) {
#photos {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
}
}
#media (max-width: 400px) {
#photos {
-moz-column-count: 1;
-webkit-column-count: 1;
column-count: 1;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<section id="photos"></section>
Provide imd default styles of max-width:100% and avoid using fixed width and height. Hope it is helpful to you.

Resources