cssmedia-queries

Consolidate @media tags


Is there a way to consolidate @media tags to clean them up. I am new to this and want to clean up my site to work better on mobile devices but it seems like I shouldn't have to keep repeating the @media statement in my code below.

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

  div.pagetopmenu,
  div.menutoplogo,
  div.pagemenul,
  div.pagemenur {
    display: none;
  }

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

    .pagemainbody {
      float: left;
      width: 100%;
      min-height: 100%;
      overflow: hidden;
    }

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

      .centediv {
        width: 60%;
        margin-top: 2px;
        margin-right: auto;
        margin-left: auto;
        height: 105px;
        overflow: hidden;
        clear: both;
      }

Solution

  • Well, you don't have to repeat the @media statement over and over, just group all the target CSS properties inside one media block, like so:

    @media screen and (max-width: 600px) {
    
        div.pagetopmenu,
        div.menutoplogo,
        div.pagemenul,
        div.pagemenur {
          display: none;
        }
    
        .pagemainbody {
          float: left;
          width: 100%;
          min-height: 100%;
          overflow: hidden;
        }
    
        .centediv {
          width: 60%;
          margin-top: 2px;
          margin-right: auto;
          margin-left: auto;
          height: 105px;
          overflow: hidden;
          clear: both;
        }
    }