html - 如何在具有动态高度的按钮内居中前景图像?

标签 html css image center

<分区>


关闭 8 年前

我试图在按钮上创建一个前景图像。 我已经准备好了:

button:hover .remove-me {
  opacity: 1;
}
.remove-me {
  opacity: 0.5;
  display: table-cell;
  position: absolute;
  width: 64px;
  height: 64px;
}
<button class="input-image remove-me-container focus" data-btid="3" id="imagepicker_1412162517704">
  <img class="remove-me" src="http://cdn.flaticon.com/png/64/7909.png" />
  <img draggable="false" height="250px" width="250px" class="img-responsive" src="../../../img/logo.png" />
</button>

如果可能的话,我更喜欢非 JavaScript 解决方案。

最佳答案

你可以在button上添加position: relative;然后设置图片的topleft为50%,然后使用 margin-topmargin-left 将图像向后移动一半大小:

http://jsfiddle.net/g1cbeuho/4/

button:hover .remove-me {
    opacity: 1;

}


button{
        position: relative;
}

.remove-me {
    opacity: 0.5;
    display: table-cell;
    position: absolute;
    width: 64px;
    height: 64px;
    top: 50%;
    left: 50%;
    margin-top: -32px;
    margin-left: -32px;
}
<button class="input-image remove-me-container focus" data-btid="3" id="imagepicker_1412162517704">
    <img class="remove-me" src="http://cdn.flaticon.com/png/64/7909.png" />
    <img draggable="false" height="250px" width="250px" class="img-responsive" src="../../../img/logo.png" />
</button>

关于html - 如何在具有动态高度的按钮内居中前景图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26140667/

上一篇:html - 如何理解IE评论的条件?

下一篇:css - 如何调整背景图和标签图的高度?

相关文章:

jquery - 如果标题隐藏,调整容器 div 位置

javascript - 单击按钮后如何逐个调用部分并且上一节将隐藏?

c# - 添加透明水印(png)图像到另一图像

c# - 从剪贴板复制增强型图元文件并将其另存为图像

html - 在 div 中居中 div

css - Div 显示在页面中央,背景延伸

c# - 如何在最终生成的 pdf 文档中使用库 TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator 下载嵌入式图像?

html - block 容器框中的内联和 block 级框

css - 荒谬的 CSS 媒体查询问题

php - 使用 php 输出图像的最佳方法