Search code examples
csscss-counter

change content on hover CSS


I want to change content of list on hover, i.e roman changed into numeric 1, 2, 3

I tried li:hover li:hover:after but both will add the content with existing content

ul {
  min-height: 30px;
  width: 180px;
  margin-left:-30px;
  list-style-type: none;
  counter-reset: chrome-counter;
}

li:before {
  content: counter(chrome-counter, lower-roman);
  padding: 1px 2px;
  margin-right: 0.2em;
  font-weight: bold;
}

li {
  margin: 2px 0;
  padding: 1px 4px;
  font: normal bold 18px/32px  ms-sans, geneva, sans-serif;
  display: inline-block;
  width: 40px;
  text-align: center;
  color: #FFF;
  counter-increment: chrome-counter;
}

li:hover {
  padding: 4px 8px;
  color: #333;
  /*content : '..';*/
  counter-reset : counter(chrome-counter, upper-roman);
  box-shadow: 0px 0px 4px #222;
  border-radius: 1.5em .5em / 2.5em 2em;
}

.cc1 {
  background-color: #FF6766;
  color: #FFF;
}
 <ul class="scheme">
      <li class="cc1" ></li>
     <li class="cc1" ></li>
     <li class="cc1" ></li>
</ul>


Solution

  • If you're working on the :before pseudo-element, you need to apply all the states to that exact element... so not to the main element, nor the :after element, but the one that needs to change, which is li:before in your case:

    ul {
      min-height: 30px;
      width: 180px;
      margin-left:-30px;
      list-style-type: none;
      counter-reset: chrome-counter;
    }
    
    li:before {
      content: counter(chrome-counter, lower-roman);
      padding: 1px 2px;
      margin-right: 0.2em;
      font-weight: bold;
    }
    
    li {
      margin: 2px 0;
      padding: 1px 4px;
      font: normal bold 18px/32px  ms-sans, geneva, sans-serif;
      display: inline-block;
      width: 40px;
      text-align: center;
      color: #FFF;
      counter-increment: chrome-counter;
    }
    
    li:hover {
      padding: 4px 8px;
      color: #333;
      /*content : '..';*/
      counter-reset : counter(chrome-counter, upper-roman);
      box-shadow: 0px 0px 4px #222;
      border-radius: 1.5em .5em / 2.5em 2em;
    }
    
    li:hover:before {
      content: counter(chrome-counter, upper-roman);
      padding: 1px 2px;
      margin-right: 0.2em;
      font-weight: bold;
    }
    
    .cc1 {
      background-color: #FF6766;
      color: #FFF;
    }
     <ul class="scheme">
         <li class="cc1" ></li>
         <li class="cc1" ></li>
         <li class="cc1" ></li>
    </ul>

    Here is an example with changing into HTML content like you asked for in the comments:

    ul {
      min-height: 30px;
      width: 180px;
      margin-left:-30px;
      list-style-type: none;
      counter-reset: chrome-counter;
    }
    
    li:before {
      content: counter(chrome-counter, lower-roman);
      padding: 1px 2px;
      margin-right: 0.2em;
      font-weight: bold;
    }
    
    li {
      margin: 2px 0;
      padding: 1px 4px;
      font: normal bold 18px/32px  ms-sans, geneva, sans-serif;
      display: inline-block;
      width: 40px;
      text-align: center;
      color: #FFF;
      counter-increment: chrome-counter;
    }
    
    li:hover {
      padding: 4px 8px;
      color: #333;
      /*content : '..';*/
      counter-reset : counter(chrome-counter, upper-roman);
      box-shadow: 0px 0px 4px #222;
      border-radius: 1.5em .5em / 2.5em 2em;
    }
    
    li:hover:before {
      content: "";
      padding: 1px 2px;
      margin-right: 0.2em;
      font-weight: bold;
    }
    
    li span {display: none}
    li:hover span {display: initial}
    
    .cc1 {
      background-color: #FF6766;
      color: #FFF;
    }
     <ul class="scheme">
          <li class="cc1" ><span>#</span></li>
         <li class="cc1" ><span>0</span></li>
         <li class="cc1" ><span>*</span></li>
    </ul>