Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

143
Visualizações
:after elements don't respond on hover or click

This is my code in which I want to do a simple star rating using JavaScript HTML and CSS.

var numbersDiv = document.getElementById("23");
var aa = document.createElement("div");
aa.className = "rating";
for (let i = 0; i < 5; i++) {
  var tempStar = document.createElement("input");
  tempStar.setAttribute("type", "radio");
  tempStar.setAttribute("name", "star");
  var tempStarId = "star" + i;
  tempStar.id = tempStarId;
  var tempLabel = document.createElement("label");
  tempLabel.setAttribute("for", tempStarId);
  aa.appendChild(tempStar);
  aa.appendChild(tempLabel);
}
numbersDiv.appendChild(aa);
.rating {
  display: flex;
}

.rating input {
  display: none;
}

.rating label {
  display: block;
  cursor: pointer;
  width: 50px;
  background: #ccc;
}

.rating label:before {
  content: "★";
  position: relative;
  font-family: fontAwesome;
  display: block;
  font-size: 50px;
  color: #101010;
}

.rating label:after {
  content: "★";
  position: absolute;
  font-family: fontAwesome;
  display: block;
  font-size: 50px;
  color: #1f9cff;
  top: 0;
}

.rating label:hover:after,
.rating label:hover~label:after,
.rating input:checked~label:after {
  opacity: 1;
}
<html>

<head>
  <link href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css" rel="stylesheet" />
</head>

<body>
  <div id="23"></div>

</body>

</html>

There are several problems:

  1. The after stars are slightly above the before stars
  2. When I am clicking, the after stars don't work as they should. (How should they work: When I click on a black star, it becomes blue)

I saw this code from the internet but for some reason it just won't work. Does anyone know how to fix this issue?

(Just for the record, the video which I saw online is this one: https://www.youtube.com/watch?v=Ep78KjstQuw)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You were missing 2 important CSS-Options:

  • Your Label needs to be position: relative; as the Stars will be positioned inside the label relative to the coordinates of the label. The missing position: relative; caused the stars to align to the body element and therefore at the top of the page.

  • You stars never had the opacity: 0; set, therefore are always visible.

var numbersDiv = document.getElementById("23");
var aa = document.createElement("div");
aa.className = "rating";
for (let i = 0; i < 5; i++) {
  var tempStar = document.createElement("input");
  tempStar.setAttribute("type", "radio");
  tempStar.setAttribute("name", "star");
  var tempStarId = "star" + i;
  tempStar.id = tempStarId;
  var tempLabel = document.createElement("label");
  tempLabel.setAttribute("for", tempStarId);
  aa.appendChild(tempStar);
  aa.appendChild(tempLabel);
}
numbersDiv.appendChild(aa);
.rating {
  display: flex;
}

.rating input {
  display: none;
}

.rating label {
  /* Added Position Relative*/
  position: relative;
  display: block;
  cursor: pointer;
  width: 50px;
  background: #ccc;
}

.rating label::before {
  content: "★";
  position: relative;
  font-family: fontAwesome;
  display: block;
  font-size: 50px;
  color: #101010;
}

.rating label::after {
  content: "★";
  position: absolute;
  font-family: fontAwesome;
  /* Set default Opacity to 0*/
  opacity: 0;
  display: block;
  font-size: 50px;
  color: #1f9cff;
  top: 0;
}

.rating label:hover::after,
.rating label:hover~label::after,
.rating input:checked~label::after {
  opacity: 1;
}
<html>

<head>
  <link href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.1.1/css/fontawesome.min.css" rel="stylesheet" />
</head>

<body>
  <div id="23"></div>

</body>

</html>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda