Search code examples
cssmedia-queries

How to change css according to the size of the display


Im so new in CSS and trying to fix the following code.. I want a simple thing where the screen size is smaller than 400 change the image size.. it should work but it doesn't.. I tried to make

* {
    box-sizing: border-box;
} 

body, html {
    background: #fff;
    height: 100%;
    margin: 10px;
}

.left__img2 {
    position: absolute;
    float: left;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
    border-radius: 20px;
    width: 600px;
    height: 400px; 
} 


@media screen and (max-width: 500px) {
.left__img2 {
    width: 10px;
} 
}

Solution

  • Media queries at the top of the CSS need !important to over rule the media query. Media queries at the bottom do not need !important. I placed the query at the top so I used !important to over rule any other style after.

    @media screen and (max-width: 500px) {
    .left__img2 {
    width: 10px !important;
    } 
    }
    
    * {
    box-sizing: border-box;
    } 
    
    body, html {
    background: #fff;
    height: 100%;
    margin: 10px;
    }
    
    .left__img2 {
    position: absolute;
    float: left;
    display: block;
    margin-left: auto;
    margin-right: auto;
    width: 50%;
    border-radius: 20px;
    width: 600px;
    height: 400px; 
    }