简体   繁体   English

CSS显示:没有一个不能与媒体查询一起使用

[英]CSS display:none not working with media query

I have a site that I'm trying to make responsive. 我有一个网站正在尝试做出响应。 So I need to hide an anchor when under 850px. 因此,当像素低于850像素时,我需要隐藏锚点。 The code looks roughly like this: 该代码大致如下所示:

<a href="javascript:void(0);" class="sign-up-button" id="tryitnow"><img src="assets/images/tryitnow.png" alt="Try it Now!"></a>
<div id="are-you-a-visionary">
   Bla bla some paragraphs
</div>
<a href="javascript:void(0);" class="sign-up-button" id="tryitnow1"><img src="assets/images/tryitnow.png" alt="Try it Now!"></a>

What I want is, to show the FIRST anchor ( #tryitnow ) by default, and show the other one ( #tryitnow1 ) when the screen is under 850px. 我想要的是,默认情况下显示第一锚( #tryitnow ),并在屏幕低于850px时显示另一个锚( #tryitnow1 )。

In the CSS it reflects: 在CSS中,它反映了:

div#container div#layout div#slot1 a#tryitnow {
  display: inline;
  position: absolute;
  top: 90px;
  right: 75px;
}
div#container div#layout div#slot1 a#tryitnow1 {
  display: none;
}

@media all and (max-width: 850px) {
    div#container div#layout div#slot1 a#tryitnow1 {
      display: inline;
      position: relative;
      margin-right:auto;    
      margin-left:5;    
    }
      div#container div#layout div#slot1 a#tryitnow {
      display: none
    }
 }

However, only of the anchor #tryitnow1 is visible all the time, no matter the screen size. 但是,无论屏幕大小如何, #tryitnow1仅可见锚点#tryitnow1 #tryitnow ALWAYS gets hidden. #tryitnow总是被隐藏。

Why? 为什么? Could you please help? 能否请你帮忙? Thank you! 谢谢!

the thing is, that your css selectors are not pointing right... try the following: 事实是,您的CSS选择器指向的不正确...请尝试以下操作:

 div#container div#layout div#slot1 a#tryitnow { display: inline; position: absolute; top: 90px; right: 75px; } #tryitnow1 { display: none; } @media all and (max-width: 850px) { #tryitnow1 { display: inline; position: relative; margin-right:auto; margin-left:5; } #tryitnow { display: none } } 
 <a href="javascript:void(0);" class="sign-up-button" id="tryitnow"><img src="assets/images/tryitnow.png" alt="Try it Now!"></a> <div id="are-you-a-visionary"> Bla bla some paragraphs </div> <a href="javascript:void(0);" class="sign-up-button" id="tryitnow1"><img src="assets/images/tryitnow.png" alt="Try it Now!"></a> 

Try something like this: Demo 试试这样的: 演示

 #tryitnow {
    display: inline;
    position: absolute;
    top: 90px;
    right: 75px;
     background-color:#ccc;
}
#tryitnow1 {
    display: none;
}
@media handheld, only screen and (max-width: 850px) {
#tryitnow1 {
        display: inline;
        position: relative;
        margin-right:auto;
        margin-left:5;
     background-color:red;
    }
     #tryitnow {
        display: none
    }
}

Just use only the id / class which are needed, use the structure you followed previously where you have some hierarchy.. 只需仅使用所需的id /类,在具有一定层次结构的情况下使用之前遵循的结构即可。

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

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