0

Есть блок, которые размещены в контейнере с overflow: scroll;

У блоках при наведении, справа появляется крестик. Все хорошо работает, пока не начинается скролл контейнера

Как прикрепить крестик к блоку?

.container {
  height: 150px;
  overflow: scroll;
}
.block {
  height: 70px;
  width: 100px;
  background: #adadad;
  padding: 10px;
  margin: 10px;
}
.close {
  position: absolute;
  right: 328px;
  background: #fff;
  padding: 10px;
}
.block:hover .close {
  right: 360px;
}
<div class="container">
  
  <div class="block">
    text
    <div class="close">X</div>
  </div>
  
  <div class="block">
    text
    <div class="close">X</div>
  </div>
  
</div>

1
  • 1
    если у родителя нет position relative то тогда будет позиционироватся от body иначе от родителя
    – Саске
    19 ноя 2018 в 10:50

1 ответ 1

2

.container {
  height: 150px;
  overflow: scroll;
}
.block {
  height: 70px;
  width: 100px;
  background: #adadad;
  padding: 10px;
  margin: 10px;
  position: relative;
}
.close {
  position: absolute;
  top: 30%;
  left: 100%;
  right: 328px;
  background: #fff;
  padding: 10px;
}
.block:hover .close {
  right: 360px;
}
<div class="container">
  
  <div class="block">
    text
    <div class="close">X</div>
  </div>
  
  <div class="block">
    text
    <div class="close">X</div>
  </div>
  
</div>

Что бы close прикреплялся к block, надо чтобы у block стоял position:relative. Иначе close прикрепляется к container-у который ты скролиш.

Ваш ответ

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

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