最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to showhide elements using media query? - Stack Overflow

programmeradmin1浏览0评论

By using media query I would like to show the last element of the following list (2) when the screen width is above 768px.

Here is the jsfiddle like.
Here is the basic css code (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)

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

By using media query I would like to show the last element of the following list (2) when the screen width is above 768px.

Here is the jsfiddle like.
Here is the basic css code (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)

<ul class="menu">
    <li>One</li>
    <li class='hide'>Nine</li>
</ul>​
Share Improve this question edited Sep 20, 2012 at 17:48 nana 4,4911 gold badge37 silver badges49 bronze badges asked Sep 20, 2012 at 17:06 Lorraine BernardLorraine Bernard 13.5k23 gold badges85 silver badges138 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 1

Try this - 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;
    }
}

You can set the min and max width on the media query:

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

I've updated your jsfiddle: http://jsfiddle/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.

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) {
发布评论

评论列表(0)

  1. 暂无评论