简体   繁体   English

css覆盖img中心在错误的地方

[英]css overlay img centers in wrong place

I have list-group-item elements that each holds an image and some text.我有list-group-item元素,每个元素都包含一个图像和一些文本。 The image is set to the left, text to the right.左边是图片,右边是文字。

I want a play icon overlay over the image, in its center, but instead its centered on the list-group-item instead.我想要一个播放图标覆盖在图像上,在它的中心,而不是它以list-group-item为中心。 I'm using bootstrap 4.4.1.我正在使用引导程序 4.4.1。

Html html

<div class="video-list-stacked">
  <ul class='list-group video-list-container'>
    <li id="some-guid-01" class="list-group-item video-item">
      <div class=video-item-img>
        <img src="https://via.placeholder.com/140x100" />
        <img class="video-item-playicon" src="https://images.vexels.com/media/users/3/131784/isolated/preview/a9ff82db0cf438516e13b8c3bf918a00-play-flat-icon-by-vexels.png">
      </div>
      <div class="video-item-details">
        <h4>Video Title 1</h4>
        <div>Description for Video Title 1. A little description that elaborates what this is. The description should take not more than two lines.</div>
        <span class="video-item-tag">Tag One</span>
        <span class="video-item-tag">Some other tag</span>
        <span class="video-item-tag">Some other tag</span>
        <span class="video-item-tag">Some other tag</span>
        <span class="video-item-tag">Some other tag</span>
      </div>
    </li>
</div>

CSS CSS

.video-item{
 display: flex;
 padding-left: 5px;
}

.video-item-details {
  padding-left: 10px;
}

.video-item-tag {
  background: #2196F3;
  padding: 2px 10px 2px 10px;
  float: left;
  margin: 2px;
  margin-bottom: 5px;
  border-radius: 100px;
  color: white;
  font-family: monospace;
  font-size: 8pt;
  position: relative;
  cursor: pointer;
  
  text-align: center;
}

.video-item-playicon {
  position: absolute;
  width: 60px;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  pointer: center;
}

What do I need to do to make the play icon appear centered in the image?我需要做什么才能使播放图标显示在图像的中心?

I added css for video-item-img to set the width and to position relatively because using position: absolute on the play icon only works for positioned elements.我为video-item-img添加了 css 来设置宽度和相对位置,因为在播放图标上使用position: absolute仅适用于定位元素。

Absolute position: The element is removed from the normal document flow, and no space is created for the element in the page layout.绝对位置:元素从正常的文档流中移除,页面布局中没有为元素创建空间。 It is positioned relative to its closest positioned ancestor, if any;如果有的话,它是相对于其最近定位的祖先定位的; otherwise, it is placed relative to the initial containing block.否则,它相对于初始包含块放置。 Its final position is determined by the values of top, right, bottom, and left.它的最终位置由顶部、右侧、底部和左侧的值决定。

MDN MDN

 .video-item { display: flex; padding-left: 5px; } .video-item-details { padding-left: 10px; } .video-item-tag { background: #2196F3; padding: 2px 10px 2px 10px; float: left; margin: 2px; margin-bottom: 5px; border-radius: 100px; color: white; font-family: monospace; font-size: 8pt; position: relative; cursor: pointer; text-align: center; } .video-item-playicon { position: absolute; width: 60px; left: 50%; top: 50%; transform: translate(-50%, -50%); pointer: center; } .video-item-img { /*<------- added */ width: fit-content; /*<------- added */ position: relative; /*<------- added */ }
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <div class="video-list-stacked"> <ul class='list-group video-list-container'> <li id="some-guid-01" class="list-group-item video-item"> <div class=video-item-img> <img src="https://via.placeholder.com/140x100" /> <img class="video-item-playicon" src="https://images.vexels.com/media/users/3/131784/isolated/preview/a9ff82db0cf438516e13b8c3bf918a00-play-flat-icon-by-vexels.png"> </div> <div class="video-item-details"> <h4>Video Title 1</h4> <div>Description for Video Title 1. A little description that elaborates what this is. The description should take not more than two lines.</div> <span class="video-item-tag">Tag One</span> <span class="video-item-tag">Some other tag</span> <span class="video-item-tag">Some other tag</span> <span class="video-item-tag">Some other tag</span> <span class="video-item-tag">Some other tag</span> </div> </li> </div>

I only added this code to your css and that is working fine:我只将此代码添加到您的 css 中,并且工作正常:

 .video-item-img { position: relative; }

Also I think you have a mistake in your CSS:此外,我认为您的 CSS 有误:

 .video-item-playicon { cursor: pointer; /* not pointer:center */ }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM