HTML/CSS - Put img on top of img? - html

I have two png's. One is the actual image, another is a mostly transparent image with a sticker price icon in the top right. I know I could combine these in photoshop and just create one image.
But I need these to be generated dynamically, for a bunch of different base images.
Is there some way to code the "actual image" and then use code to overlay the "transparent sticker image"?

Sure, the easiest way would be to absolutely position both images within their container:
<div style="position:relative">
<img src="main-image.jpg" style="position:absolute;"/>
<img src="overlay-image.png" style="position:absolute;"/>
</div>
The position:relative on the container is needed for absolute positioning of children to work. Of course, if the container is itself absolutely positioned already, then that's fine.
The position:absolute is not needed on the base image if both images are in the top-left corner, but having it allows you to tweak its placement if needed.
You could also use static position on the main image and relative position on the overlay image:
<div style="position:relative">
<img src="main-image.jpg" style="width:100px"/>
<img src="overlay-image.png" style="position:relative; left:-100px"/>
</div>
but for this to work you'd need to know the width of the base image.

Wrap the images in a <div> with the overlay image first and the actual image second, and can set the css of the div to position: relative.
The two images can then be given the css {position: absolute; top: 0; left: 0;}.
<div style="position:relative;">
<img src="overlay.png" style="position: absolute; top: 0; left: 0;">
<img src="actual.png" style="position: absolute; top: 0; left: 0;">
</div>`
If you really want to be safe, you can set the z-index of each image.

You will need to set the position attribute to relative or absolute, set the left and top attributes to the desired values, then set the z-index attribute to 1 (assuming that you don't have other z-index properties already set). Keep in mind that the place where the image is supposed to render without the changed top and left attributes, there will be a space where it should be.

Related

Relative div-Container shall have the height of the absolute img inside it

<div id="mainPic" style="width:50%;">
<img id="pic1" src="#" width="100%">
<img id="pic2" src="#" width="100%">
</div>
I have two images inside a div#mainPic that has a 50% width of his parent. The images will use the full width not more not less and autoscale the height.
The mainPic shall now have the same height like the images.
The problem is that I have to set the images absolute, because I want one above the other. So both have the rules:
position : absolute;
top : 0px;
left : 0px;
But now the div#mainPic is not dynamicaly having the height from the autoscaled images.
Javascript is also out because JS is setting the height only one time not every time you resize the window.
Absolute positioning sets the position of element relative to the first non-static parent element. You probably haven't set the position of #mainPic, which sets it's default value to static. This means if you'll change the position of the child elements #pic1 and #pic2 to absolute then they'll be given the position absolute relative to the body. This can simply be solved by giving a non-static position to #mainPic.
Example fiddle : https://jsfiddle.net/thinker3197/n4kn2wLa/

Image Position on an HTML/CSS Website

I have an HTML Document and a CSS Document and I'm trying to remove a large margin in between the image I've placed and the text below it. When I adjust the pixel margin on the top of the image it adjusts, but I've tried a few things on the bottom of the image and just can't seem to get it to work right. There is a huge gap and I just want it to look "normal". That is, having a smaller margin between the img and the text below it.
<div id="header" class="container">
<!--Home Logo -->
<a href="#">
<img src="http://www.satckoverflow.com/wp-content/uploads/2015/02/Stack-Overflow-Logo.png" style="position: relative; top: -150px;" alt="Platinum Imprints">
</a>
<p>Welcome! Here at Platinum Imprints we specialize in Custom Screen Printing. Use anywhere from one to many colors on a simple shirt.</p>
This is what I have for HTML.
You have inline styles on the img element that are setting relative positioning. Removing this will fix your problem.
Remove the inline style position: relative; top: -150px; in the img tag to remove the spacing.

What is the difference in behavior of this two pairs of tags(see in)?

First example
<div style="position: relative">
<div style="position: relative; top: 10px">text</div>
</div>
Second example
<div style="position: relative">
<div style="position: absolute; top: 10px">text</div>
</div>
The first will position the inner div 10px top relative to where it would be positioned.
The second will position the inner div 10px top to where it would be positioned ignoring the padding and border of the outer div, and removing it from the flow of the document.
Well an absolutely positioned element does not take up space in the dom so the outside div would have no height. That's one thing it does. I would encourage you to read up on it and just test things out in a codepen though, this isn't really the place for this.
The first will leave a "ghost" behind of the moved element. The second takes it fully out of the flow of the document.

How can one layer two aligned images in a scrolling <div> using CSS?

I would like to layer two aligned images in a scrolling <div>.
At first I tried:
<div style="width:300; height:300; overflow:scroll;">
<img src="bottom.jpg"
style="width:400; height:800">
<img src="top.png"
style="width:400; height:800; position:absolute; top:0; left:0;">
</div>
(this is a simplified example -- the actual site has a separate CSS sheet etc.)
I would like the two images to behave as one when the <div> is scrolled, but the "absolute" positioning of the second image causes it not to scroll at all and to go outside the borders of the <div>.
I have also tried changing the style of the second image to:
<img src="top.png"
style="width:400; height:800; position:relative; top:-800; left:0;">
but then there are 800px of extra white space in the bottom of my <div>.
Is there any CSS I can use on the second image that will align it on top of the first image and still allow both of them to be scrolled together?
[update] The working solution is at ozake.com
you just need to add position: relative to the parent (div). When you use position:absolute if you do not contain that element in a parent set to relative it will contain itself within the body. SO what's happening is top.jpg is scolling with it's parent div but bottom.jpg is staying with the body. Once you contain them both inside the parent, then you can set the 2nd image to top: 800px to align it just below the other image
<div style="width:300; height:300; overflow:scroll; position: relative;">
<img src="top.jpg" style="width:400; height:800"/>
<img src="bottom.jpg" style="width:400; height:800;position:absolute; top:800px; left:0;"/>
</div>
FIDDLE
Do you want this: Jsfiddle
If so, here you go:
<div style="width:300px; height:300px; overflow:scroll;">
<div id="container" style="position: relative">
<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/c/ce/Petrus_Christus_-_Portrait_of_a_Young_Woman_-_Google_Art_Project.jpg/785px-Petrus_Christus_-_Portrait_of_a_Young_Woman_-_Google_Art_Project.jpg" style="width:400px;">
<img src="http://pixabay.com/static/uploads/photo/2014/04/02/17/07/hat-308003_640.png" style="width:250px; position:absolute; top:75px; left: 125px;">
</div>
</div>
Use some units in your style, for example pixels.
If you are styling element with position absolute it will be positioned relatively to the document itself, so you should set position: relative to a parent element to bind absolutely positioned element to this parent element and not to the whole document. In our case we made a container for our images with position: relative.
So we have the #container which is scrolling inside our overflow:scroll div, and our image aligned relative to this container.

How do I make two images stay relative to eachother?

I'd like to tie the two logos at the bottom together so they are in the same position relative to the text.
I'd preferably prefer some kind of explanation as well as just code so I can learn from this.
Relevant HTML:
<div id="twitterdiv"><img id="twitter" src="images/Twitter_white.png" ></div>
<div id="instagramdiv"><img id="instagram" src="images/instagram_white.png"></div>
Relevant CSS:
#instagramdiv {
right: 50%;
position: absolute
}
#twitterdiv {
position: absolute;
right: 55%;
}
Here absolutely positioned element will do the trick. In fact, this is typical situation when one element must be positioned such as "sticky" in relation to some other. Keeping in mind definition of absolute position "The element is positioned relative to its first positioned (not static) ancestor element", all you need to do is wrap your "l" letter and images in the same container (parent), then give the parent container relative position and your images - absolute position and finally set left and top properties (if needed): jsfiddle
<div>app<span class="parentContainer">l
<div class="positionedContainer">
<img id="twitter" src="images/Twitter_white.png"/>
<img id="instagram" src="images/instagram_white.png" />
</div>
</span>
elemontomato</div>