before and after image with loop - html

I need to display a before and after images on mouse hover.I did edit the code from w3schools image effects.and come up with the CSS.With one Object Containing the before and after image. it works But when there two objects the 2nd Object "after" image displays .in the 1st Object "after" image place.since there can only be one class (Overlay) been used twice(because of the loop) .only the first class(overlay) place.will be used for all further "After" images.How do I get the after image to display with in its own place? thanks for
any information or advice
<style>
.containerImage {
position: relative;
width: 50%;
}
.image {
display: block;
width: 150px;
height: 150px;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 150px;
width: 150px;
opacity: 0;
transition: .5s ease;
}
.containerImage:hover .overlay {
opacity: 1;
}
.text {
display: block;
width: 150px;
height: 150px;
position: absolute;
top: 75px;
left: 75px;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
</style>
<div class="containerImage">
#foreach (var item in Model.Take(2))
{
<img src="~/Img/#item.BeforePic" alt="Avatar" class="image">
<div class="overlay">
<img src="~/Img/#item.AfterPic" alt="Avatar" class="text">
</div>
<br/>
}
</div>
<style>
.containerImage {
position: relative;
width: 50%;
}
.image {
display: block;
width: 150px;
height: 150px;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 150px;
width: 150px;
opacity: 0;
transition: .5s ease;
}
.containerImage:hover .overlay {
opacity: 1;
}
.text {
display: block;
width: 150px;
height: 150px;
position: absolute;
top: 75px;
left: 75px;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
</style>
<div class="containerImage">
<img src="https://images.pexels.com/photos/52533/orange-fruit-vitamins-healthy-eating-52533.jpeg?h=350&auto=compress&cs=tinysrgb" alt="Avatar" class="image">
<div class="overlay">
<img src="https://images.pexels.com/photos/370014/pexels-photo-370014.jpeg?h=350&auto=compress&cs=tinysrgb" alt="Avatar" class="text">
</div>
<br/>
<img src="https://images.pexels.com/photos/566888/pexels-photo-566888.jpeg?h=350&auto=compress&cs=tinysrgb" alt="Avatar" class="image">
<div class="overlay">
<img src="https://images.pexels.com/photos/46174/strawberries-berries-fruit-freshness-46174.jpeg?h=350&auto=compress&cs=tinysrgb" alt="Avatar" class="text">
</div>
<br/>
</div>
<hr/>
<p>The actual "after image" of the Orange but the Strawberry "after image" is showing</p>
<img src="https://images.pexels.com/photos/370014/pexels-photo-370014.jpeg?h=350&auto=compress&cs=tinysrgb"class="image" alt="Avatar">

This is because you have the containerImage with position relative and it is the only wrapper for all images.
This causes all the absolute elements to start at top:0, left:0, from the very first parent with position relative, in your case containerImage.
You can create multiple containerImage and bring it inside the foreach loop:
#foreach (var item in Model.Take(2)){
<div class="containerImage">
<img src="~/Img/#item.BeforePic" alt="Avatar" class="image">
<div class="overlay">
<img src="~/Img/#item.AfterPic" alt="Avatar" class="text">
</div>
</div>
}
And remove the br, since divs has a default display set to block.
Like this you don't have to change your CSS. Alternatively if you can't bring in the div containerImage. Just create another div (name it something like inner-wrapper and give it position relative).
I've created a working codepen.
Hope this helps you!

Related

how to attach picture to another picture?

I want to do synchronising device, it measures phase shift between phases. It's a measurement device, that have a pointer like a clock.
It consists of two images, a frame and arrow, that rotates. but when i try to resize window i have the following:
My html:
<div>
<div>
<div className={classes.sync_point}> . </div>
<img src={arpng} ref={point} alt="arrow" className={classes.arrow} />
<img src={syncFrame} alt="syncframe" className={classes.syncFrame} />
</div>
<div className={classes.btn}>
<Button variant='contained' onClick={calculateResult}>SYNCHRONISE</Button>
</div>
<div>
<h1>{word}</h1>
</div>
</div>
My css:
.syncFrame{
margin-top: 40px;
width: 50%;
height: 50%;
position: absolute;
left: 20vh;
z-index: 1;
}
.arrow{
margin-top: 40px;
width: 50%;
height: 50%;
position: relative;
z-index: 2;
}
How can i bound that images so that when i resize the window or change my device it proportionaly resizes.
<div className="parent">
<div className={classes.sync_point}>
.
</div>
<img src={arpng} ref={point} alt="arrow" className={classes.arrow} />
<img src={syncFrame} alt="syncframe" className={classes.syncFrame} />
</div>
.parent{
height: ....;
width: ....;
border: 1px solid black;
position: relative;
}
img, .sync_point {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 100%;
max-height: 100%;
}
You can use a container width position:relative and set the second image with position:absolute. You can set the pointer as you need with top:0, left:50%, bottom:0 and margin:auto
.container{
background-color:blue;
width: 50%;
position: relative;
}
.clock{
width:100%;
}
.pointer{
position:absolute;
top: 0;
left: 50%;
bottom: 0;
margin: auto;
width: 40%;
transform: rotate(-180deg);
transition-duration: 1s;
transform-origin: left;
}
.container:hover .pointer{
transform: rotate(-45deg);
}
<div class="container">
<img class="clock" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a0/Circle_-_black_simple.svg/500px-Circle_-_black_simple.svg.png" alt="">
<img class="pointer" src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/8d/U%2B2192.svg/250px-U%2B2192.svg.png" alt="">
</div>

How do I align a div element onto an image in a responsive matter?

I'm working on my portfolio site now. My projects are listed as clickable images, arranged into two columns. I want it that when a user hovers over an image, a solid block of color covers it with the project title in the center. Basically like this: https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_image_overlay_fade.
I had it working perfectly when I used row/column css classes, but since I've found that to not be very responsive (on mobile, the first column gets stacked on top of the other, which makes sense but that's not the order I want), I aligned the images with float and padding instead. Now, the hover effect/link stretches across the entire page, instead of being contained in the image area.
Here's my code for reference:
CSS
.container {
position: relative;
}
.image {
display: block;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
height: 100%;
width: 100%;
transition: .5s ease;
}
.container:hover .overlay {
opacity: 1;
}
.text {
color: white;
font-size: 30px;
font-family: "Lato-Bold";
position: absolute;
top: 50%;
left: 50%;
-webkit-transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
transform: translate(-50%, -50%);
text-align: center;
}
#media screen and (min-width: 1025px) {
.image {
float: left;
width: 45%;
padding-left: 40px;
padding-right: 15px;
padding-bottom: 30px;
}
}
HTML
<div class = "container">
<img src="image name" class = "image">
<div class="overlay" style = "background-color: #color;">
<div class="text">project title</div>
</div>
</div>
How can I fix this? I've tried adding display: block to the .overlay class. I've tried making the .overlay class the same size as the image. I've also tried wrapping the link around the container class instead of the other way around like it is now. Nothing happens at all. I've also tried adjusting the container size, but that shrunk the images and stacked them into 1 column :(
Read about Responsive Images and Flexbox
Try the following code.
Note: I changed the HTML structure slightly.
.container {
display: flex;
justify-content: center;
flex-wrap: wrap;
}
.thumb {
position: relative;
width: 50%;
}
.thumb img {
width: 100%;
}
.thumb:hover .overlay {
opacity: 1;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
opacity: 0;
height: 100%;
width: 100%;
transition: .5s ease;
color: #000;
background: #fff;
}
.overlay .text {
position: absolute;
top: 50%;
transform: translate(-50%);
left: 50%;
text-align: center;
}
<div class="container">
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
<a href="#" class="thumb"><img src="https://loremflickr.com/320/240/dog" alt="" />
<div class="overlay">
<div class="text">project title</div>
</div>
</a>
</div>

Align different images surrounding a circle and revolve on orbit and display text in circle on hover with bootstrap 4

I am trying to make a circle and there will be different images surrounding circle. And the images will keep revolving around the circle on an orbit like planets. On hover of those images, I want revolving to stop and I am trying to display some different text in the centre of circle. I have draw a perfect circle, but I am having trouble in aligning those images around the circle and also making it revolving around the circle for infinity. Below image is an example of it.
So basically the smaller circle with numbers are different images and on hover of these images some different texts will be displayed in the centre of big circle and the revolution to be stop. I am also trying to make it responsive so I have added it in a container and trying to put it in col-3.
I am having issue with aligning these images and also displaying the text in the centre of circle. This is what I have tried so far
HTML
<div class="col-3 mx-auto" style="background-color: grey;">
<img src="images/location/2x/location.png" style="float: left; ">
<div class="circle">
<div class="text-center">
</div>
</div>
</div>
CSS
.circle{
width: 80%;
height:0;
padding-bottom: 80%;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
background: #4679BD;
}
What would be the better way to achieve this ?
Any help ?
Here is my solution,
Its not perfect, but It will give u a starting point.
The dyanamic text can be passed from the data-text attribute
<img data-text="A" src="http://placehold.it/50x50" alt="">
In order to get this text, I have added mouseover eventlistener to each images. And when we hover over the images, the text is fetched and updated from the data-text attribute using
sampleText.innerHTML=this.getAttribute('data-text');
To stop the animation on hover, I pause the animation using
.image-holder:hover {
animation-play-state: paused;
}
var images = document.getElementsByTagName('img');
var sampleText = document.getElementById('sample-text');
for (var i = 0; i < images.length; i++) {
images[i].addEventListener("mouseover", updateName)
}
function updateName() {
sampleText.innerHTML = this.getAttribute('data-text');
}
body {
padding: 100px;
}
.parent {
position: relative;
}
.circle {
width: 200px;
height: 200px;
border-radius: 50%;
background-color: teal;
display: flex;
justify-content: center;
align-items: center;
}
* {
box-sizing: border-box;
}
.image-holder {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
/*animation: rotate linear 5s infinite forwards;*/
animation-name: rotate;
animation-duration: 5s;
animation-iteration-count: infinite;
animation-timing-function: linear;
animation-fill-mode: forwards;
}
.image-holder:hover {
animation-play-state: paused;
}
.image-holder img {
position: absolute;
width: 50px;
height: 50px;
border-radius: 50%;
}
.image-holder img:nth-child(1) {
top: -15px;
left: -15px;
}
.image-holder img:nth-child(3) {
top: -15px;
right: -15px;
}
.image-holder img:nth-child(5) {
bottom: -15px;
right: -15px;
}
.image-holder img:nth-child(7) {
bottom: -15px;
left: -15px;
}
.image-holder img:nth-child(2) {
top: -50px;
left: 50%;
transform: translateX(-50%);
}
.image-holder img:nth-child(4) {
top: 50%;
right: -50px;
transform: translateY(-50%);
}
.image-holder img:nth-child(6) {
bottom: -50px;
left: 50%;
transform: translateX(-50%);
}
.image-holder img:nth-child(8) {
top: 50%;
left: -50px;
transform: translateY(-50%);
}
#keyframes rotate {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row">
<div class="parent">
<div class="circle" id="sample-text">
Sample Text
</div>
<div class="image-holder">
<img data-text="A" src="http://placehold.it/50x50" alt="">
<img data-text="B" src="http://placehold.it/50x50" alt="">
<img data-text="C" src="http://placehold.it/50x50" alt="">
<img data-text="D" src="http://placehold.it/50x50" alt="">
<img data-text="E" src="http://placehold.it/50x50" alt="">
<img data-text="F" src="http://placehold.it/50x50" alt="">
<img data-text="G" src="http://placehold.it/50x50" alt="">
<img data-text="H" src="http://placehold.it/50x50" alt="">
</div>
</div>
</div>
</div>
Try using position elements to position you circle and text like so
.circle{
width: 300px;
height:300px;
-moz-border-radius: 50%;
-webkit-border-radius: 50%;
border-radius: 50%;
background:white;
position: relative;
}
span{
color: red;
position: absolute;
left: 130px;
top: 50%;
}
img{
position: absolute;
left: 270px;
top: 45px;
}
<div class="col-3 mx-auto" style="background-color: grey;">
<img src="images/location/2x/location.png" style="float: left">
<div class="circle">
<span>Hello</span>
</div>
</div>

How to remove automatically generated HTML container margin around image

I'm trying to remove the automatically generated container margin around this image. Below is the code I used to produce it. You can view the website here. I tried to add a margin and padding item to the body element, but it didn't resolve the issue.
.container {
position: relative;
width: 240px;
height: 240px;
}
.image {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
}
.container:hover .overlay {
opacity: 0.85;
}
.text {
color: white;
font-size: 20px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
<div class="container">
<img src="./img/headshots/Exec_DMoon.jpg" width="240" height="240" alt="Photo of David Moon, Assistant Vice President for Financial Affairs" class="image">
<div class="overlay">
<div class="text"><b>David Moon</b> Assistant Vice President for Financial Affairs, <a class="usa-external_link" target="_blank" href="mailto:davidmoon826#gwmail.gwu.edu">Email</a></div>
</div>
</div>
This is the desired output:
What am I doing wrong?
The easiest fix for this, imo: wrap the items you want in a grid in a div and give the div display: flex and flex-wrap: wrap. Good luck!
Well, just add float: left to .container
(to achieve what you show under "this is the desired output")
The answer from Johannes almost worked, but it caused issues where text would reposition itself into the open gaps (see image below), instead of formatting below all the images.
The solution was to use display: inline-block; in .container, as Adrian recommended.
<!DOCTYPE html>
<html>
<head>
<style>
.container {
position: relative;
width: 50%;
height: 50%;
display: inline-block;
}
.image {
display: block;
width: 100%;
height: auto;
}
.overlay {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
height: 100%;
width: 100%;
opacity: 0;
transition: .5s ease;
background-color: #008CBA;
}
.container:hover .overlay {
opacity: 0.8;
}
.text {
color: white;
font-size: 25px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
-ms-transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<h2>Fade in Overlay</h2>
<p>Hover over the image to see the effect.</p>
<div class="container">
<img src="img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<img src="img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<div class="container">
<img src="img_avatar.png" alt="Avatar" class="image">
<div class="overlay">
<div class="text">Hello World</div>
</div>
</div>
<h2>Fade in Overlay</h2>
<p>Hover over the image to see the effect.</p>
</body>
</html>

Placing close button in top rigth corner of an image with different sizes

I have a <div> which includes two <img>, one is the close button, the other one is a roommap. However, the roommap size is always different, so putting the close button in an absolute position doesnt seem to work.
Does anybody has an idea how I could achieve that the close button is based on the roommap size and always in top right corner? Also, the whole <div> is a popup which is centered in the middle of the screen.
.cont2 {
position: relative;
}
.cont2 .img2 {
position: absolute;
margin-top: 10%;
right: 10%;
z-index: 2;
}
.cont2 .img1 {
position: absolute;
margin-top: 15%;
left: 50%;
z-index: 1;
border: 4px solid black;
border-radius: 5px;
-webkit-transform: translateX(-50%);
-moz-transform: translateX(-50%);
-transform: translateX(-50%);
}
<div class="cont2" id="popImg">
<img class="img2" width="40px" id="closebutton" src="https://placehold.it/40x40&text=Button">
<img class="img1" onclick="point_it(event)" id="roomchoose" src="https://placehold.it/150x150&text=Image" />
</div>
Ok, your issue is you need a div to just hold the image and be the same size so you can position your cross relative to the image size.
Try the following (I have added an extra image-holder div but if you don't want this, just make your main cont2 div inline-block):
.cont2 {
position: relative;
}
.cont2 .img2 {
position: absolute;
top: 10%;
right: 10%;
z-index: 2;
border:1px solid red;
}
.cont2 .image-holder {
display: inline-block;
position: absolute;
margin-top: 15%;
left: 50%;
border: 4px solid black;
border-radius: 5px;
transform: translateX(-50%);
}
.cont2 .image-holder img {
display: block
}
<div class="cont2" id="popImg">
<div class="image-holder">
<img class="img2" width="40px" id="closebutton" src="https://placehold.it/40x40&text=Button">
<img class="img1" onclick="point_it(event)" id="roomchoose" src="https://placehold.it/150x150&text=Image" />
</div>
</div>
I had to refactor your css a little but here is a working version:
#popImg {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
width: 200px;
height: 200px;
}
img {
position: absolute;
}
#closebutton {
right: 0;
top: 0;
z-index: 2;
cursor: pointer;
}
#roomchoose {
width: 100%;
height: auto;
z-index: 1;
}
<div class="cont2" id="popImg">
<img class="img2" id="closebutton" src="http://placehold.it/50x40/ff0000/ffffff">
<img class="img1" onclick="point_it(event)" id="roomchoose" src="http://placehold.it/500x400" />
</div>
The main changes are that the #popImg is now the element that is being offset to the centre. This was the main cause of your problems before, because the #closebutton had no relationship with the #roomchoose.
Now you use block wrapper, it has width 100%. Use inline-block wrapper for your image. And don't use position absolute for main image, it doesn't make width for parent div:
.cont2 {
position: relative;
text-align:center;
}
.cont2 .img2 {
position: absolute;
margin-top: 10%;
right: 10%;
z-index: 2;
}
.cont2 .img1 {
position: relative;
margin-top: 15%;
z-index: 1;
border: 4px solid black;
border-radius: 5px;
}
.wrapper{
display:inline-block;
position:relative;
}
<div class="cont2" id="popImg">
<span class="wrapper">
<img class="img2" width="40px" id="closebutton" src="https://placehold.it/40x40&text=Button">
<img class="img1" onclick="point_it(event)" id="roomchoose" src="https://placehold.it/150x150&text=Image" />
</span>
</div>