The html is given below
.container {
width: 90%;
background: #eee;
margin: 10px auto;
position: relative;
text-align:center;
}
#cf {
background: green;
position: relative;
height: 360px;
width: 640px;
display:inline-block;
margin: 20px;
}
#cf img {
position:absolute;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#cf img.top:hover {
opacity:0;
}
<div class="container">
<div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div>
</div>
The background is properly aligned but the images are offset . What have i done wrong ? Why is the image not being positioned properly?
All the images used are of the dimension 360px height and 640px width.
The images are offset because you are using text-align: center on the container which means that the origin for your positioned child elements is no longer 0 0, but 50% 0 (centre top):
.container {
...
text-align:center;
}
To get around this you just need to override this for the child elements:
#cf {
..
text-align: left;
}
Demo below
.container {
width: 90%;
background: #eee;
margin: 10px auto;
position: relative;
text-align:center;
}
#cf {
background: green;
position: relative;
height: 360px;
width: 640px;
display:inline-block;
margin: 20px;
text-align: left;
}
#cf img {
position:absolute;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
#cf img.top:hover {
opacity:0;
}
<div class="container">
<div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div id="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div>
</div>
But your HTML is invalid. IDs must be unique. You should be using a class for your cf element like so...
.container {
width: 90%;
background: #eee;
margin: 10px auto;
position: relative;
text-align:center;
}
.cf {
background: green;
position: relative;
height: 360px;
width: 640px;
display:inline-block;
margin: 20px;
text-align: left;
}
.cf img {
position:absolute;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
.cf img.top:hover {
opacity:0;
}
<div class="container">
<div class="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div class="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div class="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div><!--
--><div class="cf">
<img class="bottom" src="http://placehold.it/640x360" />
<img class="top" src="http://placehold.it/640x360" />
</div>
</div>
Related
Trying to make these pictures align in the center but every time i try to add alignment or margins they wont move, not sure if i added properties that mean it cannot move. I have tried to change the position to fixed on the img but it still wont center, only wany i can find is to manually position it with margins, however that is obviously not ideal.
Any help is greatly appreciated!
Heres the code on JSFiddle to make it a bit easier
https://jsfiddle.net/dzk59p3u/3/
<html>
<head>
</nav>
<div class="box1">
<!-- <h1>W I L D</h1>-->
<div class="cf">
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" style="width:400px;height:420px" />
</div>
</div>
</div>
<div class="cd">
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" style="width:400px;height:420px" />
</div>
</div>
</div>
CSS
.cf {
position: absolute;
align-items: center;
}
.cf p {
position: absolute;
color: rgb(255, 255, 255);
background-color: rgb(5, 5, 5);
top:100%;
left:50%;
transform:translate(-50%, -50%);
}
.bottom {
float: left;
width: 33.33%;
margin-top: 50px;
margin-left: 50px;
}
.bottom img {
width: 25%;
margin-top: 50px;
margin-left: 50px;
}
.cd img {
display:block;
align-content: center;
position: relative;
float: left;
width: 33.33%;
margin-top: 50px;
margin-left: 50px;
z-index: -1;
}
.cf img {
display:block;
align-content: center;
position: relative;
float: left;
width: 33.33%;
margin-top: 50px;
margin-left: 50px;
-webkit-transition: opacity 1s ease-in-out;
-moz-transition: opacity 1s ease-in-out;
-o-transition: opacity 1s ease-in-out;
transition: opacity 1s ease-in-out;
}
.cf img.top:hover {
opacity:0;
}
Put your images inside a container, and use display:flex on it. Setting align-items and justify-content to center will vertically and horizontally align the direct children of this container.
Then I'd put an additional container for each of your hover images. Set it to position:relative so its children are positioned relatively to it.
Finally, absolutely position your images inside your hover-container. I'd also use object-fit:cover so they don't stretch but are the same size.
.container {
display: flex;
align-items: center;
justify-content: center;
flex-direction:column;
min-height: 100vh;
}
.hover-container {
position: relative;
width: 400px;
height: 270px;
}
img.top,
img.bottom {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit:cover;
}
img.top {
transition: opacity 1s ease-in-out;
z-index:1;
}
img.top:hover {
opacity: 0;
}
<div class="container">
<div class="hover-container">
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" />
</div>
<div class="hover-container">
<img class="top" src="https://ichef.bbci.co.uk/news/976/cpsprodpb/12A9B/production/_111434467_gettyimages-1143489763.jpg" alt="sometext" />
<img class="bottom" src="https://s3.amazonaws.com/cdn-origin-etr.akc.org/wp-content/uploads/2017/11/22130127/Pug-puppy-standing-in-profile-on-a-white-background.jpg" alt="sometext" />
</div>
</div>
These images are using a CSS code for hovering effects, everything works good except i have no idea how to re arrange their position on the page.
Right now they are horizontally on top of each other, how can i make them side by side?:
body {
color:#000000;
background-color:#000000;
background-image:url('Background Image');
background-repeat:no-repeat;
}
a { color:#0000FF; }
a:visited { color:#800080; }
a:hover { color:#008000; }
a:active { color:#FF0000; }
#cf {
position:relative;
height:281px;
width:450px;
margin:0 auto;
}
#cf img {
position:absolute;
left:0;
-webkit-transition: opacity 200ms ease-in-out;
-moz-transition: opacity 200ms ease-in-out;
-o-transition: opacity 200ms ease-in-out;
transition: opacity 200ms ease-in-out;
}
#cf img.top:hover {
opacity:0;
}
<a href="http://example.com/page1">
<div id="cf">
<img class="bottom" src="images/img_a_1.jpg" />
<img class="top" src="images/img_a_2.jpg" />
</div></a>
<a href="http://example.com/page2">
<div id="cf">
<img class="bottom" src="images/img_b_1.jpg" />
<img class="top" src="images/img_b_2.jpg" />
</div></a>
<a href="http://example.com/page3">
<div id="cf">
<img class="bottom" src="images/img_c_1.jpg" />
<img class="top" src="images/img_c_2.jpg" />
</div></a>
As arhak has previously commented, we do not use an id more than once on a page, they must be unique. Here are the following changes:
Changed each #cf to class .cf.
Added display:table-cell to each .cf so that they are side-by-side.
Changed each .cf dimensions to something manageable 50 x 50px.
Replaced non-functioning <img> src to working values*.
Decreased the size of each .cf to 50 x 50px*.
All of these changes are optional with the exception of the first and second changes.
*I know that your relative urls work for you, but an example that doesn't work here will make things harder to demonstrate. In the future if you have images, use those or images of equivalent size in your examples. If we don't know that 50 x 50px is insufficient for your requirements, that will slowdown the process in finding a resolution.
SNIPPET
body {
color: #fff;
background-color: #000000;
background-image: url('Background Image');
background-repeat: no-repeat;
}
a {
color: #0000FF;
}
a:visited {
color: #800080;
}
a:hover {
color: #008000;
}
a:active {
color: #FF0000;
}
.cf {
position: relative;
height: 50px;
width: 50px;
margin: 0 auto;
display:table-cell;
}
.cf img {
position: absolute;
left: 0;
-webkit-transition: opacity 200ms ease-in-out;
-moz-transition: opacity 200ms ease-in-out;
-o-transition: opacity 200ms ease-in-out;
transition: opacity 200ms ease-in-out;
}
.cf img.top:hover {
opacity: 0;
}
<body>
<a href="http://example.com/page1">
<div class="cf">
<img class="bottom" src='http://placehold.it/50x50/00f/fff?text=1'>
<img class="top" src='http://placehold.it/50x50/cf0/000?text=2'>
</div>
</a>
<a href="http://example.com/page2">
<div class="cf">
<img class="bottom" src='http://placehold.it/50x50/0e0/110?text=3'>
<img class="top" src='http://placehold.it/50x50/0bb/011?text=4'>
</div>
</a>
<a href="http://example.com/page3">
<div class="cf">
<img class="bottom" src='http://placehold.it/50x50/fff/000?text=5'>
<img class='top' src='http://placehold.it/50x50/f00/fff?text=6'>
</div>
</a>
</body>
body {
color:#000000;
background-color:#000000;
background-image:url('Background Image');
background-repeat:no-repeat;
}
a { color:#0000FF; }
a:visited { color:#800080; }
a:hover { color:#008000; }
a:active { color:#FF0000; }
#cf img {
text-align: center;
}
#cf img.top:hover {
opacity:0;
}
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<body>
<a href="http://example.com/page1">
<div class="col-xs-12 form-group" id="cf">
<img class="bottom col-xs-6" src="images/img_a_1.jpg" />
<img class="top col-xs-6" src="images/img_a_2.jpg" />
</div></a>
<a href="http://example.com/page2">
<div class="col-xs-12 form-group" id="cf">
<img class="bottom col-xs-6" src="images/img_b_1.jpg" />
<img class="top col-xs-6" src="images/img_b_2.jpg" />
</div></a>
<a href="http://example.com/page3">
<div class="col-xs-12 form-group" id="cf">
<img class="bottom col-xs-6" src="images/img_c_1.jpg" />
<img class="top col-xs-6" src="images/img_c_2.jpg" />
</div></a>
</body>
I am building a site that requires 6 images side by side and when you hover each image you should be able to see a larger image.
Currently I have managed to write the following code that shows 2 images with the hover feature. The problem I have is that the images appear on top of each rather than side by side. I have tried adding a float:left but then the 2nd image doesn't show up the hover image and I am not sure how I can fix this.
The following is the HTML and CSS:
.Enlarge {
position: relative;
}
.Enlarge span {
position: absolute;
left: -9999px;
visibility: hidden;
opacity: 0;
-webkit-transition: opacity 0.5s ease;
-moz-transition: opacity 0.5s ease;
-ms-transition: opacity 0.5s ease;
-o-transition: opacity 0.5s ease;
transition: opacity 0.5s ease;
}
.Enlarge span img {
margin-bottom: 5px;
}
div.Enlarge:hover {
z-index: 999;
cursor: pointer;
}
div.Enlarge:hover span {
visibility: visible;
opacity: 1;
top: 10px;
left: 0px;
z-index: 999;
width: 240px;
height: 340px;
padding: 10px;
}
<div class="Enlarge">
<img src="image1.jpg" alt="" /><span><img src="image1h.jpg" alt="" /></span>
</div>
<div class="Enlarge">
<img src="image2.jpg" alt="" /><span><img src="image2h.jpg" alt="" /></span>
</div>
Would appreciate it if somebody could advise on how I can fix this, as I need to show 6 images but at present tried with 2 images to make sure it works.
Take a look at my jsfiddle i just used style="float: left"
<div class="Enlarge" style="float: left">
<img src="image1.jpg" alt="" /><span><img src="image1h.jpg" alt="" /></span>
</div>
<div class="Enlarge" style="float: left">
<img src="image2.jpg" alt="" /><span><img src="image2h.jpg" alt="" /></span>
</div>
Edit: it was missing the link https://jsfiddle.net/m4odfrcr/
modify your class as below
.Enlarge {
position: relative;
display:inline-block;
}
I have two divs with .container class which are not stacking properly. Instead, they overlap.
This is my code:
HTML
<div id="crossfadingImages" class="container">
<img src="./media/..." class="bottom img-responsive" alt="..."/>
<img src="./media/..." class="top img-responsive" alt="..."/>
</div>
<div id="about" class="container">
<div class="row">
<div class="col-sm-6">
<p>about...</p>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
CSS
#crossfadingImages{
position: relative;
width: 100%;
padding: 0;
margin-top: 55px;
}
#crossfadingImages img{
position: absolute;
left: 0;
-webkit-transition: opacity 3s ease-in-out;
-moz-transition: opacity 3s ease-in-out;
-o-transition: opacity 3s ease-in-out;
transition: opacity 3s ease-in-out;
}
#crossfadingImages img.top:hover{
opacity: 0;
}
I think the problem is absolute position of images in crossfadingImages div.
Please provide your solution to my problem as well as an explanation.
Then you can you the hover() function
$(document).ready(function() {
$('#crossfadingImages img').hover(function() {
$('#crossfadingImages img:first-child').stop().fadeOut(500);
$('#crossfadingImages img:last-child').stop().fadeIn(500);
},
function() {
$('#crossfadingImages img:first-child').stop().fadeIn(500);
$('#crossfadingImages img:last-child').stop().fadeOut(500);
});
});
#crossfadingImages {
width: 100%;
padding: 0;
margin-top: 55px;
top: 0;
display: block;
}
#crossfadingImages img {
position: absolute;
width: 45%;
left: 0;
}
#crossfadingImages img:last-child {
display: none;
}
#about{
position:relative;
top:80px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div id="crossfadingImages" class="container">
<img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" class="bottom img-responsive" alt="..." />
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png" class="top img-responsive" alt="..." />
</div>
<div id="about" class="container">
<div class="row">
<div class="col-sm-6">
<p>about...</p>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
Ok, I managed to solve this problem by using only CSS.
NOTE: This will only work if you have two images (JPG or JPEG format) of the same size. If they aren't the same size then the bottom image will be shown all the time cause we are just changing opacity of top image.
#crossfadingImages{
position: relative;
width: 100%;
padding: 0;
margin-top: 55px;
}
#crossfadingImages img{
left: 0;
width: 45%;
-webkit-transition: opacity 3s ease-in-out;
-moz-transition: opacity 3s ease-in-out;
-o-transition: opacity 3s ease-in-out;
transition: opacity 3s ease-in-out;
}
#crossfadingImages img.top:hover{
opacity: 0;
}
.bottom-image{
position: absolute;
}
.top-image{
position: relative;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
<div id="crossfadingImages" class="container">
<img src="https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png" class="bottom img-responsive bottom-image" alt="..."/>
<img src="https://upload.wikimedia.org/wikipedia/commons/4/4a/Logo_2013_Google.png" class="top img-responsive top-image" alt="..."/>
</div>
<div id="about" class="container">
<div class="row">
<div class="col-sm-6">
<p>about...</p>
</div>
<div class="col-sm-6">
</div>
</div>
</div>
I am trying to place some text in the center-bottom of a div which contains an overlay and image.
But how can i achieve this?
HTML code:
<div class="container">
<div class="row team-images">
<div class="col-sm-3">
<div class="team-item">
<div class="team-image-overlay"></div>
<img src="http://placehold.it/400x400" class="img-responsive" alt="" />
</div>
</div>
<div class="col-sm-3">
<div class="team-item">
<div class="team-image-overlay"></div>
<img src="http://placehold.it/400x400" class="img-responsive" alt="" />
</div>
</div>
<div class="col-sm-3">
<div class="team-item">
<div class="team-image-overlay"></div>
<img src="http://placehold.it/400x400" class="img-responsive" alt="" />
</div>
</div>
</div>
</div>
CSS code:
.team-images .team-item {
position: relative;
}
.team-images img {
width: 100%;
height: 100%;
margin: 0 0 15px;
}
.team-images .team-image-overlay {
position: absolute;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(41, 128, 185, 0.4);
color: #fff;
-webkit-transition: all ease .5s;
-moz-transition: all ease .5s;
transition: all ease .5s;
}
.team-images .team-image-overlay:hover {
background: rgba(24, 188, 156, 0);
}
Here is a complete example of whats created so far: https://jsfiddle.net/prp794Lb/1/
How about this?
<div class="container">
<div class="row team-images">
<div class="col-sm-3">
<div class="team-item">
<div class="team-text">Text here</div>
<div class="team-image-overlay"></div>
<img src="http://placehold.it/400x400" class="img-responsive" alt="" />
</div>
</div>
<div class="col-sm-3">
<div class="team-item">
<div class="team-text">Lots and lots and lots and lots and lots of text here</div>
<div class="team-image-overlay"></div>
<img src="http://placehold.it/400x400" class="img-responsive" alt="" />
</div>
</div>
<div class="col-sm-3">
<div class="team-item">
<div class="team-text">Text here</div>
<div class="team-image-overlay"></div>
<img src="http://placehold.it/400x400" class="img-responsive" alt="" />
</div>
</div>
</div>
/* Latest compiled and minified CSS included as External Resource*/
/* Optional theme */
#import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');
body {
margin: 10px;
}
.team-images .team-item {
position: relative;
}
.team-images img {
width: 100%;
height: 100%;
margin: 0 0 15px;
}
.team-images .team-image-overlay {
position: absolute;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(41, 128, 185, 0.4);
color: #fff;
-webkit-transition: all ease .5s;
-moz-transition: all ease .5s;
transition: all ease .5s;
}
.team-images .team-image-overlay:hover {
background: rgba(24, 188, 156, 0);
}
.team-item .team-text {
position: absolute;
width: 100%;
bottom: 0;
text-align: center;
}
https://jsfiddle.net/wL5Lf5se/3/
Go for span inside overlay and style it like this:
Demo
.team-images .team-image-overlay span {
display:block;
text-align:center;
position:absolute;
bottom:20px;
left:20px;
right:20px;
opacity:0;
transition: all .3s ease;
color:#333;
}
.team-images .team-image-overlay:hover span {
display:block;
text-align:center;
position:absolute;
bottom:20px;
left:20px;
right:20px;
opacity:1;
}