简体   繁体   English

我在一行中有3个div,如何在移动设备中使中间div排在第一位,第二个div排在第一位,第三个div排在最后?

[英]I have 3 divs in one row, how do I get the middle div comes first and first div comes in second and third div comes last in mobile?

I have 3 divs in one row, how do I get the middle div comes first and first div comes in second and third div come last in mobile? 我在一行中有3个div,如何使中间div排在第一位,第一div排在第二个,第三div在移动排在最后?

<div class="first">hello first</div>
<div class="middle">hello Middle</div>
<div class="last">hello last</div>

.first {float:left; width:33%;}

.middle {float:left; width:33%;}

.last {float:left; width:33%;}

If you're using Bootstrap as per you question, there's also a way to specify div ordering on per breakpoint basis, using Bootstrap's pull/push classes. 如果您按自己的方式使用Bootstrap,则还有一种方法可以使用Bootstrap的pull / push类在每个断点的基础上指定div顺序。 Assumed you set the right order as it should be on mobile, first: 假设您设置了正确的顺序(应在移动设备上进行设置),首先:

<div class="row">
  <div class="middle col-md-4 col-md-push-4 col-sm-4">hello middle</div>
  <div class="first col-md-4 col-sm-4 col-md-pull-4 ">hello first</div>
  <div class="last col-md-4 col-sm-4">hello last</div>
</div>

http://jsfiddle.net/ximes/dw8n36jj/ http://jsfiddle.net/ximes/dw8n36jj/

See: http://getbootstrap.com/css/#grid-column-ordering for more info/examples. 有关更多信息/示例,请参见: http//getbootstrap.com/css/#grid-column-ordering

  • If you give float:left to all these divs, they will display respectively in the order they are written in your HTML. 如果为所有这些div赋予float:left ,则它们将按照在HTML中编写的顺序分别显示。
  • To make divs display in a one row you need to display them as inline-block because they are dispalyed as block by default. 要使div在一行中显示,您需要将它们显示为inline-block因为默认情况下它们以block形式显示。

Solution: 解:

You just need to remove float:left; 您只需要删除float:left; from first and last elements and use display:inline-block with all these divs, it will give you the expected results even on mobile. first last元素到last元素,并在所有这些div中使用display:inline-block ,即使在移动设备上,它也可以为您带来预期的效果。

Demo: 演示:

This is a sample working Demo: 这是一个示例演示示例:

 .first { display: inline-block; width: 33%; } .middle { display: inline-block; ; float: left; width: 33%; } .last { display: inline-block; width: 33%; } 
 <div class="first">hello first</div> <div class="middle">hello Middle</div> <div class="last">hello last</div> 

You don't need the extra CSS to set the widths. 您不需要额外的CSS来设置宽度。 Just use the Bootstrap grid and column ordering.. 只需使用Bootstrap网格和列顺序即可。

http://www.codeply.com/go/eMKtd3qzYU http://www.codeply.com/go/eMKtd3qzYU

<div class="container">
    <div class="row">
        <div class="middle col-sm-4 col-sm-push-4">hello Middle</div>
        <div class="first col-sm-4 col-sm-pull-4">hello first</div>
        <div class="laste col-sm-4">hello last</div>
    </div>
</div>
<div class="first">hello first</div>
<div class="middle">hello Middle</div>
<div class="last">hello last</div>
.first {display:inline-block; width:33%;}
.middle {float:left; width:33%;}
.last {display:inline-block; width:33%;}

https://jsfiddle.net/gaLo5s1o/ https://jsfiddle.net/gaLo5s1o/

如果使用的是引导,然后使用col-sm-push-4的第二divcol-sm-pull-4的第一div

Here is a sample code using flexbox approach . 这是使用flexbox方法的示例代码。

 .parent{ display:flex; flex-direction:row;} .parent div{ height:200px;width:33%;} .first { background:red;} .middle { background:yellow;} .last { background:blue;} @media screen and (max-width: 768px) { .parent { flex-direction: row; } .parent div{ width:100%; } .first { order: 2; } .second{ order: 1; } .third{ order:3} } 
 <div class="parent"> <div class="first">hello first</div> <div class="middle">hello Middle</div> <div class="last">hello last</div> </div> 

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

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