简体   繁体   English

结合最小宽度和最大宽度

[英]Combining min-width and max-width

I want div1 to appear only when the window width is less than 800px, and I want div2 to appear only when the window width is greater than 800px. 我希望div1仅在窗口宽度小于800px时出现,并且我希望div2仅在窗口宽度大于800px时出现。 My solution is to use the following CSS which works. 我的解决方案是使用下面的CSS。 However, is there a way to do this using only one @media command? 但是,有没有办法仅使用一个@media命令来执行此操作? It seems clumsy to have to write two conditions, one for a max-width , and one for a min-width . 必须写两个条件似乎很笨拙,一个条件为max-width ,一个条件为min-width

 @media screen and (max-width: 800px) { #div1 { display: block; } #div2 { display: none; } } @media screen and (min-width: 800px) { #div1 { display: none; } #div2 { display: block; } } 
 <div id="div1"> DIV1 </div> <div id="div2"> DIV2 </div> 

Take one of the set out of media queries. 从媒体查询中选择其中之一。

 #div1 { display: none; } #div2 { display: block; } @media screen and (max-width: 800px) { #div1 { display: block; } #div2 { display: none; } } 
 <div id="div1"> DIV1 </div> <div id="div2"> DIV2 </div> 

Or 要么

 #div1 { display: block; } #div2 { display: none; } @media screen and (min-width: 800px) { #div1 { display: none; } #div2 { display: block; } } 
 <div id="div1"> DIV1 </div> <div id="div2"> DIV2 </div> 

There is a way to combine media queries try: 有一种组合媒体查询的方法:

@media only screen and (max-width: 600px) and (min-width: 400px) {

The query above will trigger only for screens that are 600-400px wide. 上面的查询仅会触发600-400px宽的屏幕。 This can be used to target specific devices with known widths. 这可用于瞄准具有已知宽度的特定设备。

Or if you only want to target screen sizes that are 800px or less use: 或者,如果您只想定位800像素或更小的屏幕尺寸,请使用:

@media screen and (max-width: 800px) {
replace @media screen and (max-width: 800px) { #div1 {
display: block;
}
#div2 {
display: none;
} }

with  #div1 {
display: block;
}
#div2 {
display: none;
}

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

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