简体   繁体   English

如何使用媒体查询显示/隐藏元素?

[英]How to show/hide elements using media query?

By using media query I would like to show the last element of the following list (2) when the screen width is above 768px. 通过使用媒体查询,我希望在屏幕宽度大于768px时显示以下列表(2)的最后一个元素。

Here is the jsfiddle like . 这是jsfiddle之类的
Here is the basic css code (1). 这是基本的CSS代码(1)。


(1) (1)

@media only screen and (min-width: 480px) {
    .menu li.hide{    
        visibility: none;
    }
}

@media only screen and (min-width: 768px) {
    .menu li.hide{    
        visibility: visible;
    }
}

(2) (2)

<ul class="menu">
    <li>One</li>
    <li class='hide'>Nine</li>
</ul>​

Try this - DEMO 试试这个-DEMO

@media only screen and (max-width: 768px) {
    body{
        background:#030;
    }
    .menu li{    
        font:12px Verdana;    
        width:100px;
        padding-left:10px;
    }
    .menu li:last-child{    
        display: none
    }
}

@media only screen and (min-width: 769px) {
    body{
        background: honeydew;
    }
    .menu li{    
        font:22px Verdana;    
        width:200px;
        padding-left:20px;
    }
}

您可以在媒体查询中设置最小和最大宽度:

 @media screen and (min-width: 480px) and (max-width: 768px) {

I've updated your jsfiddle: http://jsfiddle.net/yyQ5u/28/ 我已经更新了您的jsfiddle: http : //jsfiddle.net/yyQ5u/28/

It looks like the main problem was with the order of things and the actual media queries. 看起来主要问题在于事物的顺序和实际的媒体查询。 I put your non-conditional CSS at the top of the CSS pane and the media queries below. 我将您的无条件CSS放在CSS窗格的顶部,并在下面的媒体查询中。

Then I tweaked the media queries which seemed to sort of cover each other: 然后,我调整了似乎相互掩盖的媒体查询:

@media only screen and (max-width: 768px) {

and

@media only screen and (min-width: 768px) {

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

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