Showing and hiding divs using CSS @media query not working












2















When my browser is at greater than 300px width, my <div id="div1"> is hidden with display: none. However, if the browser screen is less than 300px width, it should be showing with display: inline-block.



Why isn't my <div id="div1"> showing up and displaying on-screen after I adjust the width of the browser less than 300px in my code below:






@media screen and (max-width: 300px){
#div1 { display: inline-block;}
}

#div1 { display: none; }

<!--My HTML-->
<div>
Some content 1
</div>
<div id="div1">
Some Content 2
</div>
<div>
Some content 3
</div>





This JSFiddle offers a small results window where you can adjust the width of the window.










share|improve this question



























    2















    When my browser is at greater than 300px width, my <div id="div1"> is hidden with display: none. However, if the browser screen is less than 300px width, it should be showing with display: inline-block.



    Why isn't my <div id="div1"> showing up and displaying on-screen after I adjust the width of the browser less than 300px in my code below:






    @media screen and (max-width: 300px){
    #div1 { display: inline-block;}
    }

    #div1 { display: none; }

    <!--My HTML-->
    <div>
    Some content 1
    </div>
    <div id="div1">
    Some Content 2
    </div>
    <div>
    Some content 3
    </div>





    This JSFiddle offers a small results window where you can adjust the width of the window.










    share|improve this question

























      2












      2








      2








      When my browser is at greater than 300px width, my <div id="div1"> is hidden with display: none. However, if the browser screen is less than 300px width, it should be showing with display: inline-block.



      Why isn't my <div id="div1"> showing up and displaying on-screen after I adjust the width of the browser less than 300px in my code below:






      @media screen and (max-width: 300px){
      #div1 { display: inline-block;}
      }

      #div1 { display: none; }

      <!--My HTML-->
      <div>
      Some content 1
      </div>
      <div id="div1">
      Some Content 2
      </div>
      <div>
      Some content 3
      </div>





      This JSFiddle offers a small results window where you can adjust the width of the window.










      share|improve this question














      When my browser is at greater than 300px width, my <div id="div1"> is hidden with display: none. However, if the browser screen is less than 300px width, it should be showing with display: inline-block.



      Why isn't my <div id="div1"> showing up and displaying on-screen after I adjust the width of the browser less than 300px in my code below:






      @media screen and (max-width: 300px){
      #div1 { display: inline-block;}
      }

      #div1 { display: none; }

      <!--My HTML-->
      <div>
      Some content 1
      </div>
      <div id="div1">
      Some Content 2
      </div>
      <div>
      Some content 3
      </div>





      This JSFiddle offers a small results window where you can adjust the width of the window.






      @media screen and (max-width: 300px){
      #div1 { display: inline-block;}
      }

      #div1 { display: none; }

      <!--My HTML-->
      <div>
      Some content 1
      </div>
      <div id="div1">
      Some Content 2
      </div>
      <div>
      Some content 3
      </div>





      @media screen and (max-width: 300px){
      #div1 { display: inline-block;}
      }

      #div1 { display: none; }

      <!--My HTML-->
      <div>
      Some content 1
      </div>
      <div id="div1">
      Some Content 2
      </div>
      <div>
      Some content 3
      </div>






      html css media-queries display






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 8:50









      James McTyreJames McTyre

      535




      535
























          5 Answers
          5






          active

          oldest

          votes


















          2














          Because css is read from top to bottom. The rule that is set last, is the one that will be executed, so replace position of media query






          #div1 { display: none; }
          @media screen and (max-width: 300px){
          #div1 { display: inline-block;}
          }

          <!--My HTML-->
          <div>
          Some content 1
          </div>
          <div id="div1">
          Some Content 2
          </div>
          <div>
          Some content 3
          </div>








          share|improve this answer
























          • Thanks for the straightforward answer. I wasn't aware CSS was read in that way prior.

            – James McTyre
            Nov 23 '18 at 2:02



















          2














          Because you need to declare your display: none; first, CSS runs from top to bottom:



          #div1 { 
          display: none;
          }

          @media screen and (max-width: 300px) {
          #div1 {
          display: inline-block;
          }
          }


          Hope this helps!






          share|improve this answer

































            1














            Edit your code as shown below:



            #div1 { display: none; }

            @media screen and (max-width: 300px){
            #div1 { display: inline-block;}
            }





            share|improve this answer































              1














              You need to reverse the order of your cascaded styles



              NB display none is to be over-ridden with display so that it will display at all



              #div1 { display: none; }

              @media screen and (max-width: 300px){
              #div1 { display: inline-block;}
              }





              share|improve this answer































                1














                @media queries do not change specificity. At all. They just either apply the code or ignore it, depending on the provided condition.



                Therefore, your code translates into:



                #div1 { display: inline-block;}
                #div1 { display: none; }


                below width: 300px



                and into



                #div1 { display: none; }


                above it.



                You need to place the @media last if the specificity of the selectors remains the same.






                #div1 { display: none; }

                @media (max-width: 300px){
                #div1 { display: inline-block;}
                }

                <div>
                Some content 1
                </div>
                <div id="div1">
                Some Content 2 - only visible up to max-width:300px
                </div>
                <div>
                Some content 3
                </div>








                share|improve this answer

























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426999%2fshowing-and-hiding-divs-using-css-media-query-not-working%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  2














                  Because css is read from top to bottom. The rule that is set last, is the one that will be executed, so replace position of media query






                  #div1 { display: none; }
                  @media screen and (max-width: 300px){
                  #div1 { display: inline-block;}
                  }

                  <!--My HTML-->
                  <div>
                  Some content 1
                  </div>
                  <div id="div1">
                  Some Content 2
                  </div>
                  <div>
                  Some content 3
                  </div>








                  share|improve this answer
























                  • Thanks for the straightforward answer. I wasn't aware CSS was read in that way prior.

                    – James McTyre
                    Nov 23 '18 at 2:02
















                  2














                  Because css is read from top to bottom. The rule that is set last, is the one that will be executed, so replace position of media query






                  #div1 { display: none; }
                  @media screen and (max-width: 300px){
                  #div1 { display: inline-block;}
                  }

                  <!--My HTML-->
                  <div>
                  Some content 1
                  </div>
                  <div id="div1">
                  Some Content 2
                  </div>
                  <div>
                  Some content 3
                  </div>








                  share|improve this answer
























                  • Thanks for the straightforward answer. I wasn't aware CSS was read in that way prior.

                    – James McTyre
                    Nov 23 '18 at 2:02














                  2












                  2








                  2







                  Because css is read from top to bottom. The rule that is set last, is the one that will be executed, so replace position of media query






                  #div1 { display: none; }
                  @media screen and (max-width: 300px){
                  #div1 { display: inline-block;}
                  }

                  <!--My HTML-->
                  <div>
                  Some content 1
                  </div>
                  <div id="div1">
                  Some Content 2
                  </div>
                  <div>
                  Some content 3
                  </div>








                  share|improve this answer













                  Because css is read from top to bottom. The rule that is set last, is the one that will be executed, so replace position of media query






                  #div1 { display: none; }
                  @media screen and (max-width: 300px){
                  #div1 { display: inline-block;}
                  }

                  <!--My HTML-->
                  <div>
                  Some content 1
                  </div>
                  <div id="div1">
                  Some Content 2
                  </div>
                  <div>
                  Some content 3
                  </div>








                  #div1 { display: none; }
                  @media screen and (max-width: 300px){
                  #div1 { display: inline-block;}
                  }

                  <!--My HTML-->
                  <div>
                  Some content 1
                  </div>
                  <div id="div1">
                  Some Content 2
                  </div>
                  <div>
                  Some content 3
                  </div>





                  #div1 { display: none; }
                  @media screen and (max-width: 300px){
                  #div1 { display: inline-block;}
                  }

                  <!--My HTML-->
                  <div>
                  Some content 1
                  </div>
                  <div id="div1">
                  Some Content 2
                  </div>
                  <div>
                  Some content 3
                  </div>






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 22 '18 at 8:55









                  Hiren VaghasiyaHiren Vaghasiya

                  3,3581519




                  3,3581519













                  • Thanks for the straightforward answer. I wasn't aware CSS was read in that way prior.

                    – James McTyre
                    Nov 23 '18 at 2:02



















                  • Thanks for the straightforward answer. I wasn't aware CSS was read in that way prior.

                    – James McTyre
                    Nov 23 '18 at 2:02

















                  Thanks for the straightforward answer. I wasn't aware CSS was read in that way prior.

                  – James McTyre
                  Nov 23 '18 at 2:02





                  Thanks for the straightforward answer. I wasn't aware CSS was read in that way prior.

                  – James McTyre
                  Nov 23 '18 at 2:02













                  2














                  Because you need to declare your display: none; first, CSS runs from top to bottom:



                  #div1 { 
                  display: none;
                  }

                  @media screen and (max-width: 300px) {
                  #div1 {
                  display: inline-block;
                  }
                  }


                  Hope this helps!






                  share|improve this answer






























                    2














                    Because you need to declare your display: none; first, CSS runs from top to bottom:



                    #div1 { 
                    display: none;
                    }

                    @media screen and (max-width: 300px) {
                    #div1 {
                    display: inline-block;
                    }
                    }


                    Hope this helps!






                    share|improve this answer




























                      2












                      2








                      2







                      Because you need to declare your display: none; first, CSS runs from top to bottom:



                      #div1 { 
                      display: none;
                      }

                      @media screen and (max-width: 300px) {
                      #div1 {
                      display: inline-block;
                      }
                      }


                      Hope this helps!






                      share|improve this answer















                      Because you need to declare your display: none; first, CSS runs from top to bottom:



                      #div1 { 
                      display: none;
                      }

                      @media screen and (max-width: 300px) {
                      #div1 {
                      display: inline-block;
                      }
                      }


                      Hope this helps!







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 22 '18 at 9:04









                      Allan Jebaraj

                      450211




                      450211










                      answered Nov 22 '18 at 8:59









                      Ben SwindellsBen Swindells

                      1809




                      1809























                          1














                          Edit your code as shown below:



                          #div1 { display: none; }

                          @media screen and (max-width: 300px){
                          #div1 { display: inline-block;}
                          }





                          share|improve this answer




























                            1














                            Edit your code as shown below:



                            #div1 { display: none; }

                            @media screen and (max-width: 300px){
                            #div1 { display: inline-block;}
                            }





                            share|improve this answer


























                              1












                              1








                              1







                              Edit your code as shown below:



                              #div1 { display: none; }

                              @media screen and (max-width: 300px){
                              #div1 { display: inline-block;}
                              }





                              share|improve this answer













                              Edit your code as shown below:



                              #div1 { display: none; }

                              @media screen and (max-width: 300px){
                              #div1 { display: inline-block;}
                              }






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '18 at 8:54









                              Ozturk Can GokkayaOzturk Can Gokkaya

                              2695




                              2695























                                  1














                                  You need to reverse the order of your cascaded styles



                                  NB display none is to be over-ridden with display so that it will display at all



                                  #div1 { display: none; }

                                  @media screen and (max-width: 300px){
                                  #div1 { display: inline-block;}
                                  }





                                  share|improve this answer




























                                    1














                                    You need to reverse the order of your cascaded styles



                                    NB display none is to be over-ridden with display so that it will display at all



                                    #div1 { display: none; }

                                    @media screen and (max-width: 300px){
                                    #div1 { display: inline-block;}
                                    }





                                    share|improve this answer


























                                      1












                                      1








                                      1







                                      You need to reverse the order of your cascaded styles



                                      NB display none is to be over-ridden with display so that it will display at all



                                      #div1 { display: none; }

                                      @media screen and (max-width: 300px){
                                      #div1 { display: inline-block;}
                                      }





                                      share|improve this answer













                                      You need to reverse the order of your cascaded styles



                                      NB display none is to be over-ridden with display so that it will display at all



                                      #div1 { display: none; }

                                      @media screen and (max-width: 300px){
                                      #div1 { display: inline-block;}
                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 22 '18 at 8:54









                                      Carol McKayCarol McKay

                                      1,9011711




                                      1,9011711























                                          1














                                          @media queries do not change specificity. At all. They just either apply the code or ignore it, depending on the provided condition.



                                          Therefore, your code translates into:



                                          #div1 { display: inline-block;}
                                          #div1 { display: none; }


                                          below width: 300px



                                          and into



                                          #div1 { display: none; }


                                          above it.



                                          You need to place the @media last if the specificity of the selectors remains the same.






                                          #div1 { display: none; }

                                          @media (max-width: 300px){
                                          #div1 { display: inline-block;}
                                          }

                                          <div>
                                          Some content 1
                                          </div>
                                          <div id="div1">
                                          Some Content 2 - only visible up to max-width:300px
                                          </div>
                                          <div>
                                          Some content 3
                                          </div>








                                          share|improve this answer






























                                            1














                                            @media queries do not change specificity. At all. They just either apply the code or ignore it, depending on the provided condition.



                                            Therefore, your code translates into:



                                            #div1 { display: inline-block;}
                                            #div1 { display: none; }


                                            below width: 300px



                                            and into



                                            #div1 { display: none; }


                                            above it.



                                            You need to place the @media last if the specificity of the selectors remains the same.






                                            #div1 { display: none; }

                                            @media (max-width: 300px){
                                            #div1 { display: inline-block;}
                                            }

                                            <div>
                                            Some content 1
                                            </div>
                                            <div id="div1">
                                            Some Content 2 - only visible up to max-width:300px
                                            </div>
                                            <div>
                                            Some content 3
                                            </div>








                                            share|improve this answer




























                                              1












                                              1








                                              1







                                              @media queries do not change specificity. At all. They just either apply the code or ignore it, depending on the provided condition.



                                              Therefore, your code translates into:



                                              #div1 { display: inline-block;}
                                              #div1 { display: none; }


                                              below width: 300px



                                              and into



                                              #div1 { display: none; }


                                              above it.



                                              You need to place the @media last if the specificity of the selectors remains the same.






                                              #div1 { display: none; }

                                              @media (max-width: 300px){
                                              #div1 { display: inline-block;}
                                              }

                                              <div>
                                              Some content 1
                                              </div>
                                              <div id="div1">
                                              Some Content 2 - only visible up to max-width:300px
                                              </div>
                                              <div>
                                              Some content 3
                                              </div>








                                              share|improve this answer















                                              @media queries do not change specificity. At all. They just either apply the code or ignore it, depending on the provided condition.



                                              Therefore, your code translates into:



                                              #div1 { display: inline-block;}
                                              #div1 { display: none; }


                                              below width: 300px



                                              and into



                                              #div1 { display: none; }


                                              above it.



                                              You need to place the @media last if the specificity of the selectors remains the same.






                                              #div1 { display: none; }

                                              @media (max-width: 300px){
                                              #div1 { display: inline-block;}
                                              }

                                              <div>
                                              Some content 1
                                              </div>
                                              <div id="div1">
                                              Some Content 2 - only visible up to max-width:300px
                                              </div>
                                              <div>
                                              Some content 3
                                              </div>








                                              #div1 { display: none; }

                                              @media (max-width: 300px){
                                              #div1 { display: inline-block;}
                                              }

                                              <div>
                                              Some content 1
                                              </div>
                                              <div id="div1">
                                              Some Content 2 - only visible up to max-width:300px
                                              </div>
                                              <div>
                                              Some content 3
                                              </div>





                                              #div1 { display: none; }

                                              @media (max-width: 300px){
                                              #div1 { display: inline-block;}
                                              }

                                              <div>
                                              Some content 1
                                              </div>
                                              <div id="div1">
                                              Some Content 2 - only visible up to max-width:300px
                                              </div>
                                              <div>
                                              Some content 3
                                              </div>






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Nov 22 '18 at 9:04

























                                              answered Nov 22 '18 at 8:56









                                              Andrei GheorghiuAndrei Gheorghiu

                                              35.5k74774




                                              35.5k74774






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426999%2fshowing-and-hiding-divs-using-css-media-query-not-working%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                                  Alcedinidae

                                                  RAC Tourist Trophy