Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make CSS grid stack on mobile?

Tags:

html

css

css-grid

I have a CSS grid that has grid template columns set to grid-template-columns: repeat(auto-fit, minmax(225px, 1fr))

When I check the responsiveness of the site for mobile, the grid elements just get smaller and don't stack. I am trying to get them to stack into a 1fr column on mobile. I have tried using a media query to set the grid template columns to 1fr at a break-point and that didn't work.

Here is a link to a CodePen of what I have https://codepen.io/Swildman/pen/ZEzqvXK

/* variables for colors */

:root {
  --highlight-color: #ff7264;
  --white-color: #fff;
  --text-color: #7f7f7f;
  --dark-bg-color: #2e2e2e;
  --light-bg-color: #fff;
  --gray-bg-color: #666;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}


/* services section */

.services-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(225px, 1fr));
  grid-template-rows: repeat(3, 200px);
  grid-gap: 5px;
  margin: 5px;
}

.cell {
  background: var(--highlight-color);
  text-align: center;
  color: var(--white-color);
  padding: 10px;
}

.cell h3 {
  font-size: 20px;
}

.cell p {
  line-height: 1.4em;
}

.cell-1 {
  grid-column: 1/3;
  grid-row: 1/3;
  padding: 10px;
}

.cell-1 p,
.cell-4 p {
  font-size: 20px;
  line-height: 1.4em;
}

.cell-1 h3,
.cell-4 h3 {
  font-size: 30px;
}

.cell-1:hover {
  background-image: url("https://picsum.photos/460");
}

.cell-1:hover p,
.cell-1:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-2 {
  background-image: url("https://picsum.photos/225");
}

.cell-2 h3,
.cell-2 p {
  opacity: 0;
}

.cell-2:hover {
  background: var(--highlight-color);
}

.cell-2:hover h3,
.cell-2:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-3 {
  background-image: url("https://picsum.photos/225");
}

.cell-3 h3,
.cell-3 p {
  opacity: 0;
}

.cell-3:hover {
  background: var(--highlight-color);
}

.cell-3:hover h3,
.cell-3:hover p {
  opacity: 1;
  transition: 1000ms
}

.cell-4 {
  grid-column: 3/5;
  grid-row: 2/4;
  padding: 10px;
}

.cell-4:hover {
  background-image: url("https://picsum.photos/460");
  background-repeat: no-repeat;
  background-size: cover;
}

.cell-4:hover p,
.cell-4:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-5 {
  background-image: url("https://picsum.photos/225");
}

.cell-5 h3,
.cell-5 p {
  opacity: 0;
}

.cell-5:hover {
  background: var(--highlight-color);
}

.cell-5:hover h3,
.cell-5:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-6 {
  background-image: url("https://picsum.photos/225");
}

.cell-6 p,
.cell-6 h3 {
  opacity: 0;
}

.cell-6:hover {
  background: var(--highlight-color);
}

.cell-6:hover p,
.cell-6:hover h3 {
  opacity: 1;
  transition: 1000ms;
}

#services-section {
  text-align: center;
  margin-top: 30px;
  max-width: 900px;
  margin: 30px auto;
}

.services-title {
  font-size: 2em;
  text-shadow: 1px 1px var(--text-color);
  color: var(--highlight-color);
}

@media screen and (max-width: 768px) {
  grid-template-columns: 1fr;
}
<section id="services-section">
  <h2 class="services-title">Services</h2>
  <div class="services-container">
    <div class="cell cell-1">
      <h3>Creative Web Design Services</h3>
      <p>Our web design services will greatly enhance your business’s presence on the Internet. Our designers mix a potent combination of brand strategy with a generous splash of creative juices and blend in the latest trends in Website UX and UI design.
        Since 2019, S&E has designed and built over 25 Websites from e-commerce, b2c, b2b, non-profit, to social networks. We even create custom web applications.</p>
    </div>
    <div class="cell cell-2">
      <h3>Digital Marketing</h3>
      <p>Our digital marketing services are driven not only by passion, but also a driving desire to deliver exceptional sales conversions.</p>
    </div>
    <div class="cell cell-3">
      <h3>App Development</h3>
      <p>Our app development services are sought after from venture capital start-ups to fortune 100 companies.</p>
    </div>
    <div class="cell cell-4">
      <h3>Website Maintenance</h3>
      <p>Extend your Website’s Lifespan</p>
      <p>By subscribing into a website maintenance plan with S&E, we can help your website keep up with the ever-changing and evolving industry.</p>
      <p>S&E is one of the leading website maintenance agencies. Our web maintenance packages cover everything that your website may need – from simple content updates to extensive design updates.</p>
    </div>
    <div class="cell cell-5">
      <h3>Web Development</h3>
      <p>If you’re looking for custom Internet applications or complex web development solutions, you’ve come to the right place.</p>
    </div>
    <div class="cell cell-6">
      <h3>Branding</h3>
      <p>Branding has never been more expansive, adventurous and agile than it is today</p>
    </div>
  </div>
</section>
like image 984
swildman75 Avatar asked Sep 18 '19 16:09

swildman75


3 Answers

One of the biggest problems was in your media query - you didn't specify any element to apply the property to.

Here's the important part of the CSS that I changed:

@media screen and (max-width: 768px) {
  .services-container {
      grid-template-columns: 1fr;
      grid-template-rows: 1fr;
   }

  .cell-1, .cell-2, .cell-3, .cell-4, .cell-5, .cell-6 {
    grid-column: auto;
    grid-row: auto;
  }
}

You can see, I also reset all of your .cell to auto for placement.

/* variables for colors */

:root {
  --highlight-color: #ff7264;
  --white-color: #fff;
  --text-color: #7f7f7f;
  --dark-bg-color: #2e2e2e;
  --light-bg-color: #fff;
  --gray-bg-color: #666;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}


/* services section */

.services-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(225px, 1fr));
  grid-template-rows: repeat(3, 200px);
  grid-gap: 5px;
  margin: 5px;
}

.cell {
  background: var(--highlight-color);
  text-align: center;
  color: var(--white-color);
  padding-top: 10px;
}

.cell h3 {
  font-size: 20px;
}

.cell p {
  line-height: 1.4em;
}

.cell-1 {
  grid-column: 1/3;
  grid-row: 1/3;
  padding: 10px;
}

.cell-1 p,
.cell-4 p {
  font-size: 20px;
  line-height: 1.4em;
}

.cell-1 h3,
.cell-4 h3 {
  font-size: 30px;
}

.cell-1:hover {
  background-image: url("https://picsum.photos/460");
}

.cell-1:hover p,
.cell-1:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-2 {
  background-image: url("https://picsum.photos/225");
}

.cell-2 h3,
.cell-2 p {
  opacity: 0;
}

.cell-2:hover {
  background: var(--highlight-color);
}

.cell-2:hover h3,
.cell-2:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-3 {
  background-image: url("https://picsum.photos/225");
}

.cell-3 h3,
.cell-3 p {
  opacity: 0;
}

.cell-3:hover {
  background: var(--highlight-color);
}

.cell-3:hover h3,
.cell-3:hover p {
  opacity: 1;
  transition: 1000ms
}

.cell-4 {
  grid-column: 3/5;
  grid-row: 2/4;
  padding: 10px;
}

.cell-4:hover {
  background-image: url("https://picsum.photos/460");
  background-repeat: no-repeat;
  background-size: cover;
}

.cell-4:hover p,
.cell-4:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-5 {
  background-image: url("https://picsum.photos/225");
}

.cell-5 h3,
.cell-5 p {
  opacity: 0;
}

.cell-5:hover {
  background: var(--highlight-color);
}

.cell-5:hover h3,
.cell-5:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-6 {
  background-image: url("https://picsum.photos/225");
}

.cell-6 p,
.cell-6 h3 {
  opacity: 0;
}

.cell-6:hover {
  background: var(--highlight-color);
}

.cell-6:hover p,
.cell-6:hover h3 {
  opacity: 1;
  transition: 1000ms;
}

#services-section {
  text-align: center;
  margin-top: 30px;
  max-width: 900px;
  margin: 30px auto;
}

.services-title {
  font-size: 2em;
  text-shadow: 1px 1px var(--text-color);
  color: var(--highlight-color);
}

@media screen and (max-width: 768px) {
  .services-container {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
  }
  .cell-1,
  .cell-2,
  .cell-3,
  .cell-4,
  .cell-5,
  .cell-6 {
    grid-column: auto;
    grid-row: auto;
  }
}
<section id="services-section">
  <h2 class="services-title">Services</h2>
  <div class="services-container">
    <div class="cell cell-1">
      <h3>Creative Web Design Services</h3>
      <p>Our web design services will greatly enhance your business’s presence on the Internet. Our designers mix a potent combination of brand strategy with a generous splash of creative juices and blend in the latest trends in Website UX and UI design.
        Since 2019, S&E has designed and built over 25 Websites from e-commerce, b2c, b2b, non-profit, to social networks. We even create custom web applications.</p>
    </div>
    <div class="cell cell-2">
      <h3>Digital Marketing</h3>
      <p>Our digital marketing services are driven not only by passion, but also a driving desire to deliver exceptional sales conversions.</p>
    </div>
    <div class="cell cell-3">
      <h3>App Development</h3>
      <p>Our app development services are sought after from venture capital start-ups to fortune 100 companies.</p>
    </div>
    <div class="cell cell-4">
      <h3>Website Maintenance</h3>
      <p>Extend your Website’s Lifespan</p>
      <p>By subscribing into a website maintenance plan with S&E, we can help your website keep up with the ever-changing and evolving industry.</p>
      <p>S&E is one of the leading website maintenance agencies. Our web maintenance packages cover everything that your website may need – from simple content updates to extensive design updates.</p>
    </div>
    <div class="cell cell-5">
      <h3>Web Development</h3>
      <p>If you’re looking for custom Internet applications or complex web development solutions, you’ve come to the right place.</p>
    </div>
    <div class="cell cell-6">
      <h3>Branding</h3>
      <p>Branding has never been more expansive, adventurous and agile than it is today</p>
    </div>
  </div>
</section>

Here's a working CodePen: https://codepen.io/chrislafrombois/pen/xxKyYPN

like image 54
disinfor Avatar answered Nov 06 '22 23:11

disinfor


First, you forgot to put the style rule in your media query. You had:

@media screen and (max-width: 768px) {
    grid-template-columns: 1fr;
}

Should be:

@media screen and (max-width: 768px) {
  .services-container {
    grid-template-columns: 1fr;
  }
}

Second, you need to reset the grid-column and grid-row style rules for all of the grid items:

/* variables for colors */

:root {
  --highlight-color: #ff7264;
  --white-color: #fff;
  --text-color: #7f7f7f;
  --dark-bg-color: #2e2e2e;
  --light-bg-color: #fff;
  --gray-bg-color: #666;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}


/* services section */

.services-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(225px, 1fr));
  grid-template-rows: repeat(3, 200px);
  grid-gap: 5px;
  margin: 5px;
}

.cell {
  background: var(--highlight-color);
  text-align: center;
  color: var(--white-color);
  padding: 10px;
}

.cell h3 {
  font-size: 20px;
}

.cell p {
  line-height: 1.4em;
}

.cell-1 {
  grid-column: 1/3;
  grid-row: 1/3;
  padding: 10px;
}

.cell-1 p,
.cell-4 p {
  font-size: 20px;
  line-height: 1.4em;
}

.cell-1 h3,
.cell-4 h3 {
  font-size: 30px;
}

.cell-1:hover {
  background-image: url("https://picsum.photos/460");
}

.cell-1:hover p,
.cell-1:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-2 {
  background-image: url("https://picsum.photos/225");
}

.cell-2 h3,
.cell-2 p {
  opacity: 0;
}

.cell-2:hover {
  background: var(--highlight-color);
}

.cell-2:hover h3,
.cell-2:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-3 {
  background-image: url("https://picsum.photos/225");
}

.cell-3 h3,
.cell-3 p {
  opacity: 0;
}

.cell-3:hover {
  background: var(--highlight-color);
}

.cell-3:hover h3,
.cell-3:hover p {
  opacity: 1;
  transition: 1000ms
}

.cell-4 {
  grid-column: 3/5;
  grid-row: 2/4;
  padding: 10px;
}

.cell-4:hover {
  background-image: url("https://picsum.photos/460");
  background-repeat: no-repeat;
  background-size: cover;
}

.cell-4:hover p,
.cell-4:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-5 {
  background-image: url("https://picsum.photos/225");
}

.cell-5 h3,
.cell-5 p {
  opacity: 0;
}

.cell-5:hover {
  background: var(--highlight-color);
}

.cell-5:hover h3,
.cell-5:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-6 {
  background-image: url("https://picsum.photos/225");
}

.cell-6 p,
.cell-6 h3 {
  opacity: 0;
}

.cell-6:hover {
  background: var(--highlight-color);
}

.cell-6:hover p,
.cell-6:hover h3 {
  opacity: 1;
  transition: 1000ms;
}

#services-section {
  text-align: center;
  margin-top: 30px;
  max-width: 900px;
  margin: 30px auto;
}

.services-title {
  font-size: 2em;
  text-shadow: 1px 1px var(--text-color);
  color: var(--highlight-color);
}

@media screen and (max-width: 768px) {
  .services-container {
    grid-template-columns: 1fr;
  }
  .services-container > div{
     grid-column: auto;
     grid-row: auto;
  }
}
<section id="services-section">
  <h2 class="services-title">Services</h2>
  <div class="services-container">
    <div class="cell cell-1">
      <h3>Creative Web Design Services</h3>
      <p>Our web design services will greatly enhance your business’s presence on the Internet. Our designers mix a potent combination of brand strategy with a generous splash of creative juices and blend in the latest trends in Website UX and UI design.
        Since 2019, S&E has designed and built over 25 Websites from e-commerce, b2c, b2b, non-profit, to social networks. We even create custom web applications.</p>
    </div>
    <div class="cell cell-2">
      <h3>Digital Marketing</h3>
      <p>Our digital marketing services are driven not only by passion, but also a driving desire to deliver exceptional sales conversions.</p>
    </div>
    <div class="cell cell-3">
      <h3>App Development</h3>
      <p>Our app development services are sought after from venture capital start-ups to fortune 100 companies.</p>
    </div>
    <div class="cell cell-4">
      <h3>Website Maintenance</h3>
      <p>Extend your Website’s Lifespan</p>
      <p>By subscribing into a website maintenance plan with S&E, we can help your website keep up with the ever-changing and evolving industry.</p>
      <p>S&E is one of the leading website maintenance agencies. Our web maintenance packages cover everything that your website may need – from simple content updates to extensive design updates.</p>
    </div>
    <div class="cell cell-5">
      <h3>Web Development</h3>
      <p>If you’re looking for custom Internet applications or complex web development solutions, you’ve come to the right place.</p>
    </div>
    <div class="cell cell-6">
      <h3>Branding</h3>
      <p>Branding has never been more expansive, adventurous and agile than it is today</p>
    </div>
  </div>
</section>
like image 28
symlink Avatar answered Nov 06 '22 23:11

symlink


I have tried using a media query to set the grid template columns to 1fr at a break-point and that didn't work.

  1. You didn't specify a selector in your media query. You just posted code, with no where for it to be applied.

    @media screen and (max-width: 768px) {
      grid-template-columns: 1fr; /* selector missing */
    }
    
  2. You didn't reset the column and row spans you defined.

    .cell-1 {
      grid-column: 1/3;
      grid-row: 1/3;
    }
    
    .cell-4 {
      grid-column: 3/5;
      grid-row: 2/4;
    }
    

Try this instead:

@media screen and (max-width: 768px) {

  .services-container {
    grid-template-columns: 1fr;
  }

  .cell-1, .cell-4 {
    grid-column: auto;
    grid-row: auto;
  }
}

/* variables for colors */
:root {
  --highlight-color: #ff7264;
  --white-color: #fff;
  --text-color: #7f7f7f;
  --dark-bg-color: #2e2e2e;
  --light-bg-color: #fff;
  --gray-bg-color: #666;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* services section */
.services-container {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(225px, 1fr));
  grid-template-rows: repeat(3, 200px);
  grid-gap: 5px;
  margin: 5px;
}

.cell {
  background: var(--highlight-color);
  text-align: center;
  color: var(--white-color);
  padding-top: 10px;
}

.cell h3 {
  font-size: 20px;
}

.cell p {
  line-height: 1.4em;
}

.cell-1 {
  grid-column: 1/3;
  grid-row: 1/3;
  padding: 10px;
}

.cell-1 p, .cell-4 p {
  font-size: 20px;
  line-height: 1.4em;
}

.cell-1 h3, .cell-4 h3 {
  font-size: 30px;
}

.cell-1:hover {
  background-image: url("https://picsum.photos/460");
}

.cell-1:hover p, .cell-1:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-2 {
  background-image: url("https://picsum.photos/225");
}

.cell-2 h3, .cell-2 p {
  opacity: 0;
}

.cell-2:hover {
  background: var(--highlight-color);
}

.cell-2:hover h3, .cell-2:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-3 {
  background-image: url("https://picsum.photos/225");
}

.cell-3 h3, .cell-3 p {
  opacity: 0;
}

.cell-3:hover {
  background: var(--highlight-color);
}

.cell-3:hover h3, .cell-3:hover p {
  opacity: 1;
  transition: 1000ms
}

.cell-4 {
  grid-column: 3/5;
  grid-row: 2/4;
  padding: 10px;
}

.cell-4:hover {
  background-image: url("https://picsum.photos/460");
  background-repeat: no-repeat;
  background-size: cover;
}

.cell-4:hover p, .cell-4:hover h3 {
  opacity: 0;
  transition: 1000ms;
}

.cell-5 {
  background-image: url("https://picsum.photos/225");
}

.cell-5 h3, .cell-5 p {
  opacity: 0;
}

.cell-5:hover {
  background: var(--highlight-color);
}

.cell-5:hover h3, .cell-5:hover p {
  opacity: 1;
  transition: 1000ms;
}

.cell-6 {
  background-image: url("https://picsum.photos/225");
}

.cell-6 p, .cell-6 h3 {
  opacity: 0;
}

.cell-6:hover {
  background: var(--highlight-color);
}

.cell-6:hover p, .cell-6:hover h3 {
  opacity: 1;
  transition: 1000ms;
}

#services-section {
  text-align: center;
  margin-top: 30px;
  max-width: 900px;
  margin: 30px auto;
}

.services-title {
  font-size: 2em;
  text-shadow: 1px 1px var(--text-color);
  color: var(--highlight-color);
}

@media ( max-width: 600px ) {
  
  .services-container { grid-template-columns: 1fr; grid-template-rows: auto; }
  
  .cell-1 {
  grid-column: auto;
  grid-row: auto;

}

.cell-4 {
  grid-column: auto;
  grid-row: auto;

}
  
}
<section id="services-section">
  <h2 class="services-title">Services</h2>
  <div class="services-container">
    <div class="cell cell-1">
      <h3>Creative Web Design Services</h3>
      <p>Our web design services will greatly enhance your business’s presence on the Internet. Our designers mix a potent combination of brand strategy with a generous splash of creative juices and blend in the latest trends in Website UX and UI design.
        Since 2019, S&E has designed and built over 25 Websites from e-commerce, b2c, b2b, non-profit, to social networks. We even create custom web applications.</p>
    </div>
    <div class="cell cell-2">
      <h3>Digital Marketing</h3>
      <p>Our digital marketing services are driven not only by passion, but also a driving desire to deliver exceptional sales conversions.</p>
    </div>
    <div class="cell cell-3">
      <h3>App Development</h3>
      <p>Our app development services are sought after from venture capital start-ups to fortune 100 companies.</p>
    </div>
    <div class="cell cell-4">
      <h3>Website Maintenance</h3>
      <p>Extend your Website’s Lifespan</p>
      <p>By subscribing into a website maintenance plan with S&E, we can help your website keep up with the ever-changing and evolving industry.</p>
      <p>S&E is one of the leading website maintenance agencies. Our web maintenance packages cover everything that your website may need – from simple content updates to extensive design updates.</p>
    </div>
    <div class="cell cell-5">
      <h3>Web Development</h3>
      <p>If you’re looking for custom Internet applications or complex web development solutions, you’ve come to the right place.</p>
    </div>
    <div class="cell cell-6">
      <h3>Branding</h3>
      <p>Branding has never been more expansive, adventurous and agile than it is today</p>
    </div>
  </div>
</section>

jsFiddle demo

like image 27
Michael Benjamin Avatar answered Nov 06 '22 22:11

Michael Benjamin