HTML Element Style How to - Style li item by index








Question

We would like to know how to style li item by index.

Answer


<!--   w  w w  . j a  v a2  s.c om-->



<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
ul {
  list-style: none;
  margin: 0;
  padding: 2px;
  display: block;
  clear: both;
  width: 400px;
}
li {
  float: left;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  -webkit-box-sizing: border-box;
  width: 129px;
  height: 120px;
  border: 1px solid grey;
  margin-left: -1px;
  margin-top: -1px;
  padding: 8px;
  position: relative;
  z-index: 1;
}
li:nth-child(5) {
  border: 2px solid blue;
  z-index: 2;
}
</style>
</head>
<body>
  <ul>
    <li>1</li>
    <li>2</li>
    <li>3</li>
    <li>4</li>
    <li>5</li>
    <li>6</li>
    <li>7</li>
    <li>8</li>
    <li>9</li>
  </ul>
</body>
</html>

The code above is rendered as follows: