Html – css: how to position a X sign at the top right corner of the img element

csscss-floathtml

Besides, position: relative, and then set right and bottom position?

I wanna the close sign positioned top right corner whatever the size of the img

Best Answer

You can use absolute position on the X sign and set its position with respect to the relatively positioned parent.

document.querySelector('.close').addEventListener('click', function() {
  console.log('close');
});
.wrapper {
  position: relative;
  display: inline-block;
}
.close:before {
  content: '✕';
}
.close {
  position: absolute;
  top: 0;
  right: 0;
  cursor: pointer;
}
<div class="wrapper">
  <img src="https://www.google.com/images/srpr/logo11w.png" />
  <span class="close"></span>
</div>