2

im learning css in general and i am failling very, - v e r y - hard at it...

im trying to do a simple thing with my css grid, just animate the hidden menu right to left, instead of left to right, but i dont know what i must change to do it..

Can you guys help me? Here the fiddle: https://jsfiddle.net/xwvL2a70/

/* grid */
.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-gap: 10px;
}

/* items */
.container > * {
  color: #353535;
  font-size: 1.2em;
  line-height: 1.5;
  padding: 20px;
  background: #d0cfc5;
}

/* nav styles */
.container nav {
  background: #136fd2;
}

nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

nav a {
  color: #d0cfc5
}

nav a:hover {
  text-decoration: none;
}

/* media query for grid layout */
@media only screen and (min-width: 600px) {

  /* grid */
  .container {
    grid-template-columns: repeat(4, 1fr);
  }

  /* specific item styles */
  .container header,
  .container nav,
  .container footer {
    grid-column: span 4;
  }
  .container section {
    grid-column: span 3;
  }

  /* nav styles */
  nav ul li {
    display: inline-block;
    padding: 0 20px 0 0;
  }

  /* hide toggle */
  .toggle {
    display: none;
  }

}

/* media query for nav styles */
@media only screen and (max-width: 599px) {

    #nav {
      transition: transform .3s ease-in-out;
      top: 0;
      bottom: 0;   
      min-height: 100vh; /* override Safari bug */
      position: fixed; /* or choose `absolute` depending on desired behavior*/
      width: 300px;
      left: -340px;
    }

    #nav:target {     
      transform: translateX(340px);
    }

    .close {
      text-align: right;
      display: block;
      text-decoration: none;
      font-size: 3em;
      position: relative;
      top: -30px;
    }

  .open {
    text-align: left;
  }

}
Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
Vinicius Souza
  • 143
  • 3
  • 11

1 Answers1

7

You just need to change your left: -340px; to right: -340px; and then your transform: translateX(340px); to transform: translateX(-340px);. Both of these are within the media query.

Here's a working example (if you view in full screen and scale the window width down to preview):

/* grid */
.container {
  display: grid;
  grid-template-columns: 1fr;
  grid-gap: 10px;
}

/* items */
.container > * {
  color: #353535;
  font-size: 1.2em;
  line-height: 1.5;
  padding: 20px;
  background: #d0cfc5;
}

/* nav styles */
.container nav {
  background: #136fd2;
}

nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

nav a {
  color: #d0cfc5
}

nav a:hover {
  text-decoration: none;
}

/* media query for grid layout */
@media only screen and (min-width: 600px) {
    
  /* grid */
  .container {
    grid-template-columns: repeat(4, 1fr);
  }
  
  /* specific item styles */
  .container header,
  .container nav,
  .container footer {
    grid-column: span 4;
  }
  .container section {
    grid-column: span 3;
  }
  
  /* nav styles */
  nav ul li {
    display: inline-block;
    padding: 0 20px 0 0;
  }
  
  /* hide toggle */
  .toggle {
    display: none;
  }
  
}

/* media query for nav styles */
@media only screen and (max-width: 599px) {
  
    #nav {
      transition: transform .3s ease-in-out;
      top: 0;
      bottom: 0;   
      position: fixed;
      width: 300px;
      right: -340px;
    }
  
    #nav:target {     
      transform: translateX(-340px);
    }
  
    .close {
      text-align: right;
      display: block;
      text-decoration: none;
      font-size: 3em;
      position: relative;
      top: -30px;
    }
  
  .open {
    text-align: left;
  }
  
}
<div class="support-grid"></div>

<div class="container">
  
  <header role="banner">
      <a class="toggle open" href="#nav">open</a>
      <h1>Header</h1>
  </header>

  <nav id="nav" role="navigation">  
    <a class="toggle close" href="#">×</a>
    <ul>
        <li>
            <a href="#">Item 1</a>
        </li>
        <li>
            <a href="#">Item 2</a>
        </li>
        <li>
            <a href="#">Item 3</a>
        </li>
    </ul>
  </nav>

  <section role="main">
      <article>
          <h2>Article</h2>
          <p>Curabitur orci lacus, auctor ut facilisis nec, ultricies quis nibh. Phasellus id diam sollicitudin, malesuada turpis id, gravida erat. Maecenas placerat elit vel hendrerit convallis. Sed in mauris ut justo vulputate viverra feugiat ac dui. Fusce feugiat arcu in vehicula vehicula. Donec varius justo at nulla aliquet volutpat.</p> 
          <p>Ut id rutrum eros. Nulla tristique, magna et mattis vulputate, mi eros suscipit turpis, nec bibendum turpis nunc feugiat sapien. Nunc arcu est, lacinia id diam quis, sagittis euismod neque. Nullam fringilla velit sed porta gravida. Proin eu vulputate libero. Ut a lacinia enim. Etiam venenatis mauris et orci tempor congue. Sed tempor eros et ultricies congue. Aenean sed efficitur orci. Nulla vel tempus mi.</p>
          <p>Ut cursus suscipit augue, id sagittis nibh faucibus eget. Etiam suscipit ipsum eu augue ultricies, at rhoncus mi faucibus. In et tellus vitae leo scelerisque fringilla nec at nunc.</p>
      </article>
  </section>

  <aside>
      <h3>Aside</h3>
  </aside>

  <footer>
      <h3>Footer</h3>
  </footer>
  
</div>
  • 1
    Dude, you just dont know how much time ive been changing things to try and make this work, thank you very much! I have another question, should i create another thread or should i ask here? – Vinicius Souza Jan 16 '18 at 12:05
  • 2
    No problem, I'm glad I could help. It's probably better to ask another question, to keep the topics separate. –  Jan 16 '18 at 12:06
  • If its not too much to ask, what should i google to learn about these positions? Like, i dont get why with left i shouldnt use negative pxs, and with right i should.. – Vinicius Souza Jan 16 '18 at 12:24
  • 1
    It's quite a difficult question answer because a lot of it depends on use cases. A good place to start might be: [CSS Tricks](https://css-tricks.com/almanac/properties/p/position/). With this question, it changed to right to hide the panel off screen to the right, rather than the left, and then the transform changed because instead of moving it across, it was moving it backwards, which meant it needed the negative value. Hope this helps. –  Jan 16 '18 at 12:54
  • your snippet does not work for me in Safari/Mac. When I click "open", I get what I assume to be the bottom of a menu in the top-right corner, but I see no links.... – Zonker.in.Geneva Jun 24 '18 at 15:38