Search code examples
htmlcssstaticmedia-queriespadding

Padding change when adding a screen width media query isn't working


I have a example exercise with a Sidebar Navigation that is fixed left of the screen. I said that the content-div should have a position of absolute to the body and also a padding-left with the width of the sidebar, so that i can center my content in the visible area (width without the sidebar).

Now when I try to add a media query that says "when the screen is smaller than 1000px the Sidebar should disappear and the the padding of the content area should go to 0 (so that it gets centered in the normal screen width area)" it doesnt work.

I'm a beginner in html and css

Here is my code:

body {
  font-family: 'Oswald', sans-serif;
  padding: 0;
  margin: 0;
  background-color: #35af7e;
}

@media screen and (max-width: 1000px) {
  .Sidebar {
    left: -10rem;
  }

  .body {
    padding-left: 0;
    padding: 0;
  }


}

#flex {
  display: flex;
}

/* MEDIA QUERY FIX: NAVIC ; BODY-paddingleft ;  */

.NavIc {
  left: 1rem;
  top: 1rem;
  height: 30px;
  width: 30px;
  background-color: red;
  border-radius: 3px;
  z-index: 4;
  position: fixed;
  cursor: pointer;
}

.NavIc:hover .Sidebar {
  background-color: red;
}


.Sidebar {
  position: fixed;
  width: 10rem;
  height: 100%;
  background: #E9D758;
  color: white;
  z-index: 3;
  display: flex;
  flex-direction: column;
  transition: 200ms ease-in-out;
}

.space {
  flex-grow: 1;
  width: auto;
}

.Nav {
  list-style-type: none;
  display: block;
  width: 100%;
  padding: 0;
  font-size: 1.5rem;
  z-index: 2;
  flex-grow: 18;
}

ul li {
  box-sizing: border-box;
  padding: 0.7rem 0 0.7rem 1rem;
  position: relative;
  transition: 150ms ease-in-out;
}

ul li:hover {
  background-color: white;
  color: black;
  transform: scale(1.07) translate(0.3rem);
  cursor: pointer;
}

.footnotes {
  flex-grow: 1;
  box-sizing: border-box;
  padding: 0 1rem 0 1rem;
}

hr {
  border-style: solid;
  color: white;
}

.body {
  position: absolute;
  width: 100%;
  padding-left: 10rem;
  box-sizing: border-box;
  top: 0;
  color: white;
  transition: 200ms ease-in-out;
}

.mid {
  position: relative;
  width: 60%;
  left: 50%;
  transform: translate(-50%);
  font-size: 1.5rem;
}


.img {
  position: relative;
  max-width: 500px;
  height: auto;
  left: 50%;
  transform: translate(-50%);
  transition: 200ms ease-in-out;
}

.Logo {
  position: relative;
  left: 50%;
  transform: translate(-50%);
  max-width: 100%;
  height: auto;
  padding: 5rem 0 5rem 0;
}

.mid > hr {
  width: 80%;
}
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Sidebar</title>

    <link rel="stylesheet" style="text/css" href="sidebar.css">
    <link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
  </head>
  <body>

    <div class="NavIc">
    </div>

    <div class="Sidebar">
      <div class="space">
      </div>
      <ul class="Nav">
        <li>Home</li>
        <li>Gallery</li>
        <li>Our Team</li>
        <li>Contact</li>
      </ul>

      <div class="footnotes">
        <hr>
        <p>blablabla<br>
        All rights reserved</p>
        <p>blablabla</p>
      </div>
    </div>

    <div class="body">
      <div class="mid">
        <div class="img">
          <img src="Logo.svg" alt="Logo" class="Logo">
        </div>

        <hr>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea.Commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident.Sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.</p><p>Incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</br>
        <hr>
 
      </div>
    </div>

  </body>
</html>


Solution

  • Because your .body rule in @media (max-width: 1000px) is being override. Use !important or learn some CSS specificity/precedence.

    body {
      font-family: 'Oswald', sans-serif;
      padding: 0;
      margin: 0;
      background-color: #35af7e;
    }
    
    @media screen and (max-width: 1000px) {
      .Sidebar {
        left: -10rem;
      }
    
      .body {
        padding-left: 0 !important;
        padding: 0;
      }
    
    
    }
    
    #flex {
      display: flex;
    }
    
    /* MEDIA QUERY FIX: NAVIC ; BODY-paddingleft ;  */
    
    .NavIc {
      left: 1rem;
      top: 1rem;
      height: 30px;
      width: 30px;
      background-color: red;
      border-radius: 3px;
      z-index: 4;
      position: fixed;
      cursor: pointer;
    }
    
    .NavIc:hover .Sidebar {
      background-color: red;
    }
    
    
    .Sidebar {
      position: fixed;
      width: 10rem;
      height: 100%;
      background: #E9D758;
      color: white;
      z-index: 3;
      display: flex;
      flex-direction: column;
      transition: 200ms ease-in-out;
    }
    
    .space {
      flex-grow: 1;
      width: auto;
    }
    
    .Nav {
      list-style-type: none;
      display: block;
      width: 100%;
      padding: 0;
      font-size: 1.5rem;
      z-index: 2;
      flex-grow: 18;
    }
    
    ul li {
      box-sizing: border-box;
      padding: 0.7rem 0 0.7rem 1rem;
      position: relative;
      transition: 150ms ease-in-out;
    }
    
    ul li:hover {
      background-color: white;
      color: black;
      transform: scale(1.07) translate(0.3rem);
      cursor: pointer;
    }
    
    .footnotes {
      flex-grow: 1;
      box-sizing: border-box;
      padding: 0 1rem 0 1rem;
    }
    
    hr {
      border-style: solid;
      color: white;
    }
    
    .body {
      position: absolute;
      width: 100%;
      padding-left: 10rem;
      box-sizing: border-box;
      top: 0;
      color: white;
      transition: 200ms ease-in-out;
    }
    
    .mid {
      position: relative;
      width: 60%;
      left: 50%;
      transform: translate(-50%);
      font-size: 1.5rem;
    }
    
    
    .img {
      position: relative;
      max-width: 500px;
      height: auto;
      left: 50%;
      transform: translate(-50%);
      transition: 200ms ease-in-out;
    }
    
    .Logo {
      position: relative;
      left: 50%;
      transform: translate(-50%);
      max-width: 100%;
      height: auto;
      padding: 5rem 0 5rem 0;
    }
    
    .mid > hr {
      width: 80%;
    }
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Sidebar</title>
    
        <link rel="stylesheet" style="text/css" href="sidebar.css">
        <link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
      </head>
      <body>
    
        <div class="NavIc">
        </div>
    
        <div class="Sidebar">
          <div class="space">
          </div>
          <ul class="Nav">
            <li>Home</li>
            <li>Gallery</li>
            <li>Our Team</li>
            <li>Contact</li>
          </ul>
    
          <div class="footnotes">
            <hr>
            <p>blablabla<br>
            All rights reserved</p>
            <p>blablabla</p>
          </div>
        </div>
    
        <div class="body">
          <div class="mid">
            <div class="img">
              <img src="Logo.svg" alt="Logo" class="Logo">
            </div>
    
            <hr>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea.Commodo consequat.</p><p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident.Sunt in culpa qui officia deserunt mollit anim id est laborum. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.</p><p>Incididunt ut labore et dolore magna aliqua Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</br>
            <hr>
     
          </div>
        </div>
    
      </body>
    </html>