How do I position one image on top of another in HTML?

前端 未结 10 1363
难免孤独
难免孤独 2020-11-22 17:15

I\'m a beginner at rails programming, attempting to show many images on a page. Some images are to lay on top of others. To make it simple, say I want a blue square, with

相关标签:
10条回答
  • 2020-11-22 17:20

    Ok, after some time, here's what I landed on:

    .parent {
      position: relative;
      top: 0;
      left: 0;
    }
    .image1 {
      position: relative;
      top: 0;
      left: 0;
      border: 1px red solid;
    }
    .image2 {
      position: absolute;
      top: 30px;
      left: 30px;
      border: 1px green solid;
    }
    <div class="parent">
      <img class="image1" src="https://placehold.it/50" />
      <img class="image2" src="https://placehold.it/100" />
    </div>

    As the simplest solution. That is:

    Create a relative div that is placed in the flow of the page; place the base image first as relative so that the div knows how big it should be; place the overlays as absolutes relative to the upper left of the first image. The trick is to get the relatives and absolutes correct.

    0 讨论(0)
  • 2020-11-22 17:20

    @buti-oxa: Not to be pedantic, but your code is invalid. The HTML width and height attributes do not allow for units; you're likely thinking of the CSS width: and height: properties. You should also provide a content-type (text/css; see Espo's code) with the <style> tag.

    <style type="text/css"> 
    .containerdiv { float: left; position: relative; } 
    .cornerimage { position: absolute; top: 0; right: 0; } 
    </style>
    
    <div class="containerdiv">
        <img border="0" src="http://www.gravatar.com/avatar/" alt="" width="100" height="100">
        <img class="cornerimage" border="0" src="http://www.gravatar.com/avatar/" alt="" width="40" height="40">
    <div>
    

    Leaving px; in the width and height attributes might cause a rendering engine to balk.

    0 讨论(0)
  • 2020-11-22 17:21

    The easy way to do it is to use background-image then just put an <img> in that element.

    The other way to do is using css layers. There is a ton a resources available to help you with this, just search for css layers.

    0 讨论(0)
  • 2020-11-22 17:27

    Here's code that may give you ideas:

    <style>
    .containerdiv { float: left; position: relative; } 
    .cornerimage { position: absolute; top: 0; right: 0; } 
    </style>
    
    <div class="containerdiv">
        <img border="0" src="https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" alt=""">
        <img class="cornerimage" border="0" src="http://www.gravatar.com/avatar/" alt="">
    <div>
    

    JSFiddle

    I suspect that Espo's solution may be inconvenient because it requires you to position both images absolutely. You may want the first one to position itself in the flow.

    Usually, there is a natural way to do that is CSS. You put position: relative on the container element, and then absolutely position children inside it. Unfortunately, you cannot put one image inside another. That's why I needed container div. Notice that I made it a float to make it autofit to its contents. Making it display: inline-block should theoretically work as well, but browser support is poor there.

    EDIT: I deleted size attributes from the images to illustrate my point better. If you want the container image to have its default sizes and you don't know the size beforehand, you cannot use the background trick. If you do, it is a better way to go.

    0 讨论(0)
  • 2020-11-22 17:30

    Inline style only for clarity here. Use a real CSS stylesheet.

    <!-- First, your background image is a DIV with a background 
         image style applied, not a IMG tag. -->
    <div style="background-image:url(YourBackgroundImage);">
        <!-- Second, create a placeholder div to assist in positioning 
             the other images. This is relative to the background div. -->
        <div style="position: relative; left: 0; top: 0;">
            <!-- Now you can place your IMG tags, and position them relative 
                 to the container we just made -->   
            <img src="YourForegroundImage" style="position: relative; top: 0; left: 0;"/>
        </div>
    </div>
    
    0 讨论(0)
  • 2020-11-22 17:31

    One issue I noticed that could cause errors is that in rrichter's answer, the code below:

    <img src="b.jpg" style="position: absolute; top: 30; left: 70;"/>
    

    should include the px units within the style eg.

    <img src="b.jpg" style="position: absolute; top: 30px; left: 70px;"/>
    

    Other than that, the answer worked fine. Thanks.

    0 讨论(0)
提交回复
热议问题