我有一个CSS网格,它的网格模板列设置为grid-template-columns: repeat(auto-fit, minmax(225px, 1fr))。
当我检查移动站点的响应性时,网格元素就会变得更小,而不是堆叠。我试图让他们在移动上堆叠成一个1fr栏。我尝试使用媒体查询在断点将网格模板列设置为1 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>
发布于 2019-09-18 17:05:53
最大的问题之一是您的媒体查询-您没有指定任何元素应用该属性。
下面是我更改的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
发布于 2019-09-18 17:09:24
首先,您忘记将样式规则放在媒体查询中。你有:
@media screen and (max-width: 768px) {
grid-template-columns: 1fr;
}应:
@media screen and (max-width: 768px) {
.services-container {
grid-template-columns: 1fr;
}
}其次,您需要重置所有网格项的grid-column和grid-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>
发布于 2019-09-18 17:06:31
我尝试使用媒体查询在断点将网格模板列设置为1 1fr,但这不起作用。
试一试:
@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>
https://stackoverflow.com/questions/57997501
复制相似问题