Increasing the height of a jQuery div element and displaying image captions when hovering over them

There is a mysterious 3px gap between the image and the overlay, causing the overlay to extend 3px beyond the height of the image. Switching the CSS to use bottom:3px fixes the issue, but it feels like a hacky solution. The source of this spacing conundrum remains elusive.

Here's the code snippet in question:

HTML

<div class="item col-50">
    <div class="image-hover">
      <div class="image-hover-inner">
      <a href="<?php the_sub_field('project_index_link');?>">
      <img src="<?php the_sub_field('project_index_image'); ?>">

        <div class="image-caption">                    
          <div class="image-caption-inner">
          <h3 class="heading"><?php the_sub_field('project_index_title');?></h3>
          <p><?php the_sub_field('project_index_description');?></p>
          </div>
        </div>
        </a>
      </div>
    </div>
  </div>

SASS

.image-hover {
    position: relative;

        a,
        a:hover {
            color:white;
        }
}

.image-caption {
    position: absolute;
    top:0;
    right:0;
    bottom:3px;
    left:0;
}

.image-caption-inner {
    position: absolute;
    top:50%;
    width:100%;
    display:block;
    -webkit-transform: translatey(-50%);
    -moz-transform: translatey(-50%);
    -o-transform: translatey(-50%);
    transform: translatey(-50%);
}

JQUERY

jQuery(document).ready(function($) {
    $('.image-caption').hide();
        $('.image-hover').hover( function() {
        $(this).find('.image-caption').fadeIn(300);
        }, function() {
        $(this).find('.image-caption').fadeOut(300);
    });
});

Answer №1

The reason behind this issue is the presence of extra spaces after the image such as new lines and tabs. These whitespaces are acknowledged by browsers, causing an unintended gap to appear. To solve this problem temporarily, you can set the font-size to 0 for the a element and then revert it back to its original value for the .image-caption class:

.image-hover {
    position: relative;
        a,
        a:hover {
            color: white;
        }
}

.image-caption {
    ...
    bottom: 0;
    font-size: 16px;
}

By setting bottom: 0, you can eliminate any strange spacing issues.

Check out the demo here: http://jsfiddle.net/abcdefg123/

Answer №2

After implementing your code, I decided to enhance it by adding a border to the .image-caption class in order to clearly define the overlay boundary.

If you'd like to see the updated version, feel free to visit this fiddle. I have ensured that there are no gaps between the title and description, both perfectly centered on the image.

.image-caption {
    position: absolute;
    top:0;
    right:0;
    bottom:3px;
    left:0;
    border:1px solid red;  /* newly added line */
}

If you could share your issue using the fiddle tool, it would greatly help others better understand the problem at hand.

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

Tips for updating input values in real-time?

I am working with dynamic inputs that allow me to add and delete rows with inputs. These inputs include Material-UI timepickers, which have an input field with a clock icon. When I click on the icon, a clock appears. However, the values in this input field ...

Selenium Tips: Ensuring RemoteDriver Remains Connected to the Active Browser Tab

Currently working on a unique Windows application that utilizes voice commands to control web browsers. I am trying to figure out the best approach when users add tabs and modify the selected tab as needed. Unfortunately, RemoteDriver only supports swi ...

Centering the date vertically in JQuery Datepicker

I'm currently customizing the appearance of the jQuery datepicker, but I'm facing difficulty in aligning the dates vertically at the center. Here's the progress I've made so far: https://jsfiddle.net/L4vrkpmc/1/ Since the datepicker i ...

Pop-Up Window Shifts Down after Initial Appearance to the Right

This code snippet demonstrates a popup that is intended to appear to the right of the question mark link once it is clicked. Check out the example here: https://jsfiddle.net/eur6ao3z/13/ Initially, the popup displays correctly on the first click. However ...

The input type file is not correctly inserting an image into the image tag

While I was working on a project, I had a question that got answered but now I need to find a different way to use the solution. I have created a jsFiddle to demonstrate how it currently works. You can view it here: http://jsfiddle.net/TbZzH/4/ However, w ...

Having trouble launching Cypress on my Mac - stating that it cannot find Cypress

Despite searching through multiple answers on S.O, none of them have solved my issue. To better explain my question, I've created a video. You can view it here Everything was working perfectly just yesterday, so what could have possibly gone wrong? ...

Selenium Python option other than using send_keys()

I was looking to enhance the efficiency of this Python code and found a faster alternative: driver.get(myUrl) message = driver.find_element_by_id('message') send = driver.find_element_by_id('submit') for _ in range(myRange): messa ...

Inject the HTML code into a bash script and perform operations on it

Could someone help me with a bash scripting question? I'm new to this, so I may have missed it if it's already been answered. I want to pass the html source code of a web page to my script, which will then modify or scrape the web page of its HT ...

Tips for utilizing the fixed position within a flexbox design

I am working on a flexbox layout with two columns. The left column needs to be fixed, while the right column should be scrollable. Can you provide some guidance on how to achieve this? Here is the code snippet that I have been using: #parent { d ...

Conceal one element and reveal a different one upon submitting a form

On a web page, I want to hide the form upon submission (either by clicking or pressing enter) and display the results. However, this functionality does not seem to work when the Go web server is running. When I test the HTML file (without executing the Go ...

Transmitting a multidimensional array in JavaScript to an AjaxPro method on a front-end webpage: A step-by-step guide

Imagine a scenario where I have a C# method that requires an array parameter: [AjaxPro.AjaxMethod] public int Test3(string[,] array) { return array.Length; } Next, I define a multidimensional array on the front page using JavaScript: var array = ...

How can I determine the size of the custom options dropdown in Magento?

I'm facing a challenging issue that I can't seem to crack. It might be because I'm still learning the ropes and struggling with technical jargon. Please bear with me as I try to explain my problem. Here is a summary of what I'm trying ...

What strategies are available for managing intricate nested data conversions in TypeScript with lodash?

I am currently developing a TypeScript project that involves performing intricate transformations on deeply nested JSON data. While I am utilizing lodash for utility functions, I have encountered a challenge in the following scenario: { "users" ...

Tips on utilizing the useState hook for storing numerous key objects?

Currently, I am working with a candlestick chart for a cryptocurrency that displays data over different timeframes such as 1 minute and 30 minutes. Below is the code snippet that sets the initial state to show a 1-hour chart when a user first visits: const ...

Exploring JSON nested objects with jQuery UI Autocomplete

I'm attempting to implement the jQuery UI Autocomplete widget with a customized JSON feed received from an API. The format of the JSON data is as follows: { "SearchTerm": "ches", "HasDirectCountyHit": false, "DirectCountyHitId": null ...

Performing a deep insert in SAPUI5 with the Kapsel Offline App on an OData V2 Model

Query: What is the process for performing a "Deep Insert" from a SAPUI5 Client application on an OData V2 Model? Situation: In my SAPUI5 Client application, I need to Deep Insert an "Operation" along with some "Components" into my OData V2 Model. // h ...

Is it possible to redirect the client to a different HTML file after they input a number?

Following a submission of numerical input, I am looking to navigate from the index.html file to another HTML file or similar destination. Could someone provide assistance? Below is the code I have written: <!DOCTYPE html> <html lang="en&quo ...

Removing elements from an array with reduced speed

Within my array, I am looking to remove N elements from the beginning. For instance, if my array contains 1 million floating point elements and I need to remove the first 500,000, I have two options. The first is to iterate and call the shift method 500,0 ...

Clouds marred by a unpleasant visual presentation

I've been following the instructions on this specific tutorial, but I'm attempting to scale it up significantly (increasing the radius to 100000 units). It seems like the size may be affecting the outcome, as the clouds in my earth render are ap ...

Navigating the issue of "Experiencing additional hooks rendered compared to the previous render"

I'm currently in the process of developing a small application where certain elements will be nested within one another. My approach involves segmenting each component into its own file (children), returning a function with two components in each Rend ...