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

前端 未结 10 1472
难免孤独
难免孤独 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;
    }

    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.

提交回复
热议问题