I have an html page containing elements as indicated in the code below: The goal is to have a larger image and a caption visible when the thumb is hover.
The problem is; if I wrap the anchor in a parent element then the code does not work: How do I get this to work even if the anchor is not the parent element?
For example: Doing this will not work, it will only work if the anchor is a parent to the image element. I need to place the images in a unordered list.
It would be handy if you could post a Pen (codepen.io) with the code as close as you can get it—images in place etc. Here’s a guide to doing that: Forum Posting Basics
Initially, the reason I did not go your suggested route is because I was going to use one element for multiple place holders. Meaning if I had 100 thumb images, then I will rotate the main image in a single placeholder but with individual elements for the caption:
However, this will no longer work because I will need to create additional elements for each placeholder so that they are in the same parent element.
My understanding regarding the sibling selectors is they must be either adjacent or share the same parent, correct?
This is what I have come up with:
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Image Gallary Example</title>
<style type="text/css">
ul {
margin: 0;
padding: 0;
list-style: none;
width: 210px;
height: 160px;
background-color: #090;
}
ul li{
margin: 0;
padding: 0;
display: block;
float: left;
background-color: #F00;
width: 100px;
height: 75px;
}
ul li figure,
ul li figure img,
ul li figure a {
border: none;
outline: none;
text-decoration: none;
display: block;
width: 100px;
height: 75px;
margin: 0;
padding: 0;
}
ul li figure img{
vertical-align: bottom;
}
ul li:nth-child(1){
margin: 0px 10px 0px 0px;
}
ul li:nth-child(3){
margin: 10px 10px 0px 0px;
}
ul li:nth-child(4){
margin: 10px 0px 0px 0px;
}
figcaption{
display: none;
margin: 0;
padding: 0;
width: 400px;
height: 300px;
background-repeat: no-repeat;
background-position: top left;
position: absolute;
left: 500px;
}
figure figcaption{
display: none;
}
.thumb2:hover + figcaption {
background-image: url(images/photo2.jpg);
display: block;
}
.thumb3:hover + figcaption {
background-image: url(images/photo3.jpg);
display: block;
}
.thumb4:hover + figcaption {
background-image: url(images/photo4.jpg);
display: block;
}
.thumb6:hover + figcaption {
background-image: url(images/photo6.jpg);
display: block;
}
</style>
</head>
<body>
<ul>
<li>
<figure>
<a class="thumb2" href="#">
<img src="images/photo2thumb.jpg" width="100" height="75">
</a>
<figcaption>
This is Caption for Image 1:
</figcaption>
</figure>
</li>
<li>
<figure>
<a class="thumb3" href="#">
<img src="images/photo3thumb.jpg" width="100" height="75">
</a>
<figcaption>
This is Caption for Image 2:
</figcaption>
</figure>
</li>
<li>
<figure>
<a class="thumb4" href="#">
<img src="images/photo4thumb.jpg" width="100" height="75">
</a>
<figcaption>
This is Caption for Image 3:
</figcaption>
</figure>
</li>
<li>
<figure>
<a class="thumb6" href="#">
<img src="images/photo6thumb.jpg" width="100" height="75">
</a>
<figcaption>
This is Caption for Image 4:
</figcaption>
</figure>
</li>
</ul>
</body>
</html>