I'm trying to make "meme"-looking captioned images with CSS using an <img> object and two <p> objects styled with CSS. Right now I have a <div> which contains the <img> and then the two <p>'s. What I want to do is have the picture positioned in the top-left corner of the div and then I set the z-index to -1 and THEN somehow position the two p objects over the image but positioned relative to the top-left corner of the div.
This is so I can set the top: y and left: x values of the p objects such that they will be positioned accordingly relative to the top-left of the div and in turn the image.
But when I try to set position: relative for the p objects there's a problem where the first p is correctly placed but the 2nd p is positioned relative to the first one so even though it's at top: 0, left: 0 it's still lower than it should be.
How can I fix this?
check out this jsfiddle:
http://jsfiddle.net/56J8y/1/
relevant CSS
.meme_container {
position: relative;
}
.meme_container .top_meme {
position:absolute;
top:0;
left:0;
}
.meme_container .bottom_meme {
position:absolute;
bottom:0;
left:0;
}
and the html
<div class="meme_container">
<img src="https://www.google.com/images/srpr/logo3w.png"/>
<p class="top_meme">This is a caption 1</p>
<p class="bottom_meme">This is a caption 2</p>
</div>
One way is to use Pseudo element like :after and :before
Example:
img:after {
content: "Hello, I am the caption";
}
I think you are talking about this situation:
<div>
<img />
<p class="first">caption1</p>
<p class="second">caption2</p>
</div>
Then to do what you want you need to set the positioning of the div to something (relative will generaly not affect your behaviour so its a good choise). Once that is done postioning absolute can be used inside. Absolute refers to the next higher positioned element!!
div{position:relative};
p{position:absolute};
p.first{top:10px};
p.second{top:20px};
Related
I want to align images like shown in the Aligned Images link at the bottom of the website. I just mean that I want to place many movies dvd covers on the bottom of the page so it becomes easy to access any movies directly. So if someone hover the movie Blast than it will pop up. Can anyone help me out.
img {
position: absolute;
bottom: 0;
}
And a fiddle: http://jsfiddle.net/dwtp7pje/
Note that if you put the image inside of another div, you need to assign that div a position: relative; for it to work.
Edit: if you're looking to place several images at the bottom, place the images in a div like this:
<div>
<img src="http://orig12.deviantart.net/2b79/f/2008/314/e/8/my___dark_knight___dvd_cover_by_esbe77.jpg" alt="">
<img src="http://orig12.deviantart.net/2b79/f/2008/314/e/8/my___dark_knight___dvd_cover_by_esbe77.jpg" alt="">
<img src="http://orig12.deviantart.net/2b79/f/2008/314/e/8/my___dark_knight___dvd_cover_by_esbe77.jpg" alt="">
</div>
with css:
div {
position: absolute;
bottom: 0;
}
img {
max-width: 33%;
}
And the fiddle: http://jsfiddle.net/dwtp7pje/1/
this is in css
.fix{
position:fixed;
bottom:0px;
}
and your html
<img src="yourimagepath" class="fix"/>
absolute: An absolutely positioned based on its nearest relatively positioned parent element.
fixed: Fixed elements are completely independent on the web page. Regardless of any parents, a fixed position element will always be fixed at a point based on the window of browser
Try the following code.
<img src="http://i.imgur.com/KjuqA1l.png" style="position:fixed;bottom:0px;">
Update:
<img src="http://i.imgur.com/KjuqA1l.png" style="position:fixed;bottom:0px;max-width:100%;">
i have an image and a paragraph on a html page.
the code is as follows:-
<!DOCTYPE html>
<html>
<head>
<style>
img
{
position:absolute;
left:0px;
top:0px;
background-color:red;
z-index:1;
}
p
{
z-index:2;
}
</style>
</head>
<body>
<h1>This is a heading</h1>
<img src="w3css.gif" width="100" height="140" />
<p>This is a sample paragraph for z-index testing</p>
</body>
</html>
here when is put z-index for image lesser than p tag then also it appears above p tag.
can anyone tell me why z-index in not working in my case???
thanks in advance
assign position: relative to <p> for z-index to work.
For OP's Clarification :
from : Any weird rules about z-index I should know about?
In addition, a child can never be below its parent. This example, also on Jsfiddle illustrates it.
Based on the example you added it's clear the trouble you're having:
z-index is only relative to its parent, which in most cases is the document itself. If the z-index of one sibling is lower than another, nothing you change about first sibling's children can move it above its parents sibling. Read more about stacking context if you're interested.
Here is a visual:
Crossed out in red is what you want to do, and it is not possible with CSS (considering those small boxes are children of the bigger box, with markup which might look like this:
<div class="one">
</div>
<div class="two">
<div> I can never be above "one", if my parent "two" isn't. </div>
</div>
A solution would be to move the "overlay" inside the wall, or better yet use a pseudo element (which is rendered as a child of the element it is applied to), because the overlay sounds like it something presentational, and thus should remain in the domain of CSS if an overlay div would add no semantic meaning.
z-index will work only if position property is specified,
so specify position: relative for the p tag
p
{
position: relative;
z-index:2;
}
DEMO
Your p tag needs a position value too, so the z-index can affect him. But in this case you can asign float:left to the image, and place it inside the p tag so the paragraph text will wrap next to the image.
change z-index to -1 for your image
img
{
position:absolute;
left:0px;
top:0px;
background-color:red;
z-index: -1;
}
An element with greater stack order is always in front of an element with a lower stack order.
In your code, the p tag has a stack order of 2, whereas your img tag has a stack order of 1. Your p tag thus has a greater stack order, meaning the text will be shown in front of the image.
If you want the opposite result, try using negative values for both the image and the paragraph.
img {
position: absolute;
left: 0px;
top: 0px;
z-index: -1;
}
p {
position: absolute;
left: 15px;
top: 45px;
z-index: -2;
}
See if the above snippet helps you.
I am using JS to write HTML code where I need to display 2 images exactly overlapped.
The height and width of both are same.
What CSS properties can I use to do this?
Position relative on the container, and absolute on the images:
All of the above answers are missing the fact that you need to position a parent element with something other than static, or else you will be positioning them absolute to the browser window, which I presume you do not wish to do.
position: absolute will give your position in the container of the closest parent with some sort of positioning. So we give the parent position:relative; without declaring top or bottom, this way it will be 0px off from where it would normally be (i.e. no change, but still has position declared).
<div id="container">
<img src="data:image/png;base64,R0lGODlhAQABAPAAAC+byy+byywAAAAAAQABAEAIBAABBAQAOw==" style="height:125px; width:125px;">
<img class="hide" src="data:image/png;base64,R0lGODlhAQABAPAAADCQIzCQIywAAAAAAQABAEAIBAABBAQAOw==" style="height:125px; width:125px;">
</div>
#container{
position:relative;
}
#container img{
position:absolute;
top:0;
left:0;
}
.hide:hover{
opacity:0;
}
http://jsfiddle.net/BLbhJ/1/
Edit: Added your hide functionality
Play around with the css in this:
http://jsfiddle.net/zuZxD/
I used opacity to display the overlapping.
<style>
.imageoverlap{
position: absolute;
top:100px;
}
</style>
...
<div class='imageoverlap'>
image1
</div>
<div class='imageoverlap'>
image2
</div>
Try that :D
If you set position to absolute, you can control where you want to place it.
<style>
#overlay{position:absolute; top:0px;}
</style>
<div id="layer1"><img src="img1.png"></div>
<div id="overlay"><img src="overlay_image.png"></div>
Now you need to position #overlay where you want it, by setting top and left positions, i.e., top:0px, left:300px;
I need to position elements and receive like in attached image
I have a page where all elements are inside MainDiv. There are 2 images.
I would wondering if somebody showed my html + css should be.
thanks in advance!
A possible way would be to set the position of the overlayed image to absolute:
#overlayImage{
position:absolute;
right:0px;
bottom:0px;
width:150px;
height:150px;
}
Important is, that the position of the main div is not "static".
<div id="main_div">
<div id="other div"> </di>
<div id="overlayImage"> </div>
</div
you can accomplish this the following way: assign the background image as background to the main div
background: url(some/url/to/image) no-repeat scroll top right transparent;
then add a normal image element inside that div and position it absolute with the folllowing css
right: 0;
bottom:0;
position: absolute;
make sure the main div has position set to relative
how to find position of an element within a div
eg:
<div style="width:100px;overflow:hidden">
<img id="img_1" src=""/>
<img id="img_2" src=""/>
</div>
<input type="button" onclick"fnscroll()">
consider the second image is hidden.
how to bring that second img in the visible area of the div? at a single button click.
how to find the img position ie, x-y position
using javascript
If you want to position every image in the div on the same place, you might want to consider setting the position style to absolute and alter the z-index of the images via javascript.
Example of the CSS:
div img { position: absolute; top: 0; left:0; }
#img_1 {z-index: 1;}
#img_2 {z-index: 2;}
Replace <ELEMENT> with element in DOM Document
To find x,y coordinates
var x=<ELEMENT>.style.left
var y=<ELEMENT>.style.top
Use same properties to bring image to visible area
And if you want absolute position of image loop over it till you find body tag