如何在移动设备上制作 CSS 网格堆栈?

swi*_*n75 5 html css css-grid

我有一个 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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

dis*_*for 4

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

\n\n

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

\n\n
@media screen and (max-width: 768px) {\n  .services-container {\n      grid-template-columns: 1fr;\n      grid-template-rows: 1fr;\n   }\n\n  .cell-1, .cell-2, .cell-3, .cell-4, .cell-5, .cell-6 {\n    grid-column: auto;\n    grid-row: auto;\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

你可以看到,我也重置了你所有的.cell位置auto

\n\n

\r\n
\r\n
/* variables for colors */\r\n\r\n:root {\r\n  --highlight-color: #ff7264;\r\n  --white-color: #fff;\r\n  --text-color: #7f7f7f;\r\n  --dark-bg-color: #2e2e2e;\r\n  --light-bg-color: #fff;\r\n  --gray-bg-color: #666;\r\n}\r\n\r\n* {\r\n  margin: 0;\r\n  padding: 0;\r\n  box-sizing: border-box;\r\n}\r\n\r\n\r\n/* services section */\r\n\r\n.services-container {\r\n  display: grid;\r\n  grid-template-columns: repeat(auto-fit, minmax(225px, 1fr));\r\n  grid-template-rows: repeat(3, 200px);\r\n  grid-gap: 5px;\r\n  margin: 5px;\r\n}\r\n\r\n.cell {\r\n  background: var(--highlight-color);\r\n  text-align: center;\r\n  color: var(--white-color);\r\n  padding-top: 10px;\r\n}\r\n\r\n.cell h3 {\r\n  font-size: 20px;\r\n}\r\n\r\n.cell p {\r\n  line-height: 1.4em;\r\n}\r\n\r\n.cell-1 {\r\n  grid-column: 1/3;\r\n  grid-row: 1/3;\r\n  padding: 10px;\r\n}\r\n\r\n.cell-1 p,\r\n.cell-4 p {\r\n  font-size: 20px;\r\n  line-height: 1.4em;\r\n}\r\n\r\n.cell-1 h3,\r\n.cell-4 h3 {\r\n  font-size: 30px;\r\n}\r\n\r\n.cell-1:hover {\r\n  background-image: url("https://picsum.photos/460");\r\n}\r\n\r\n.cell-1:hover p,\r\n.cell-1:hover h3 {\r\n  opacity: 0;\r\n  transition: 1000ms;\r\n}\r\n\r\n.cell-2 {\r\n  background-image: url("https://picsum.photos/225");\r\n}\r\n\r\n.cell-2 h3,\r\n.cell-2 p {\r\n  opacity: 0;\r\n}\r\n\r\n.cell-2:hover {\r\n  background: var(--highlight-color);\r\n}\r\n\r\n.cell-2:hover h3,\r\n.cell-2:hover p {\r\n  opacity: 1;\r\n  transition: 1000ms;\r\n}\r\n\r\n.cell-3 {\r\n  background-image: url("https://picsum.photos/225");\r\n}\r\n\r\n.cell-3 h3,\r\n.cell-3 p {\r\n  opacity: 0;\r\n}\r\n\r\n.cell-3:hover {\r\n  background: var(--highlight-color);\r\n}\r\n\r\n.cell-3:hover h3,\r\n.cell-3:hover p {\r\n  opacity: 1;\r\n  transition: 1000ms\r\n}\r\n\r\n.cell-4 {\r\n  grid-column: 3/5;\r\n  grid-row: 2/4;\r\n  padding: 10px;\r\n}\r\n\r\n.cell-4:hover {\r\n  background-image: url("https://picsum.photos/460");\r\n  background-repeat: no-repeat;\r\n  background-size: cover;\r\n}\r\n\r\n.cell-4:hover p,\r\n.cell-4:hover h3 {\r\n  opacity: 0;\r\n  transition: 1000ms;\r\n}\r\n\r\n.cell-5 {\r\n  background-image: url("https://picsum.photos/225");\r\n}\r\n\r\n.cell-5 h3,\r\n.cell-5 p {\r\n  opacity: 0;\r\n}\r\n\r\n.cell-5:hover {\r\n  background: var(--highlight-color);\r\n}\r\n\r\n.cell-5:hover h3,\r\n.cell-5:hover p {\r\n  opacity: 1;\r\n  transition: 1000ms;\r\n}\r\n\r\n.cell-6 {\r\n  background-image: url("https://picsum.photos/225");\r\n}\r\n\r\n.cell-6 p,\r\n.cell-6 h3 {\r\n  opacity: 0;\r\n}\r\n\r\n.cell-6:hover {\r\n  background: var(--highlight-color);\r\n}\r\n\r\n.cell-6:hover p,\r\n.cell-6:hover h3 {\r\n  opacity: 1;\r\n  transition: 1000ms;\r\n}\r\n\r\n#services-section {\r\n  text-align: center;\r\n  margin-top: 30px;\r\n  max-width: 900px;\r\n  margin: 30px auto;\r\n}\r\n\r\n.services-title {\r\n  font-size: 2em;\r\n  text-shadow: 1px 1px var(--text-color);\r\n  color: var(--highlight-color);\r\n}\r\n\r\n@media screen and (max-width: 768px) {\r\n  .services-container {\r\n    grid-template-columns: 1fr;\r\n    grid-template-rows: 1fr;\r\n  }\r\n  .cell-1,\r\n  .cell-2,\r\n  .cell-3,\r\n  .cell-4,\r\n  .cell-5,\r\n  .cell-6 {\r\n    grid-column: auto;\r\n    grid-row: auto;\r\n  }\r\n}
Run Code Online (Sandbox Code Playgroud)\r\n
<section id="services-section">\r\n  <h2 class="services-title">Services</h2>\r\n  <div class="services-container">\r\n    <div class="cell cell-1">\r\n      <h3>Creative Web Design Services</h3>\r\n      <p>Our web design services will greatly enhance your business\xe2\x80\x99s 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.\r\n        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>\r\n    </div>\r\n    <div class="cell cell-2">\r\n      <h3>Digital Marketing</h3>\r\n      <p>Our digital marketing services are driven not only by passion, but also a driving desire to deliver exceptional sales conversions.</p>\r\n    </div>\r\n    <div class="cell cell-3">\r\n      <h3>App Development</h3>\r\n      <p>Our app development services are sought after from venture capital start-ups to fortune 100 companies.</p>\r\n    </div>\r\n    <div class="cell cell-4">\r\n      <h3>Website Maintenance</h3>\r\n      <p>Extend your Website\xe2\x80\x99s Lifespan</p>\r\n      <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>\r\n      <p>S&E is one of the leading website maintenance agencies. Our web maintenance packages cover everything that your website may need \xe2\x80\x93 from simple content updates to extensive design updates.</p>\r\n    </div>\r\n    <div class="cell cell-5">\r\n      <h3>Web Development</h3>\r\n      <p>If you\xe2\x80\x99re looking for custom Internet applications or complex web development solutions, you\xe2\x80\x99ve come to the right place.</p>\r\n    </div>\r\n    <div class="cell cell-6">\r\n      <h3>Branding</h3>\r\n      <p>Branding has never been more expansive, adventurous and agile than it is today</p>\r\n    </div>\r\n  </div>\r\n</section>
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

这是一个正在运行的 CodePen:\n https://codepen.io/chrislafrombois/pen/xxKyYPN

\n