简体   繁体   English

隐藏/显示Div取决于屏幕宽度

[英]Hide/Show Div Depending on Screen Width

I'm currently using CSS to hide a div section when the screen goes under a certain width. 当前屏幕宽度不足时,我正在使用CSS隐藏div部分。 That bit works fine, but I'm struggling to get my head round, how to show a replacement div when I hide the div. 那个工作正常,但我正在努力使自己回避,如何在隐藏div时显示替换div。

I might be going about it the wrong way, but here's my code: 我可能会以错误的方式进行操作,但这是我的代码:

@media all and (max-width: 955px) { 
    div.grid12-11 {
        display: none;
    }
}

@media all and (max-width: 954px) {
    div.grid12-12 {
        display: block;
    }
}

What I'm trying to achieve is when the screen width goes below 955px grid12-11 is hidden and replaced with grid12-12. 我要实现的是,当屏幕宽度低于955px时,grid12-11被隐藏并替换为grid12-12。

Currently both the div's are being shown > 955px rather than replacing the block. 当前,两个div的显示都大于955px,而不是替换该块。

Is there a better/correct way to doing this? 有没有更好/正确的方法来做到这一点?

There is no need to change the media query 无需更改媒体查询

   div.grid12-12 {
       display: none; // make it hidden on default view
   }

   @media all and (max-width: 955px) { 
        div.grid12-11 {
            display: none;
        }
         div.grid12-12 {
            display: block;
        }
    }

Check this: http://jsfiddle.net/Mohamed_nabil/cHQcD/ 检查此: http : //jsfiddle.net/Mohamed_nabil/cHQcD/

@media (max-width: 955px) { 
    div.gridFirst {
        display: none;
    }
}

@media (min-width: 955px) {
    div.gridSecond {
        display: none;
    }
}

How are your initial rules set up? 您的初始规则如何设置? I think you're running into a problem of specificity. 我认为您遇到了特异性问题。 If your 'standard' rules are the same or more specific than your rules in your media query, the media query will not override them. 如果您的“标准”规则与媒体查询中的规则相同或更具体,则媒体查询将不会覆盖它们。

This seems to do what you expect: 这似乎可以达到您的期望:

@media all and (max-width: 955px) { 
    div.grid12-11 {
        display: none;
    }
    div.grid12-12 {
        display: block;
    }
}

.grid12-11 { display:block; }
.grid12-12 { display:none; }

Note that if your standard rules (non-media) are prefixed with "div", it will NOT function (depending on what order the rules are processed in, and which browser you are using). 请注意,如果您的标准规则(非媒体)以“ div”为前缀,则该规则将不起作用(取决于处理规则的顺序以及所使用的浏览器)。

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

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