简体   繁体   English

缺少空间时如何使中心元素移动到上面的行?

[英]How do I make the center element move to the line above when lacking space?

I have three elements (divs for simplicity's sake).我有三个元素(为简单起见,div)。 How do I make it so that, when the space is narrowed, the center element pops up to the line above the other two?如何做到这一点,当空间变窄时,中心元素会弹出到其他两个上方的线? I'm using media queries right now, but the center content can change width, so it breaks.我现在正在使用媒体查询,但是中心内容可以改变宽度,所以它会中断。

For example with:例如:

<div class="container">
    <div class="Q">QWERTYUIOP</div>
    <div class="A">ASDFGHJKL0</div>
    <div class="Z">ZXCVBNM123</div>
</div>

When there is extra space it looks like this当有额外的空间时,它看起来像这样

QWERTYUIOP    ASDFGHJKL0    ZXCVBNM123

and when there is limited space it looks like this当空间有限时,它看起来像这样

       ASDFGHJKL0
QWERTYUIOP    ZXCVBNM123

You can achieve this behavior using media queries.您可以使用媒体查询来实现此行为。 So if you want to force the last 2 elements to next line when screen size is less than 560px, your css and html should be like this:因此,如果您想在屏幕尺寸小于 560px 时将最后 2 个元素强制到下一行,您的 css 和 html 应该是这样的:

 .container { display: flex; flex-wrap: wrap; justify-content: space-between; }.nl { display: none; width:100%; } @media only screen and (max-width:560px){.A { order: 1; flex: 0 0 100%; text-align: center; }.Q { order: 2; }.Z { order: 3; } }
 <div class="container"> <div class="Q">QWERTYUIOP</div> <div class="A">ASDFGHJKL0</div> <div class="Z">ZXCVBNM123</div> </div>

I don't know if the author is allowed to use javascript?.不知道作者是否允许使用javascript? ...but I don't know any other solution. ...但我不知道任何其他解决方案。

The css was not shown, so I made my own rules for the main .container . css 没有显示,所以我为主.container制定了自己的规则。 Flex rules are used.使用Flex规则。 Also, I don't know if the class has a .container specific width?!另外,我不知道 class 是否有.container特定宽度?!

In my example, I used the window resize event to visualize how my code works:在我的示例中,我使用了 window 调整大小事件来可视化我的代码是如何工作的:

window.onresize = function() { ... }

but in production it needs to be replaced with a window load event:但在生产中,它需要替换为 window 加载事件:

window.onload = function() { ... }

If this example does not suit the author, then I think that this example will be useful to others:)如果这个例子不适合作者,那么我认为这个例子对其他人有用:)

 window.onresize = function() { let container = document.querySelector('.container'); let Q_div = document.querySelector('.Q'); let A_div = document.querySelector('.A'); let Z_div = document.querySelector('.Z'); let sum_div = Q_div.offsetWidth + A_div.offsetWidth + Z_div.offsetWidth; if (container.offsetWidth <= sum_div) { A_div.classList.add('class_for_A'); } else { A_div.classList.remove('class_for_A'); } }
 .container { display: flex; flex-wrap: wrap-reverse; justify-content: space-between; width: 500px; max-width: 100%; }.A.class_for_A { order: 1; margin: auto; }
 <div class="container"> <div class="Q">QWERTYUIOP</div> <div class="A">ASDFGHJKL0</div> <div class="Z">ZXCVBNM123</div> </div>

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

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