如何将图像放在另一个图像的顶部

时间:2010-03-23 20:25:39

标签: html css

在div中,有一张图片在DIV边框的所有方向都应该有10px的边距。在图片的左下角有一个左右图像 只有当它通过jquery加载到DOM中时才会显示图片。 问题在于,左图像的存在使图像向下移位与周围图像的高度一样多的像素。

我正在寻找最清洁的替代方案,将照片保留在DIV中,并仍然在其上方显示左右图像。将图片设置为背景将无法正常工作,因为我需要立即加载图片。

对#about css的任何改进都将不胜感激 下面是一个完整的html页面,可以重现问题

<html>
<head>
<title>Troubleshooting :: align the main picture inside the DIV</title>
<style type="text/css">
html, body  {
   background-color: #000000;
}

#about {
    z-index:2;
    position:relative;
    top:82%;
    left:3%;
}

#pic {
    width:100%;
    height:96%;
}

#main-content-image {
    height:100%;
    margin-right:10px;
    margin-left:10px;
    margin-top:10px;
    margin-bottom:10px;
}

#main-content {
    height:490px;
    border-width: 1px;
    border-style: solid;
    border-color: #777777; 
}
#main-content-image.loading {
  background: url(http://farros.gr/images/ajax-loader2.gif) no-repeat center center;
}

a  {
   text-decoration: none;
   text-decoration: none;
   color: #868686;
   outline:none;
}

.hide {
    display:none;
}
</style>


<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script type="text/javascript">
    <!--
    $(document).ready(function(){    

        $(function () {
            var img = new Image();
            $(img).load(function () {
                $(this).hide();
                $(this).width('100%');
                $(this).height('96%');
                $('#main-content-image').removeClass('loading').append(this);
                $(this).fadeIn();
            }).error(function () {
                // notify the user that the image could not be loaded
            }).attr('src', 'http://farros.gr/images/bg.jpg');
        });

    });
    </script>
</head>
<body>
    <div id="main-content">
        <div id="main-content-image" class="loading">
            <a href="#"><img id="about" src='http://farros.gr/images/about.png' alt='Haris Farros'/></a>
        </div>
    </div>
</body>
</html>

3 个答案:

答案 0 :(得分:4)

使用position:absolute for about pic,你可以把它放在任何你想要的地方。还要确保将#main-content-image设置为position:relative,这样它就成了about图像的参考点。

编辑:我已经用你的html测试了它,它可以工作。

答案 1 :(得分:3)

一个(脏的)javascript解决方案是给包裹你的图像的锚点代替“about”id,并将#about设置为'display:block;'

然后,在图像加载回调中,添加以下语句:

$(this).css({marginTop: "-40px"});

当心挫败者;我不喜欢让标记依赖于javascript代码执行,但这就是他的代码中已经发生的事情。

答案 2 :(得分:1)

绝对定位将实现您的目标:

#main-content-image {
  position: relative; /* Needed so absolute positioning works on child */
}

#about { 
  position: absolute; /* absolutely positioning it takes it out of the document flow */
  bottom: 10px; /* You said you wanted it on the bottom left, right? */
  left: 10px; 
}