css issue with opacity transition not functioning properly

My website is designed to display images in a table, but some of them are Stock Photos, so I need to indicate the source. I want the source to appear when the user hovers over the picture. For this purpose, I have assigned the source tag to a class (.PicSource) and given it { opacity 0; transition all .2s ease-in}. When hovering on the Picture (.ServPic), .ServPic:hover + .PicSource should make the source visible by changing the opacity to 1. However, the source text does not show up when I hover, and I cannot figure out why. Other hover-events are working perfectly fine on my website. The same method for hiding the caption while hovering works without any issues. Here's an example code snippet (the opacity of .PicSource should change to 1, but it doesn't, so you might need to adjust it manually to see the change)

.ServPic {
  width: 350px;
  height: 275px;
  opacity: 0.5;
  transition: opacity .2s ease-in;
}

.PicCapt {
  position: absolute;
  top: 0px;
  left: 0px;
  opacity: 1;
  transition: opacity .2s ease-in;
}

.PicSource {
  position: absolute;
  top: 0%;
  left: 20%;
  opacity: 0;
  color : #000000;
  transition: all .2s ease-in;
}

.ServPic:hover {
  opacity: 1;
}

.ServPic:hover+.PicCapt {
  opacity: 0;
}

.ServPic:hover+.PicSource {
  opacity: 1;
}
<table>
  <tr>
    <td>
      <div>
        <img class="ServPic" src="https://cdn.lrb.co.uk/blog/wp-content/uploads/2014/03/Lorem_Ipsum.jpg">
        <a class="PicCapt">CAPTION</a>
        <a class="PicSource">SOURCE.COM</a>
      </div>
    </td>
  </tr>
</table>

Answer №1

Use the "+" selector for immediate siblings and "~" for general siblings in CSS.

Check out this fiddle to see it in action.

Here's the code snippet:

.ServPic:hover ~ .PicSource {
  opacity: 1;
}

Let me provide some insights on sibling selectors in CSS.

To clarify, siblings are elements that exist at the same hierarchical level in your HTML structure.

For example:

<div>
   <p id="sibling1"></p>
   <p id="sibling2"></p>
   <a id="sibling3"></a>
</div>
<p>I am not a sibling of 1, 2, and 3.</p>

Siblings 1, 2, and 3 all share the same level within your markup. The paragraph tag under your div is also at that level but not considered a sibling to 1, 2, and 3.

The direct sibling of Sibling1 is Sibling2. Sibling3 is the direct sibling of Sibling2, excluding 2, which is important to note.

The general siblings of Sibling1 are both Sibling2 and Sibling3.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Display loading spinner and lock the page while a request is being processed via AJAX

Currently, I am working on a project using MVC in C#, along with Bootstrap and FontAwesome. The main objective of my project is to display a spinner and disable the page while waiting for an ajax request. Up until now, I have been able to achieve this go ...

Customizing CSS in Angular Components: Taking Control of Style Overrides

I am new to working with Angular and HTML. Currently, I have two different components named componentA and componentB, each having their own .html, .css, and .ts files. In the componentA.css file, I have defined some styles like: .compA-style { font- ...

issue with transparent html5

Here is the code snippet I am struggling with: function clear(){ context2D.clearRect(0, 0, canvas.width, canvas.height); } function drawCharacterRight(){ clear(); context2D.setTransform(1, 0.30, 1, -0.30, 10, 380);//having issues here c ...

Utilizing the data sent to a modal to enhance the functionality of the code by combining it with the src attribute of an <embed> element

I am trying to pass a dynamic string of data to a Bootstrap modal using the data-val attribute in an HTML table. <td <button type="button" class="btn btn-success btn-xs" data-val="<?php echo $row['order_id']; ?&g ...

Using jQuery to make a PNG image draggable and allow it to overlap with

Can jQuery's Draggable be used with an overlapping PNG image (not as a background image, but for printing purposes)? I attempted using the CSS style "pointer-events: none," however this solution does not function correctly in Internet Explorer. <d ...

Tips for incorporating a personalized touch to your radio buttons

Is there a way to customize the appearance of radio buttons in React or HTML elements, such as the screenshot below? I want to maintain the display of the number No.01 followed by the label next to it. If you have any suggestions or solutions, please shar ...

Incorporating and designing a side button using jQuery Mobile

I'm working on adding a button to the left side of the screen that is round (but not necessarily) and partially visible, with a visually appealing design. This button will allow users to open a side menu panel. Below is the HTML code for the button: ...

When the canvas is in full screen mode, my div elements are hidden

Currently, I am immersed in a 360-panorama project, utilizing panolens.js and three.js. While Panolens offers fullscreen mode functionality, the problem arises when entering this mode as the canvas conceals all of my div elements. One particular div elemen ...

What steps can be taken to avoid an input with a width of 100% from overflowing?

I'm facing an issue that needs to be resolved. Currently, I have a group of div elements arranged in a table-like structure with a row and three columns. When these divs reach a maximum width of 768px, the first column is hidden, leaving only the se ...

Guidelines for choosing input using jQuery

I am looking to retrieve the value of an input using jQuery. Specifically, I need to extract the value of a hidden input with name="picture" when the user clicks on the حذف این بخش button by triggering the function deleteDefaultSection( ...

Button to scroll down

I have successfully implemented a #scrolldownbutton that scrolls to the first component. However, I am now attempting to modify it so that when the button is clicked, the page smoothly scrolls within the viewport and stops at the partially visible componen ...

Error in JQuery Document Ready AJAX function causing image to not load initially without a page refresh

Currently, I have a piece of customized code that extracts data from an XML file and uses this data to populate specific content on my webpage: $(document).ready(function() { $.ajax({ type: 'GET', url: 'config.xml?date=& ...

The CSS transition will only activate when coupled with a `setTimeout` function

After using JavaScript to adjust the opacity of an element from 0 to 1, I expected it to instantly disappear and then slowly fade back in. However, nothing seems to happen as anticipated. Interestingly, if I insert a setTimeout function before applying th ...

Transform Unicode characters into HTML using Qt

I am facing an issue with a particular string that includes Unicode characters and special symbols. The string, for instance, looks like this: SYMBOLSU+2510\n The string contains the Unicode character U+2510 and a new line symbol \n. I have a lo ...

Achieve full height of parent with rotated text

Hey, I've been experimenting with some CSS transforms and I'm having trouble getting this to work properly. Check it out here I'm using Bootstrap 4 to create two columns; an image on the left and text on the right. I want the text to be ce ...

How can I create a tickable image grid in Nativescript Angular?

My goal is to create an image grid in a Nativescript Angular App where users can select images and then move to the next page with the selected image IDs or names. I have successfully created the image grid, but I am struggling to make the images tickable ...

"A lengthy contenteditable div designed to mimic an input field, with the ability to horizontally scroll

When the input is too short for the entire text to be displayed, users have the option to horizontally scroll the text inside the input by dragging with the mouse. Is there a way to implement this functionality in a contenteditable field that appears as ...

Slide the first div upward to display the text in the second div when hovering over the first div

I'm facing an issue where a div needs to move upwards when hovering over it. To see an example, take a look at this JSfiddle. The desired outcome is as follows: As illustrated, the right div is the one being hovered. I have attempted to create this ...

Navigation isn't aligning correctly on the right side

I am having trouble with my navigation menu not aligning properly to the right. <div class="logo"> <h2><i class="icon-reorder"></i> Frosty</h2> </div> <div class="nav"> <a href="#">Home</a> </div& ...

Show a pop-up window when a button is clicked, just before the page redirects

Is there a way to display a popup message before redirecting to another page when clicking on a button? Here's the scenario: <a href="addorder.php?id=<? echo $row01['id']; ?>" ><button id="myButton" class="btn btn-primary btn ...