【问题标题】:How do I make CSS grid stack on mobile?如何在移动设备上制作 CSS 网格堆栈?
【发布时间】:2020-01-19 16:34:23
【问题描述】:

我有一个 CSS 网格,它的网格模板列设置为 grid-template-columns: repeat(auto-fit, minmax(225px, 1fr))

当我检查网站对移动设备的响应能力时,网格元素会变小并且不会堆叠。我试图让它们在移动设备上堆叠成 1fr 列。我尝试使用媒体查询在断点处将网格模板列设置为 1fr,但没有成功。

这是我所拥有的 CodePen 的链接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>

【问题讨论】:

    标签: html css css-grid


    【解决方案1】:

    首先,您忘记将样式规则放入媒体查询中。你有:

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

    应该是:

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

    其次,您需要为所有网格项重置grid-columngrid-row 样式规则:

    /* 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>

    【讨论】:

      【解决方案2】:

      我尝试使用媒体查询在断点处将网格模板列设置为 1fr,但没有奏效。

      1. 您没有在媒体查询中指定选择器。您只是发布了代码,没有应用它的地方。

        @media screen and (max-width: 768px) {
          grid-template-columns: 1fr; /* selector missing */
        }
        
      2. 您没有重置您定义的列和行跨度。

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

      试试这个:

      @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

      【讨论】:

        【解决方案3】:

        最大的问题之一是您的媒体查询 - 您没有指定要应用该属性的任何元素。

        这是我更改的 CSS 的重要部分:

        @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;
          }
        }
        

        您可以看到,我还将您的所有.cell 重置为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 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>

        这是一个有效的 CodePen: https://codepen.io/chrislafrombois/pen/xxKyYPN

        【讨论】:

        • 由于网格项始终是网格容器的直接子项,因此它们也可以将第二个样式规则写为.services-container &gt; div
        • 是的,这是个好电话。我想我主要是想弄清楚实际标记发生了什么。我也可以做到.services-container &gt; .cell...比元素选择器更具体一点,但我离题了。
        猜你喜欢
        • 2015-09-28
        • 1970-01-01
        • 2017-03-02
        • 1970-01-01
        • 2010-11-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多