How do I make two images stay relative to eachother? - html

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>

Related

No relative elements - What happens?

Hello,
So I bumped into quite an interesting problem today. I am not sure I got it right, nor am I sure what is actually happening in the background. I did not find any article answering my question. So here it is :
Let's say I have a page very very simple, something like:
<html style="height:100%;">
<body style="height:100%;">
<div style="height:2000px;"></div>
<test style="position:absolute; bottom:0;"></test>
</body>
</html>
So I did not set any element as relative.
What I was expecting :
My test should be positioned relatively to the body element. So at the very end of my page.
What seemed to happen: My div was positioned relatively to the viewport (???)
So instead of being at the very end of the page. it was Xpx from the top.
so if Im on a browser with 900px height, then the element is positioned 900px from the top, and stays there if I scroll.
So sorry if this aint clear. I can specify if needed. Any explanation ?
An absolutely positioned element will be positioned in terms of the nearest relatively positioned ancestor in the flow. Make sure one of the parent wrappers has position set to relative.
In this fiddle the element is at the bottom:
http://jsfiddle.net/L51u8qhz/
<div class="html" style="height:100%;">
<div class="body" style="height:100%; position: relative;">
<div style="height:2000px;"></div>
<div style="position:absolute; bottom:0; background: #000; height: 10px; width: 100px;"></div>
</div>
</div>
From MDN
The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static). If a positioned ancestor doesn't exist, the initial container is used.
Now, the initial container is the box of the size of viewport. It will not move even when you scroll.
So, if there is no containing block for the absolute positioned element, then position will be w.r.t the region bounded by the viewport.
Recommended solution is to set position to a parent (mostly it's body) other than static(mostly it's relative)

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/

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.

HTML/CSS - Put img on top of img?

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.