0

Прошу вашей помощи. Вкратце: есть несколько item блоков, у каждого есть ::after в виде звездочки , а так же дочерний элемент, скрытый по умолчанию. Нужно на чистом CSS реализовать такую вещь : при наведении на ::after показывать дочерний блок только этого item, исключая отображение остальных

Вот код который есть сейчас

   <div class="item">
       <div class="info">
           Lorem ipsum, dolor sit amet consectetur adipisicing elit.
       </div>
   </div>
   <div class="item">
       <div class="info">
           Lorem ipsum, dolor sit amet consectetur adipisicing elit.
       </div>
   </div>

И стили к нему

.item{
  position: relative;
}
.item::after{
  content: "*";
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 10px 4px 4px 3px;
  border-radius: 50%;
  background: #9C3939;
  font-size: 1em;
  cursor: pointer;
  line-height: 0;
}
.info{
  display: none;
  position: absolute;
  bottom: 0;
  right: 0;
  background: #2a2a2a;
  padding: 10px;
  width: 200px;
  text-align: left;
  border: 1px solid #c4c4c4;
}

Пытался сделать это с помощью такого кода

.item::after:hover > .info{
  display: block;
}

Но в этом случае, насколько я понимаю, он ищет блок внутри ::after, а не внутри item

2 ответа 2

2

Можно реализовать наведение на ::after или ::before, поиграв с target'ом, при помощи правила pointer-events.

Для этого родителю нужно отключить target, при помощи правила pointer-events: none, а вот его псевдоэлементу наоборот включить - pointer-events: all.

После такого действия, при наведение псевдоэлемент, у него появится target именно родителя, т.е. родитель:hover будет идти от родитель::псевдоэлемент

Но учтите, что после такой "махинации" всё область родителя, будет неактивна.
(т.е. если в родителе будет, к примеру, кнопка, то она работать не будет)

.item {
  display: block;
  width: 200px;
  height: 50px;
  position: relative;
  pointer-events: none;
}

.item::after {
  content: "*";
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 10px 4px 4px 3px;
  border-radius: 50%;
  background: #9C3939;
  font-size: 1em;
  cursor: pointer;
  line-height: 0;
  pointer-events: all;
}

.info {
  display: none;
  position: absolute;
  bottom: 0;
  right: 0;
  background: #2a2a2a;
  padding: 10px;
  width: 200px;
  text-align: left;
  border: 1px solid #c4c4c4;
}

.item:hover > .info {
  display: block;
}
<div class="item">
  <div class="info">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
  </div>
</div>
<div class="item">
  <div class="info">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
  </div>
</div>

0

Кажется никак. Можно организовать звездочку в виде отдельного блока (+ добавил z-index псевдоэлементу). Со стилями вам придется еще немного поиграть:

.item {
  position: relative;
  width: 200px;
  height: 50px;
}

.star::after {
  content: "*";
  position: absolute;
  bottom: 0;
  right: 0;
  padding: 10px 4px 4px 3px;
  border-radius: 50%;
  background: #9C3939;
  font-size: 1em;
  cursor: pointer;
  line-height: 0;
  z-index: 10;
}

.info {
  display: none;
  position: absolute;
  bottom: 0;
  right: 0;
  color: white;
  background: #2a2a2a;
  padding: 10px;
  width: 200px;
  text-align: left;
  border: 1px solid #c4c4c4;
}


.star:hover ~ .info {
  display: block;
}
<div class="item">
  <div class="star"></div>
  <div class="info">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
  </div>
</div>
<div class="item">
  <div class="star"></div>
  <div class="info">
    Lorem ipsum, dolor sit amet consectetur adipisicing elit.
  </div>
</div>

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.