简体   繁体   English

移动和桌面视图的 CSS 对齐方式不同

[英]CSS alignment different for Mobile and Desktop view

I have this layout :我有这个布局:

 .biggest { height: 500px; width: 50%; background-color: teal; float: left; } .section1 { height: 200px; width: 50%; background-color: white; float: right; } .inside1 { height: 30px; width: 90%; background: deeppink; margin: 20px; } .inside2 { height: 30px; width: 90%; background: forestgreen; margin: 20px; }
 <div> <div class="biggest"></div> <div class="section1"> <div class="inside1"></div> <div class="inside2"></div> </div> </div>

I want to align boxes in this order :pink, teal, and green on mobile view.我想按以下顺序对齐框:移动视图上的粉红色、蓝绿色和绿色。 How to do that?怎么做?

The white box is to keep the inside boxes together.白色的盒子是把里面的盒子放在一起。 I am facing problems while aligning elements inside columns in different order as boxes inside column can't be separated.我在以不同顺序对齐列内的元素时遇到问题,因为列内的框无法分开。 If anyone have idea how to do that, an explanation would be a great help.如果有人知道如何做到这一点,解释将是一个很大的帮助。

不幸的是,因为你对准他们这是不可能的,你把路.biggest之间.inside1.inside2因为.biggest是他们父母的reletive .section1与简单的HTML和CSS,你不能马上显示器是出.section1对于手机在.section1虽然有一种讨厌的方式,我不推荐它你可以先用 js 做到这一点,你应该先获取元素并将其保存在一个变量中,然后尝试将其从它所在的位置删除,然后将其添加到在.inside1旁边,您也可以使用 jquery 并尝试使用.inside1 .detach().append()来分离元素并将其附加到其他地方,您可以在此处阅读有关它的更多信息

By using the grid layout and a media query for the mobile screen you can achieve this.通过对移动屏幕使用网格布局和媒体查询,您可以实现这一点。

 .grid { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: repeat(3, 100px); grid-auto-rows: 100px; grid-gap: 10px; } .pink { background: pink; } .teal { background: teal; grid-row: 1 / -1 } .green { background: green; } @media only screen and (max-width: 480px) { .grid { grid-template-columns: 1fr; } .pink { grid-row: 1; } .teal { grid-row: 2; } }
 <div class="grid"> <div class="teal"></div> <div class="pink"></div> <div class="green"></div> </div>

I hope this helps you to solve the problem.我希望这可以帮助您解决问题。

Here's the link to the codepen .这是codepen的链接。

When inside1 and inside2 are in a different parent container, this isn't possible.inside1inside2位于不同的父容器中时,这是不可能的。 However, one solution is to have all three elements under one parent container, and then use CSS Grid to build the two-column layout as well as re-order at a different screen size.但是,一种解决方案是将所有三个元素都放在一个父容器下,然后使用 CSS Grid 构建两列布局,并在不同的屏幕尺寸下重新排序。

Here's a CodePen demo: https://codepen.io/samsonzhangthesalmon/pen/qBdgZdy这是一个 CodePen 演示: https ://codepen.io/samsonzhangthesalmon/pen/qBdgZdy

Unlike @Prathamesh Koshti's answer, mine preserves the heights of the pink and green blocks using min-content in the grid.与@Prathamesh Kosht​​i 的答案不同,我的使用网格中的min-content保留了粉红色和绿色块的高度。

 .container { display: grid; grid-template-columns: repeat(2, 1fr); grid-template-rows: min-content minmax(min-content, 1fr); } .biggest { height: 500px; width: 100%; background-color: teal; grid-row: 1/3; } .inside1 { height: 30px; width: 100%; background: deeppink; margin: 20px; } .inside2 { height: 30px; width: 100%; background: forestgreen; margin: 20px; } @media (max-width: 600px) { /* example of mobile */ .container { grid-template-columns: 1fr; /* reset to one column. Better than doing this is to put the two-column line in a min-width: 600px query and go mobile-first, but for the sake of clarity I'll overwrite it here */ } .biggest { grid-row: 2/3; } }
 <div class='container'> <div class="biggest"></div> <div class="inside1"></div> <div class="inside2"></div> </div>

2 things: 2件事:

1- always try to use the simplest html and css - makes it a lot easier to debug 2- there's a trick here. 1- 始终尝试使用最简单的 html 和 css - 使调试更容易 2- 这里有一个技巧。 notice how I had to reorder the div's in the html.请注意我必须如何重新排序 html 中的 div。 In this way I can make use of the float property but also have the div's listed in the correct mobile order.通过这种方式,我可以使用 float 属性,但也可以按照正确的移动顺序列出 div。

 body, html { margin: 0; padding: 0; } .biggest { height: 500px; width: 50%; background: teal; float: left; } .inside1, .inside2 { height: 200px; width: 50%; background: pink; float: right; margin-bottom: 100px; } .inside2{ background:green; } @media only screen and (min-width: 600px) { .biggest, .inside1, .inside2 { clear: both; width: 100%; margin-bottom: 100px; } }
 <div class="inside1"></div> <div class="biggest"></div> <div class="inside2"></div>

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

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