7

I have three images which I want to overlay (with HTML + CSS, I do not want to use javascript if possible):

image1 image2 image3

This is the result which I would like to achieve: My preferred result
[image4]

This is what I have tried:

CSS:

.imageContainer {
    position: relative;
}

#image1 {
    position: absolute;
    top: 0;
    z-index: 10;

    border: 1px solid blue; 
}
#image2 {
    position: absolute;
    top: 0;
    z-index: 100;

    border: 1px solid fuchsia; 
}
#image3 {
    position: absolute;
    top: 0;
    z-index: 1000;

    width: 10%;
    height: 10%;

    border: 1px solid green; 
}

HTML:

<div class="imageContainer">
    <img id="image1" src="https://i.sstatic.net/Es4OT.png"/>
    <img id="image2" src="https://i.sstatic.net/WQSuc.png"/>
    <img id="image3" src="https://i.sstatic.net/Xebnp.png"/>
</div>​

image1: "main" image (image1 should set max height and max width for an imageContainer - se HTML above) [blue border]
image2: horizontal-align: center; and top: 0; relative to image1 [pink border]
image3: resized by 10% from its' origin size, horizontal-align: center; relative to image1 [green border]

My error prone HTML + CSS resulted in this: enter image description here

I can't figure out how my CSS should be. What should I do to achieve a result like [image4]?

7
  • What browsers do you need to support?
    – steveax
    Commented Jul 11, 2012 at 23:44
  • As many as possible but if I have to choose: FF and Chrome
    – Hauns TM
    Commented Jul 11, 2012 at 23:56
  • Where are your z-index's for any of the elements? You might add them in
    – Lawrence
    Commented Jul 12, 2012 at 4:19
  • 1
    +1 for an attractive question.
    – sgowd
    Commented Jul 12, 2012 at 5:21
  • 1
    I have add css3 to tags becouse If you want manipulate this elements with only html and css you need css3. Commented Jul 12, 2012 at 7:00

2 Answers 2

6

You can make it with one div and more background for example:

#someDiv 
{
  background: url('topImage.jpg') center,
            url('imageInTheMiddle.jpg') 0px 0px,
            url('bottomImage.jpg') /*some position*/;
}

It was the easier way to display it. Of course in place where I placed positioning values you must add yours one.

UPDATE

In the case which you say after, I think you can use 3 absolute positioning divs with your backgrounds and manipulate them with css3 transform attribute. It gives you possibility to rotate, scale and much more with your elements. And you can also manipulate it with javascript.

More info about css3 transform

3
  • Thanks for your answer! There is absolute nothing wrong with it but I need to have access to the protractor image ([image2] with my naming convention). Why? The preferred result, [image4], displays a simplified part of a major task: The protractor should be able to rotate. In the next step I'll be using that behaviour to indicate an surveillance camera's current turn angle. Is it possible to access imageInTheMiddle.jpg with your proposal in anyhow?
    – Hauns TM
    Commented Jul 12, 2012 at 6:08
  • 2
    Whaa?? I didn't know you could put multiple url()s. Awesome. Commented Jul 12, 2012 at 6:17
  • @Hauns TM sorry but my english is not as good as I can understand all what you wrote but I will try to answer to parts which I understand. I am actually not sure If you can transform (rotate in your case) only one of the backgrounds. I think It can be impossible. Commented Jul 12, 2012 at 6:25
2

make the images transparent

UPDATED the code AGAIN for transperancy issue UPDATED code for IMAGE ROTATION I have applied image rotation for protractor image(image2) hope that helps you

<style type="text/css">
    .imageContainer {
        position: relative;
        width:165px;
        height:169px;
    }

    #image1 {
        position: absolute;
        top: 0;
        z-index: 10;
        width:120px;
        height:120px;
        border: 1px solid blue; 
    }
    #image2 {
        position: absolute;
        top: 0;
        z-index: 20;
        border: 1px solid fuchsia;
        opacity:0.6;
    filter:alpha(opacity=60); /* For IE8 and earlier */


    /*for adding image rotation */
       -webkit-transform: rotate(90deg); /*//For chrome and safari*/
        -moz-transform: rotate(90deg);  /*//For ff*/
        filter: progid:DXImageTransform.Microsoft.BasicImage(rotation=1); /*// For ie */
    /* End for adding image rotation */



    }
    #image3 {
        position: absolute;
        top: 0;
        z-index: 30;
        width: 40%;
        height: 40%;
        border: 1px solid green; 
        left:70px;
        opacity:0.9;
    filter:alpha(opacity=90); /* For IE8 and earlier */


    }

    </style>






    <form name="frmabc"  action="" method="post">
<div class="imageContainer">
    <img id="image1" src="Es4OT.png"/>
    <img id="image2" src="WQSuc.png" />
    <img id="image3" src="Xebnp.png" />
</div>
</form>
4
  • Is it possible to avoid fix width and height on [image1] resp. [image3] and use a relative variant like % (all relativities to [image1])?
    – Hauns TM
    Commented Jul 12, 2012 at 6:20
  • 1
    do you mean to say based on the clock image?? because clock image's dimensions are more than other images. if you give dimension's of clock image to .imageContainer. you will be able to set dimensions of rest of other images in terms of %. I updated the code above for that.
    – Parag
    Commented Jul 12, 2012 at 6:36
  • Well, the final issue (which I haven't mentioned in my original question) is to indicate an surveillance camera's current turn angle. All of this should work in normal browsers (with large screens) and with smartphones (with small screens). That's why I wan't to use % in front of pixels. ----- I will have three photos for this: A surveillance image (portrayed by the butterfly image above), a protractor image, an arrow image. The surveillance image is updated every second via javascript, protractor angle depends on the camera's position, the arrow will always point upward.
    – Hauns TM
    Commented Jul 12, 2012 at 6:51
  • 1
    Hauns TM , I have updated the code above for image rotation of protractor.see if it helps.
    – Parag
    Commented Jul 12, 2012 at 9:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.