简体   繁体   English

根据屏幕尺寸更改html元素的顺序

[英]Change order of html element depending on screen size

How do you change the order of html elements based on the size of the screen? 如何根据屏幕大小更改html元素的顺序?

For example, on large screen like desktops, the order would be like this: 例如,在台式机这样的大屏幕上,顺序如下:

element1 element2 element3 element1 element2 element3

However, when seeing this on phone, it wouldn't fit the width of the screen. 但是,在手机上看到此内容时,它不适合屏幕的宽度。 So, I would like it to look like this: 因此,我希望它看起来像这样:

element2 在element2
element1 部件1
element3 元素3

Since Div2 is the main div, I would like it to be on the middle on large screens and on top on smartphone screens. 由于Div2是主要的div,因此我希望它在大屏幕上位于中间,在智能手机屏幕上位于顶部。

I am using Foundation as the framework for the website. 我正在使用Foundation作为网站的框架。

Here's an example code: 这是一个示例代码:

<div id="container" class="row medium-up-3">
   <div id="element1" class="column column-block">

   </div>
   <div id="element2" class="column column-block">

   </div>
   <div id="element3" class="column column-block">

   </div>     
</div>

I have spent a lot of time learning html and css so that's all I really know to make a website. 我花了很多时间学习HTML和CSS,这是我真正知道的制作网站的全部。 I have been planning to learn javascript so it would be fine if the solution requires it. 我一直在计划学习javascript,因此如果解决方案需要它会很好。

Here's the CSS way, using flexbox (take a look at this guide to help you get started with flexbox): 这是使用flexbox的CSS方式(请参阅本指南,以帮助您开始使用flexbox):

flex-direction is either row or column (depending on how you want your elements to flow) flex-directionrow还是column (取决于您希望元素如何流动)

Change their order with order (using order: 1 on #element2 will put it at the end) 更改订单order (使用order: 1 #element2上的order: 1放在末尾)

 #container { display: flex; flex-direction: column; } #element2 { order: -1; } 
 <div id="container" class="row medium-up-3"> <div id="element1" class="column column-block"> #1 </div> <div id="element2" class="column column-block"> #2 </div> <div id="element3" class="column column-block"> #3 </div> </div> 

Here's one way you could do it: 这是您可以做到的一种方法:

In CSS you can use a media query to display or hide content: 在CSS中,您可以使用媒体查询来显示或隐藏内容:

@media (max-width:632px){
    #computer{
        display: none;
    }
    #phone{
        display: block;
    }
}

You can then have 2 x html sections for both computers / smartphones for example (If you'd like to have big differences in structure etc. between devices) 然后,您可以在两台计算机/智能手机上使用2个html部分(如果您希望设备之间在结构等方面有很大差异)

Good read on media queries - http://www.adobe.com/devnet/archive/dreamweaver/articles/introducing-media-queries.html 很好地阅读媒体查询-http: //www.adobe.com/devnet/archive/dreamweaver/articles/introducing-media-queries.html

You just simply use class medium-up-12 instead of class on container ID. 您只需要简单地使用medium-up-12类而不是容器ID上的类。 Goo Luck. 祝你好运。

 <div id="container" class="row medium-up-12"> <div id="element1" class="column column-block"> #1 </div> <div id="element2" class="column column-block"> #2 </div> <div id="element3" class="column column-block"> #3 </div> </div> 

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

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