skip to Main Content

My CSS is working but how do I reduce the number of @media CSS codes (I feel some are unnecessary)?

.spandemo {
  display: inline-block;
  padding-left: 42%;
}


}
@media screen and (max-width: 600px) {
  .spandemo {
    display: inline-block;
    padding-left: 1%;
  }
}
.column {
  width: 98%;
  float: right;
}

}
.column img {
  max-height: 129px;
  height: 100%;
  width: auto;
}

}
@media screen and (max-width: 600px) {
  .column {
    width: 90%;
    float: right;
  }
}
@media screen and (max-width: 600px) {
  .column img {
    max-height: 129px;
    height: 70%;
    width: 90%;
    padding-right: 10px;
  }
}
@media screen and (min-width: 600px) {
  .column img {
    max-height: 372px;
    height: 70%;
    width: 90%;
    padding-right: 10px;
  }
}
<div class="row">
  <div class="column">
    <img class="responsive" src="https://picsum.photos/seed/left/200/300" style="width:50%" border="1" />
    <img class="responsive" src="https://picsum.photos/seed/right/200/300" style="float:right; width:50%" border="1" />
    <span style="float:left;">&nbsp;&nbsp;Before</span>
    <span class="spandemo" style="float:left;">&nbsp;&nbsp;After</span>
  </div>
</div>

This is a follow up question to what I asked here: CSS working for mobile view but not wide screens

I added all the @media CSS codes to get the desired effects but I think some are unnecessary if used in the correct order. Please let me know what can be removed or edited.

2

Answers


  1. A few tips:

    1.) You don’t need to repeat the exact same @media query multiple times, you can put multiple CSS rules inside a single query.

    2.) When the @media query is triggered, it just adds those styles to the rules that are already in place. So, for example, if an element has float: right; you don’t need to add float: right; to the same element inside the @media query.

    3.) In general it’s a good idea to write your CSS in a way so that no @media queries are needed at all for the smallest screen size. As the screen size gets larger add more @media queries to override the previous styles. This means you should probably only use @media screen and (min-width: ____) values and not ones that target max-width. Especially important: don’t use **both min-width and max-width queries as this will get very confusing for you since they might overlap

    Below is just a simple cleanup that removes the duplication I mentioned in those first 2 tips. I have not gone through and figured out how to implement #3 since you would probably know better than me for what you want here.

    .spandemo {
      display: inline-block;
      padding-left: 42%;
    }
    
    .column {
      width: 98%;
      float: right;
    }
    
    .column img {
      max-height: 129px;
      height: 100%;
      width: auto;
    }
    
    @media screen and (max-width: 600px) {
      .spandemo {
        padding-left: 1%;
      }
      
      .column {
        width: 90%;
      }
    
      .column img {
        height: 70%;
        width: 90%;
        padding-right: 10px;
      }
    }
    
    @media screen and (min-width: 600px) {
      .column img {
        max-height: 372px;
        height: 70%;
        width: 90%;
        padding-right: 10px;
      }
    }
    <div class="row">
      <div class="column">
        <img class="responsive" src="https://picsum.photos/seed/left/200/300" style="width:50%" border="1" />
        <img class="responsive" src="https://picsum.photos/seed/right/200/300" style="float:right; width:50%" border="1" />
        <span style="float:left;">&nbsp;&nbsp;Before</span>
        <span class="spandemo" style="float:left;">&nbsp;&nbsp;After</span>
      </div>
    </div>
    Login or Signup to reply.
  2. I cleaned up your repetitive codes. When you copy paste your code in the media query to change then you should compare and remove what CSS rules remains the same for different screens. Also there were 3 unnecessary curly brackets. Here is the cleaner version of your CSS codes:

    .spandemo {
        display: inline-block;
        padding-left: 42%;
    }
    
    @media screen and (max-width: 600px) {
        .spandemo {
            padding-left: 1%;
        }
    }
    
    .column {
        width: 98%;
        float: right;
    }
    
    @media screen and (max-width: 600px) {
        .column {
            width: 90%;
        }
    }
    
    .column img {
        max-height: 129px;
        height: 100%;
        width: auto;
    }
    
    
    @media screen and (max-width: 600px) {
        .column img {
            max-height: 372px;
            height: 70%;
            width: 90%;
            padding-right: 10px;
        }
    }
    
    @media screen and (min-width: 600px) {
        .column img {
            max-height: 372px;
            height: 70%;
            width: 90%;
            padding-right: 10px;
        }
    }
    <div class="row">
      <div class="column">
        <img class="responsive" src="https://picsum.photos/seed/left/200/300" style="width:50%" border="1" />
        <img class="responsive" src="https://picsum.photos/seed/right/200/300" style="float:right; width:50%" border="1" />
        <span style="float:left;">&nbsp;&nbsp;Before</span>
        <span class="spandemo" style="float:left;">&nbsp;&nbsp;After</span>
      </div>
    </div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search