nth-child practice

In this example below you will see how to do a nth-child practice with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by MightyJoeW, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright MightyJoeW ©
  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>nth-child practice</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <html>
  <head>
    <title>nth-child</title>
  </head>
  <body>
    <header>
      <h1>Hello</h1>
    </header>
    <ul>
      <li class="list-item">1</li>
      <li class="list-item">2</li>
      <li class="list-item">3</li>
      <li class="list-item">4</li>
      <li class="list-item">5</li>
      <li class="list-item">6</li>
      <li class="list-item">7</li>
      <li class="list-item">8</li>
      <li class="list-item">9</li>
      <li class="list-item">10</li>
      <button id="odd-button">Odd</button>
      <button id="even-button">Even</button>
    </ul>
  </body>
</html>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/MightyJoeW/nth-child-practice-mXOPdP */
h1 {
  text-align: center;
}

ul {
  display: grid;
  grid-gap: 1em;
  grid-template-columns: repeat(2, 1fr);
  justify-items: center;
}

ul li {
  list-style-type: none;
  font-weight: 700;
}

/* li:nth-child(odd) {
  color: blue;
}

li:nth-child(even) {
  color: red;
} */

/*Downloaded from https://www.codeseek.co/MightyJoeW/nth-child-practice-mXOPdP */
const oddButton = document.getElementById('odd-button');

const evenButton = document.getElementById('even-button');

const listItem = document.getElementsByClassName('list-item');

oddButton.addEventListener('click', function(){
  listItem.style.color = 'green';
})

Comments