What is the best way to vertically center a caption when an image is being hovered over

I'm facing an issue with aligning my caption vertically inside a div when hovering. The image within the div is larger than the div, and I want the caption to be at the center of the div.

HTML

<div class="container-fluid">

    <div id="page">

<!-- GRID ITEM -->
        <div class="item s1">
            <a href="#">
                <div class="grid-image">
                    <img         src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
                </div>
                <div class="item-caption">
                    <h5>I would like this to be centered</h5>
                </div>
            </a>
        </div>
<!-- /GRID ITEM -->

    </div>
</div>

CSS

#page .item {
    width: calc(16.66% - 10px);
    display: inline-block;
    height: 0;
    float: left;
    padding-bottom: 16.66%;
    overflow: hidden;
    background-color: salmon;
    margin: 5px;
    position: relative;
}

#page .item.s1 {
    width: calc(50% - 10px);
    padding-bottom: 50%;
    overflow: hidden;
    background-color: navy;
}

.item > a {
    position: relative;
    display: block;
    overflow: hidden;
    color: white;
}
.item:hover .grid-image:after {
    background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
    -webkit-transform: scale(1.1);
       -moz-transform: scale(1.1);
        -ms-transform: scale(1.1);
         -o-transform: scale(1.1);
            transform: scale(1.1);

}
.item:hover .item-caption {
    opacity: 1;
    z-index: 3;
  visibility: visible;

}
.item-caption,
.grid-image > img,
.grid-image:after {
    -webkit-transition: all 0.3s ease-in-out 0s;
       -moz-transition: all 0.3s ease-in-out 0s;
        -ms-transition: all 0.3s ease-in-out 0s;
         -o-transition: all 0.3s ease-in-out 0s;
            transition: all 0.3s ease-in-out 0s;
}
.item-caption {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(29, 106, 154, 0.72);
  color: #fff;
  visibility: hidden;
  text-align: center;
  opacity: 0;
}



.grid-image {
    position: relative;
    overflow: hidden;
}

.grid-image img {
    display: block;
    overflow: hidden;
}

.grid-image:after {
    position: absolute;
    display: block;
    content: "";
  height: 100%;
  width: 100%;
    top: 0;
    left: 0;
}

JavaScript

var $container = $('#page');
$container.masonry({
columnWidth: '.grid-sizer',
itemSelector: '.item',
percentPosition: true,
gutter: 10
});

Check out this fiddle

Thank you!

Answer №1

Check out this solution: http://jsfiddle.net/2stywe2t/1/

Here's what I did to address the issue:

  • I added a new div element to your HTML code, positioned right below .item-caption.
  • I applied the display: table property and some other styles to .item-caption.
  • The new div was given properties like display: table-cell and vertical-align: middle.
  • I removed the position:relative from the ancestor a element.

Do you follow these steps?

Answer №2

Great job on finding the solution! Now let's clean up your code a little bit.

var $container = $('#page');
$container.masonry({
    columnWidth: '.grid-sizer',
    itemSelector: '.item',
    percentPosition: true,
    gutter: 10
});
#page .item {
    width: calc(16.66% - 10px);
    display: inline-block;
    height: 0;
    float: left;
    padding-bottom: 16.66%;
    overflow: hidden;
    background-color: salmon;
    margin: 5px;
    position: relative;
}
#page .item.s1 {
    width: calc(50% - 10px);
    padding-bottom: 50%;
    overflow: hidden;
    background-color: navy;
    position: relative!important;
}
.item > a {
    position: absolute;
    display: block;
    width: 100%;
    height: 100%;
    overflow: hidden;
    color: white;
}
.item:hover .grid-image:after {
    background: rgba(0, 0, 0, .7);
}
.item:hover .grid-image > img {
    -webkit-transform: scale(1.1);
    -moz-transform: scale(1.1);
    -ms-transform: scale(1.1);
    -o-transform: scale(1.1);
    transform: scale(1.1);
}
.item:hover .item-caption {
    opacity: 1;
    z-index: 3;
    visibility: visible;
}
.item-caption, .grid-image > img, .grid-image:after {
    -webkit-transition: all 0.3s ease-in-out 0s;
    -moz-transition: all 0.3s ease-in-out 0s;
    -ms-transition: all 0.3s ease-in-out 0s;
    -o-transition: all 0.3s ease-in-out 0s;
    transition: all 0.3s ease-in-out 0s;
}
.item-caption {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(29, 106, 154, 0.72);
    color: #fff;
    visibility: hidden;
    text-align: center;
    opacity: 0;
    display: table;
    height: 100%;
    width: 100%;
}
.grid-image {
    position: relative;
    overflow: hidden;
}
.grid-image img {
    display: block;
    overflow: hidden;
}
.grid-image:after {
    position: absolute;
    display: block;
    content:"";
    height: 100%;
    width: 100%;
    top: 0;
    left: 0;
}
h5 {
    display: table-cell;
    vertical-align: middle;
}
<div class="container-fluid">
    <div id="page">
        <!-- GRID ITEM -->
        <div class="item s1"> <a href="#">
                <div class="grid-image">
    <img         src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/Pleiades_large.jpg/1024px-Pleiades_large.jpg">
                </div>
                <div class="item-caption">
                    <h5>I want this to be center</h5>
                </div>
            </a>

        </div>
        <!-- /GRID ITEM -->
    </div>
</div>
</body>

</html>

Answer №3

To achieve the desired result, just a simple addition to your CSS will do the trick:

h5 { 
    margin-top: 50%;
    transform: translateY(-50%); /* for precise centering */
}

Check out the demonstration here: http://jsfiddle.net/2stywe2t/4/

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 importing font files from the node_module directory (specifically otf files)

We cannot seem to retrieve the fonts file from the node module and are encountering this error message. Can't find 'assets/fonts/roman.woff2' in 'C:\Projects\GIT2\newbusinessapp\projects\newbusinessapp\src ...

Extracting content from HTML-formatted email

When I receive inbound emails with HTML formatting that has been copied and pasted from office applications like Outlook, it often causes formatting issues when displayed on my HTML enabled UI. To address this problem, I usually copy the HTML content to an ...

Expanding the content of a single page by clicking on a separate tab

I have a link on Page A. Page B has two tabs, with the content of tabs 1 and tab 2 currently set to "display:none". I want clicking the hyperlink on page A to automatically open or activate the second tab on page B. I am seeking a solution using JavaScri ...

Issue with Jenkins displaying parameter values

Objective: Establish a conditional string parameter that spans multiple lines Method: I have set up two different jenkins parameters. Active choices parameter Active choices reactive reference parameter(Choice type: For ...

Can the value in a JavaScript object be updated dynamically when a button is clicked?

In my JavaScript code, there is an object named annualPlan. Whenever a user submits the HTML form for a specific month, I aim to update the value in the object for that particular month accordingly. For instance, if someone submits August 21 and 200, I w ...

The mobile navigation in HTML has a slight issue with the ::after pseudo-element, specifically within the

As I prepare to launch my website, I have made adjustments to the mobile layout by moving the navigation links to a navigation drawer. The template I am using included JavaScript scripts to handle this navigation change. However, I encountered an issue whe ...

"PHP Dilemma: Navigating the Ajax Button Press Situation

I've been attempting to create a button that triggers a PHP script on a webpage and updates only a specific div tag, but so far I haven't had any success. Removing the $ajax section of the script allows my buttons to change states, but as soon as ...

Vertical tab design in Bootstrap does not automatically switch tabs

I'm managing two separate tab boxes that switch content when clicked or over a 5-second period. The left box is functioning correctly, but the right box is changing the active state without updating the content. Here's the HTML for the left box ...

Sliced Text Transformation

Recently, I inquired about a pesky text effect that was bothering me. Thanks to the assistance of fellow users, I discovered that the culprit behind the effect was the background color's edge. Eliminating this problem should be simple, but even with ...

The printed PDF of a webpage does not display HTML table background colors in Chrome or Firefox

Is there a way to change the colors of table cells in an HTML table when exporting to PDF? I'm creating content dynamically that includes background-color: #ffffff (or red) and showing it on a webpage, but the cell backgrounds appear as white in the P ...

How can you use JavaScript to create a JSON object using values from an HTML form textarea and then send

I need to create an HTML form that will send specific information in the Http-request body. { “info” : { “id” : “123” “text1” : <data from html text-box> } Therefore, my goal is to convert the provided data into a JavaScri ...

Transferring Chart Div Titles Above Bars

Is there a way to adjust the layout of an HTML bar chart so that the names or labels appear outside of the bars, tagged to the left end? <!DOCTYPE html> <style> .chart div { font: 10px Ubuntu; background-color: white; text-align: right; ...

Running a method at any given time within an ngFor loop in Angular 5

On my angular page, I am facing a challenge with updating a variable and displaying it in the HTML within an *ngFor loop. Here is an example of what I need: HTML: <table *ngFor="let data of Dataset"> somehowRunThis(data) <div>{{meth ...

Responsive HTML5 audio player adjusts size when viewed on mobile devices

I am facing a challenge with an HTML5 Audio player. It looks fine on desktop but behaves strangely on mobile devices. While the width remains intact, it repositions itself and floats over the element it is contained within. How can I prevent this repositio ...

Is it advantageous to employ several wrapper-divs inside section elements when working with HTML5 and CSS?

Back in the day, I learned how to create a website by enclosing all content below the body tag within a div with the class "wrapper." This approach made sense as it allowed for easy vertical and horizontal alignment of the entire content. Just yesterday, ...

Populate a table dynamically in JavaScript using a variable

I have a table with IDs such as r0c1 = row 0 column 1. I attempted to populate it using the following script, but it doesn't seem to be functioning correctly: var data = new Array(); data[0] = new Array("999", "220", "440", "840", "1 300", "1 580", " ...

What is the best way to create a sticky/fixed navbar that conceals the div above it while scrolling on the page?

When I am at the top of the page, I want to display the phone number and email above the navigation bar. However, as soon as I start scrolling down, I want the phone number and email to disappear and only show the navigation bar. I have attempted using fix ...

What are the disadvantages of nesting CSS Grids within each other?

I have been exploring component-driven front-end frameworks like Angular and honing my skills in CSS Grid. My query is: Is it considered a bad practice to nest CSS Grids? In my main/root component, I have utilized CSS grid to create two elements: the nav ...

Make the checkbox font-weight bold when it is checked

Attempting to apply custom CSS to enhance the appearance of checkboxes using input and label elements. The goal is to make the font-weight bold when a user clicks on the checkbox, and then revert it back to regular if clicked again. Currently stuck on this ...

Retrieve the user's information using their email address

I am attempting to retrieve "Registered user information" on my email address using the "mail()" function. The mail() function is functioning correctly and successfully sending the email to the specified email address, however, it is unable to retrieve the ...