【问题标题】:How to place an img on top of another img如何将一个图像放在另一个图像之上
【发布时间】:2012-04-27 10:12:45
【问题描述】:

我正试图在我的网站finegra.in 上将一个放在另一个之上。这是该网站现在的屏幕截图:

我只想将“访问我们的 kickstarter 项目”img 放在此处:

这里是 html 和样式表:

<div id="page" class="hfeed">
    <header id="branding" role="banner">



            <!-- 
<nav id="access" role="navigation">
                <h3 class="assistive-text">Main menu</h3>
                                <div class="skip-link"><a class="assistive-text" href="#content" title="Skip to primary content">Skip to primary content</a></div>
                <div class="skip-link"><a class="assistive-text" href="#secondary" title="Skip to secondary content">Skip to secondary content</a></div>
                                <div class="menu"><ul><li class="current_page_item"><a href="http://www.finegra.in/" title="Home">Home</a></li></ul></div>
            </nav><!~~ #access ~~>
 -->

            <hgroup>
            <div>
                <a href="http://www.finegra.in"><img id="logo" alt="fine grain Logo" src="http://www.finegra.in/wp-content/uploads/2012/04/fineGRAINlogoGOOD-WEB.png">
</a>
<img id="headerimg" alt="The Sheffield Case" src="http://www.finegra.in/wp-content/uploads/2012/04/headerimg2.jpg">
<a href="http://www.kickstarter.com/projects/finegrain/17408941?token=364cb635"><img id="branding" alt="The Sheffield Case" src="http://www.finegra.in/wp-content/uploads/2012/04/checkOurProject.png">
</a>

            </div>
            <div id="site-generator">
                FineGrain LLC Copyright 2012. All Rights Reserved  
            </div>
        </hgroup>
    </header><!-- #branding -->

style.css:

#branding {
    border-top: 0px solid #BBBBBB;
    padding-bottom: 10px;
    position: relative;
    z-index: 9999;
    background: none repeat scroll 0 0 #DADAD2;
}

#headerimg {
    width: 30% !important;
    width: 100% !important;
}

#branding img {
    margin-bottom: 20px;
    width: auto;
}

.one-column #page {
    max-width: 1500px;
}

.one-column #content {
    margin: 0;
    width: auto;
}

【问题讨论】:

    标签: html css image href


    【解决方案1】:

    我认为你可以在 float 的帮助下通过简单的 css 轻松做到这一点:-

    CSS

        #header {
        background: url("http://i.imgur.com/1nrC6.jpg");
        width:1040px;
        height:402px;
    }
    
    .logo {
        float:right;
        margin-top:200px;
    }
    

    HTML

    <div id="header">
    <a href="#"><img class="logo" src="http://i.imgur.com/17oqa.png"border="0"/></a>
    </div>
    

    查看演示:- http://jsfiddle.net/UtKbC/6/

    【讨论】:

      【解决方案2】:

      假设您使用的是 jQuery,您可以在 DOM 就绪函数中为您的图像创建水印,并使其位置绝对并相对于图像计算。

      <script type="text/javascript">
      
          // Watermark image, change the src with your image's url
          var watermark = '<img src="watermark.png" border="0" style="border:none; background-color:transparent; position:absolute; ';
      
          // Position relative to image inside:
          var insideTop = 20;
          var insideLeft = 50;
      
          $(document).ready(function () {
              $("img").each(function (ix, el) {
                  var top = $(this).offset().top + insideTop;
                  var left = $(this).offset().left + insideLeft;
                  var imgWatermark = watermark + 'top:' + top + 'px; left:' + left + 'px;"/>';
                  $("body").append(imgWatermark);
              });
          });
      
      </script>
      

      如果您有带有水印的图像和没有水印的图像,请将属性 class="withWatermark" 添加到将带有水印的图像中,并将脚本中的 $("img") 替换为 $("。带水印”)。

      <img src="myimage.jpg" class="withWatermark" />
      

       

      $(".withWatermark").each(function (ix, el) {
      

      【讨论】:

        【解决方案3】:

        这样的事情怎么样? http://jsfiddle.net/UtKbC/

        CSS:

        .visit_project {
            position: absolute;
            top: 250px;
            left: 400px;
        }
        

        HTML:

        <a href="http://www.kickstarter.com/projects/finegrain/17408941?token=364cb635" class="visit_project"><img id="branding" alt="The Sheffield Case" src="http://www.finegra.in/wp-content/uploads/2012/04/checkOurProject.png">
        </a>
        

        将标题图像包装在它自己的 div 中并根据该 div 包装器将 checkOurProject.png 图像设置为绝对值可能更明智。

        【讨论】:

        • 好的,关于单独的 div 很好。现在我知道这不是我的问题,但是有没有办法让“访问我们的 kickstarter 项目”图像具有透明背景而不是背景颜色方块?这是一个带有透明背景的 png
        • 您是否使用透明背景保存了图像? png 是正确使用的格式。
        • 是的,我做到了,这就是为什么我认为它的行为很奇怪
        • 没关系,我看到你做到了。在#branding {background: none repeat scroll 0 0 #DADAD2;} 中使其更加独特,因此它不包括图像的背景颜色。
        • 我实际上只是决定通过将“访问我们的 kickstarter 项目”和标题图像在 photoshop 中组合成一个 png 来简化它。不过感谢您的提示,下次我会知道的更好。无论如何,您如何看待启动页面?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多