简体   繁体   English

浮动div从左到右到顶部和底部,其中左div位于底部,而右div位于顶部

[英]Floating div from left to right to top and bottom where left div sit bottom and right div top

I have 2 divs next to each other and on ipad(portrait) i want the right div to display on top of left div. 我有2个div彼此相邻,在ipad(portrait)上,我希望右div显示在左div的顶部。

Code: 码:

 <div class="main">
 <div class="both">
   <div class="left-go-bottom"></div>
   <div class="right-go-top"></div>
 </div>
 </div>

 <div class="main">
 <div class="both">
   <div class="left-go-bottom"></div>
   <div class="right-go-top"></div>
 </div>
 </div>

 <div class="main">
 <div class="both">
   <div class="left-go-bottom"></div>
   <div class="right-go-top"></div>
 </div>
 </div>

Css: CSS:

.both {
   width: 100%;
   display: -webkit-inline-box;
}

.left-go-bottom, right-go-top {
   height: 769px;
   width: 50%;
   background-color: darkblue;
}

.main {
  display: block;
  width: 100%;
  height: 769px;
}

The first div class .left-go-bottom is first and wil be on the left the second div class right-go-top is second and wil float on right side. 第一个div类.left-go-bottom位于第一个位置,位于左侧;第二个div类的go-go-top位于第二个位置,位于右侧。

For my media query at the break point i added. 对于我在断点处的媒体查询,我添加了。

Css: CSS:

@media only screen and (max-device-width: 1024px) and 
  (min-device-width: 768px) and (orientation: portrait) {

.left-go-bottom, .right-go-top {
    width: 100%;
    float: left;
    display: block;
 }

}

So it works fine, but the div on the top i need on the bottom and the one currently bottom i need top, i tried floating the top one right and bottom left , did not work, any help please. 因此它可以正常工作,但我需要在顶部的div底部和当前需要底部的div顶部,我试图将顶部的div浮动到顶部和底部的左浮动,没有用,请提供任何帮助。

you can use display:flex; 您可以使用display:flex; on .both and then in the media query use flex-direction:column-reverse this will reverse the order of your boxes .both ,然后在media query使用flex-direction:column-reverse这将颠倒盒子的顺序

i have added background:red to the right-go-top div for example purposes. 为了示例,我在right-go-top div中添加了background:red also changed the media query for the same reason ( it will work the same with your media query ) 出于相同的原因也更改了media query (它将与您的media query相同)

see snippet below or jsfiddle > jsfiddle flex 参见下面的片段或jsfiddle> jsfiddle flex

let me know if it helps 让我知道是否有帮助

 .both { width: 100%; display:flex; } .left-go-bottom,.right-go-top { height: 769px; width:50%; } .left-go-bottom{ background-color: darkblue; } .right-go-top{ background-color: red; } @media only screen and (max-width: 768px) { .left-go-bottom, .right-go-top { width: 100%; float: left; display: block; } .both { flex-direction:column-reverse } } 
 <div class="both"> <div class="left-go-bottom"></div> <div class="right-go-top"></div> </div> 

EDIT with new code from OP 使用OP中的新代码进行编辑

OPTION 1 : delete the height from .main . 选项1:.main删除height because you have set a fixed height of 769px on the main s content ( right and left div ) so .main will inherit the height from the content . 因为您已在main的内容(左右div)上设置了769px的固定高度,所以.main将继承content的高度。

so it will have an automatic height of 769px when right and left div are on the same line , and will have a height of 769px*2 = 1538 on mobile version . 因此,当左右div处于同一行时,它将自动具有769px的高度,而在移动版本上它将具有769px * 2 = 1538的高度。 this happens automatically if you don't write a fixed height. 如果您没有写一个固定的高度,这将自动发生。

see snippet below with this solution 请参阅下面的此解决方案摘录

 .both { width: 100%; display:flex; } .main { display: block; width: 100%; /* height:758px removed */ } .left-go-bottom,.right-go-top { height: 769px; width:50%; } .left-go-bottom{ background-color: darkblue; } .left-go-bottom.green { background:green } .right-go-top{ background-color: red; } .right-go-top.yellow{ background-color: yellow; } @media only screen and (max-width: 768px) { .left-go-bottom, .right-go-top { width: 100%; float: left; display: block; } .both { flex-direction:column-reverse; } } 
 <div class="main"> <div class="both"> <div class="left-go-bottom green"></div> <div class="right-go-top yellow"></div> </div> </div> <div class="main"> <div class="both"> <div class="left-go-bottom"></div> <div class="right-go-top"></div> </div> </div> <div class="main"> <div class="both"> <div class="left-go-bottom green"></div> <div class="right-go-top yellow"></div> </div> </div> 

Option B not recommended . 不建议使用选项B。 but you can set a fixed height on .main , although it's useless because, as i said in the previous solution, .main inherits its height from the two divs that are inside it and that have fixed height of 769px 但您可以在.main上设置固定高度,尽管它没有用,因为如我在上一个解决方案中所说, .main从其内部的两个div继承其高度,并且它们的固定高度为769px

but if you really want to. 但是如果您真的想要。 you can set height 769px on desktop version, and 1538px when the divs go on different lines. 您可以在桌面版本上将高度设置为769px,当div位于不同的行上时可以设置为1538px。 ( their height combined ) (他们的身高合计)

see snippet below : 请参见下面的代码段:

 .both { width: 100%; display:flex; } .main { display: block; width: 100%; height: 769px; } .left-go-bottom,.right-go-top { height: 769px; width:50%; } .left-go-bottom{ background-color: darkblue; } .left-go-bottom.green { background:green } .right-go-top{ background-color: red; } .right-go-top.yellow{ background-color: yellow; } @media only screen and (max-width: 768px) { .left-go-bottom, .right-go-top { width: 100%; float: left; display: block; } .both { flex-direction:column-reverse; } .main { height: 1538px; /* added */ } } 
 <div class="main"> <div class="both"> <div class="left-go-bottom green"></div> <div class="right-go-top yellow"></div> </div> </div> <div class="main"> <div class="both"> <div class="left-go-bottom"></div> <div class="right-go-top"></div> </div> </div> <div class="main"> <div class="both"> <div class="left-go-bottom green"></div> <div class="right-go-top yellow"></div> </div> </div> 

If they don't contain content that can't be doubled, the simplest solution is to create a copy of the right block and hiding or showing it accordingly. 如果它们不包含无法翻倍的内容,则最简单的解决方案是创建正确块的副本并相应地隐藏或显示它。

<div class="both">
    <div class="right-go-top show-only-on-iPad-portrait"></div>
    <div class="left-go-bottom"></div>
    <div class="right-go-top hide-on-iPad-portrait"></div>
</div>


<style>
.show-only-on-iPad-portrait {
    display:none;
}
.hide-on-iPad-portrait {
    display:block;
}

@media only screen and (max-device-width: 1024px) and 
  (min-device-width: 768px) and (orientation: portrait) {

.show-only-on-iPad-portrait {
    display:block;
}
.hide-on-iPad-portrait {
    display:none;
}

}
</style>

This is pretty ugly because it uses absolute position and this is not responsive, but this does the staff 这很丑陋,因为它使用绝对位置并且没有响应,但这确实对工作人员造成了影响。

@media only screen and (max-device-width: 1024px) and 
  (min-device-width: 768px) and (orientation: portrait) {

  .left-go-bottom, .right-go-top {
      width: 100%;
      float: left;
      display: block;
  }

  .left-go-bottom{
      top: 769px;
      position: absolute;
  }

  .right-go-top{
      position: absolute;
      top: 0px; 
  }
}

Thanks to @MihaiT for the help my problem resolved. 感谢@MihaiT的帮助,我的问题得以解决。

This is my answer that works. 这是我的答案。

code: 码:

 <div class="main">
  <div class="both">
    <div class="left-go-bottom"></div>
    <div class="right-go-top"></div>
  </div>
 </div>

 <div class="main">
  <div class="both unique-flex">
   <div class="left-go-bottom"></div>
   <div class="right-go-top"></div>
  </div>
 </div>

 <div class="main">
  <div class="both">
    <div class="left-go-bottom"></div>
    <div class="right-go-top"></div>
  </div>
 </div>

css: CSS:

.main{
    display: block;
    width: 100%;
    height: 769px;
}

.both {
   width: 100%;
   display:-webkit-inline-box;
}

.left-go-bottom,.right-go-top  {
    height: 769px;
    width:50%;
 }


 @media only screen and (max-device-width: 1024px) and
 (min-device-width: 768px) and (orientation: portrait) {

  .left-go-bottom, .right-go-top {
    width: 100%;
    float: left;
    display: block;
  }

  .both {
    display:block;
  }

  .main {
    float: left;
    display: inline-table;
   }

  div.both.unique-flex {
   display: flex;
   flex-direction: column-reverse;
  }
 }

As you can see i made the blocks i want to switch unique to them self and the one on top and bottom used display:block. 如您所见,我将自己要切换的块做成了自己,并在顶部和底部使用了display:block。

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

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