简体   繁体   English

如何使用媒体查询在手机上将 3 列 CSS 网格更改为 1 列

[英]How to make 3 column CSS grid change into 1 column on mobiles with media query

I'm using CSS Grid to make text mixed with pictures on the large screens.我正在使用 CSS 网格在大屏幕上将文本与图片混合。 I want them to form a column on mobiles though.不过,我希望他们在手机上形成一个专栏。 Basically 3 columns on desktops and 1 column on mobile devices.桌面上基本上是 3 列,移动设备上是 1 列。 How to make it happen using media query?如何使用媒体查询实现它? I was thinking about finding command for grid to disable while under 768px but don't even know if such a thing exist.我正在考虑在768px以下找到禁用网格的命令,但甚至不知道是否存在这样的东西。

 .history { display: grid; grid-template-columns: 1fr 1fr 1fr; padding: 1em; grid-row-gap: 100px; }.box1 { grid-column: 1/3; }.box2 { grid-column: 3; justify-self: center; }.box3 { grid-column: 1; justify-self: center; }.box4 { grid-column: 2/4; }.box5 { grid-column: 1/3; }.box6 { grid-column: 3; justify-self: center; }.box7 { grid-column: 1/4; }
 <div class="history"> <div class="box1"> <p> The story starts in 2010 with Hartstown </p> </div> <div class="box2"> <img src="images/clubs.jpg" alt="Clubs"> </div> <div class="box3"> <img src="images/clubs1.jpg" alt="Clubs"> </div> <div class="box4"> <h3>Clubs Officialy Merge... </h3> <p>May 2011 saw the Official launch of Hartstown Aldridge Legends X1 in Dalymount Park... </p> </div> <div class="box5"> <h3>Our First Full Season... </h3> <p>We started the 2011 / 2012 Season with Approx 280 registered club altogether we had 18 Teams….. </p> </div> <div class="box6"> <img src="images/logo.jpg" alt="Logo"> </div> <div class="box7"> <h3>Forging Ahead... </h3> <p>We presently have approx 400 Registered Club Players and approx 60 nd 2 Over 35'5 Teams and we are still growing... </p> </div> </div>

 @media only screen and (max-width: 768px) { .history { display: grid; grid-template-columns: 1fr; padding: 1em; grid-row-gap: 100px; } } 

Just add the CSS properties of elements in your page that you want to look like on mobile. 只需在页面中添加要在移动设备上显示的元素的CSS属性即可。 max-width means any device's screen size, less than 768px will show this CSS styles overriding the default styles. max-width表示小于768px像素的任何设备的屏幕尺寸都将显示此CSS样式,以覆盖默认样式。

Making grid-template-columns: 1fr; 制作grid-template-columns: 1fr; will make use of full width of the device screen, which is your requirement. 将使用设备屏幕的整个宽度,这是您的要求。

Please note: Put the @media queries only after the default CSS styles. 请注意:仅在默认CSS样式之后放置@media查询。

I know you have a couple other answers above, and one of which you chose as preferred - but the simplest, most elegant solution, which will save you the headache of having to reset all the boxes , is to declare .history as a block inside your small device media query. 我知道您在上面还有另外两个答案,并且选择了其中一个作为首选-但最简单,最优雅的解决方案是将.history声明为其中的一个块,这将使您不必重新设置所有框 ,这让您头疼您的小型设备媒体查询。 That is it. 这就对了。 One line change. 一线换。

@media only screen and (max-width: 768px) {
    .history {
       display: block;
    }
}

I tested it on your snippet above, and merely changing 'display: grid;' 我在上面的代码段中对其进行了测试,仅更改了“ display:grid;” to 'display: block;' “显示:阻止;” will allow the browser(s) to behave as they normally would, with all block elements stacking. 所有块元素都将堆叠在一起,从而使浏览器能够像往常一样运行。

If you have any further questions, hit me up. 如果您还有其他问题,请联系我。

Add in media query width of screen that should trigger change and add there .history{ grid-template-columns: 1fr; 添加应该触发更改的屏幕的媒体查询宽度,然后在其中添加.history {grid-template-columns:1fr; } }

Then adjust css more if it does not look nice. 如果看起来不太好,请再调整CSS。

Check if there's any grid items spanning, if so get rid of the spanning in your media query.检查是否有任何网格项目跨越,如果有,请消除媒体查询中的跨越。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM