html - 图像重叠图像,中间位置

标签 html css image position

这是我的 CSS 类:

.overitem {
    position: relative; 
    background: url(images/bg.png) no-repeat; 
    width:83px;
    height: 83px;
}

.sit {
    position: absolute; 
    top: 50%; 
    left: 50%;
}

我的 HTML:

<div class="overitem"><img src="/images/vcs.png" class="sit"/></div>

问题是我不能从 img 标签,在 overitem 类的背景中间。我应该怎么做才能让它出现在 bg.png 图片的中间?

最佳答案

由于 .sit 类必须有固定的宽度和高度(因为它是一个图像),您可以使用以下方法:

    .sit {
       position: absolute;
       top: 50%;
       left: 50%;
       width: 500px;
       height: 500px;
       margin-top: -250px; /* Half the height */
       margin-left: -250px; /* Half the width */
    }

来源:http://css-tricks.com/snippets/css/absolute-center-vertical-horizontal-an-image/

关于html - 图像重叠图像,中间位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8332358/

相关文章:

html - 纯 CSS - 几个 阅读更多 阅读更少

javascript - jquery 在某些 html 样式中不起作用

iphone - 我可以在 iPhone/iPad 上为 HTML5 视频换肤吗?

html - Select2 和 Bootstrap 3 CSS 问题

html - 如何沿顶部水平对齐这些 div?

android - 在线图片加载哪个Android图片库

android - 使用 Canvas 和 canvas.scale(Scale, Scale) 的图像质量问题;

javascript - 如何制作部分滚动的div?

JQuery - 如何获取按比例自动确定的图像的宽度或高度?

css - 关于 CSS 中的 float