Aligning content vertically when it is hovered over

After searching through numerous articles on Stack, I have yet to find a solution to my issue.

I have successfully implemented content display upon hovering over an image. However, I am encountering difficulties in vertically centering the displayed content, especially when dealing with multiple lines within the blocks. You can view my implementation on JSFiddle here: https://jsfiddle.net/wkmepjnk/

Any assistance with this matter would be greatly appreciated! Thank you

This is my HTML code:

<div class="grid-wrapper">

                <div class="grid-single">
                    <img src="images/qdeck-logo.png">
                        <div class="grid-content">
                        <span>
                            <div class="hover-content">
                            <h1>QDeck</h1>
                            <p>Q-Deck® decking or deckboards are available in four standard timber profiles, two anti-slip resistant timber profiles known as Q-Grip® and a composite profile in a range of colours known as Q-Deck Twinson.</p>
                            <div class="hover-button">More Info</div>
                            </div>
                        </span>
                    </div>
                </div>
                <!--Close Grid Single-->


            </div>
            <!--Close Grid Wrapper-->

And this is my CSS code:

.grid-wrapper {
    width:100%;
    height:auto;
    background-color:#ffffff;
    -webkit-box-shadow: 0px -11px 10px -11px rgba(0,0,0,0.75);
    -moz-box-shadow: 0px -11px 10px -11px rgba(0,0,0,0.75);
    box-shadow: 0px -11px 10px -11px rgba(0,0,0,0.75);
}

.grid-single {
    width:25%;
    height:auto;
    margin-right:-5px;
    display:inline-block;
    border:1px solid #e0e3e6;
    position: relative;
}

.grid-single h1 {
    font-family: 'montserratlight';
    font-size:25px;
    line-height:30px;
    color:#ffffff;
    text-transform:uppercase;
    letter-spacing:2px;
    margin-bottom:10px;
}

.grid-single img {
    width:100%;
    height:auto;
    margin-bottom:-5px;
}

.grid-single .grid-content {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: #1ABC9C;
    opacity: 0;
    transition: opacity 0.1s;
    text-align:left;
}

.grid-single .grid-content span {
    display: block;
    position:absolute;
    top:10%;
    left:10%;
    width:80%;
    height:auto;
    text-align:center;
}

.grid-single .grid-content span p { 
    font-family: 'montserratlight';
    font-size:14px;
    line-height:21px;
    color:#ffffff;
    margin-bottom:15px;
    letter-spacing:0px;
}

.grid-single .grid-content span p strong {  
    color:#a1cc3a;
    margin-bottom:4px;
}

.grid-single:hover .grid-content {
    opacity: 1;
}

.hover-button {
    display:inline-block;
    width:auto;
    height:40px;
    background-color:#2C3E50;
    padding:0px 20px;

    font-family: 'montserratlight';
    font-size:15px;
    line-height:40px;
    color:#ffffff;

}

Answer №1

To ensure that the span with the class of .grid-single .grid-content span is displayed vertically centered, you should apply top:50%; and margin-top:-x;, where x represents half of the height of the span.
In case you need it, include the following:

.grid-single h1 {
    margin-top: 0;
}

Check out this jsfiddle for reference

Answer №2

I'll attempt to implement the flex property along with justify-content:center; and align-items:center;:

.grid-wrapper {
  width: 100%;
  height: auto;
  background-color: #ffffff;
  -webkit-box-shadow: 0px -11px 10px -11px rgba(0, 0, 0, 0.75);
  -moz-box-shadow: 0px -11px 10px -11px rgba(0, 0, 0, 0.75);
  box-shadow: 0px -11px 10px -11px rgba(0, 0, 0, 0.75);
}
.grid-single {
  width: 25%;
  height: auto;
  margin-right: -5px;
  display: inline-block;
  border: 1px solid #e0e3e6;
  position: relative;
}
.grid-single h1 {
  font-family: 'montserratlight';
  font-size: 25px;
  line-height: 30px;
  color: #ffffff;
  text-transform: uppercase;
  letter-spacing: 2px;
  margin-bottom: 10px;
}
.grid-single img {
  width: 100%;
  height: auto;
  margin-bottom: -5px;
}
.grid-single .grid-content {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  background: #1ABC9C;
  opacity: 0;
  transition: opacity 0.1s;
  text-align: left;
}
.grid-single .grid-content span p {
  font-family: 'montserratlight';
  font-size: 14px;
  line-height: 21px;
  color: #ffffff;
  margin-bottom: 15px;
  letter-spacing: 0px;
}
.grid-single .grid-content span p strong {
  color: #a1cc3a;
  margin-bottom: 4px;
}
.grid-single:hover .grid-content {
  opacity: 1;
  display: flex;/*add flex*/
  justify-content: center;/*add this*/
  align-items: center;/*add this*/
}
.hover-button {
  display: inline-block;
  width: auto;
  height: 40px;
  background-color: #2C3E50;
  padding: 0px 20px;
  font-family: 'montserratlight';
  font-size: 15px;
  line-height: 40px;
  color: #ffffff;
}
.grid-content span {
  border: solid 1px;
}
<div class="grid-wrapper">
  <div class="grid-single">
    <img src="http://placehold.it/400x400">
    <div class="grid-content"> <span>
            <div class="hover-content">
            <h1>QDeck</h1>
            <div class="hover-button">More Info</div>
            </div>
            </span>

    </div>
  </div>
  <!--Close Grid Single-->
</div>
<!--Close Grid Wrapper-->

Answer №3

If you're looking to adjust for expanded text, there's a method that can help with that. I experimented with your demo, but found the setup a bit complicated. So, I created a quick version from scratch to illustrate the concept. The key components include setting display: table; on the parent element and

display: table-cell; vertical-align: middle;
on the child element.

Check out this link for a demo

<div class="wrapper">
  <img src="http://placehold.it/300x200" alt="" />
  <div class="overlay">
    <h3>Amazing Heading</h3>
    <span>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Corporis totam deleniti.</span>
    <button class="btn btn-primary">View</button>
  </div>
</div>

.wrapper{
  display: table;
  width: 300px;
  height: 200px;
}

.overlay{
  display: none;
  vertical-align: middle;
  width: 300px;
  height: 200px;  
  background: #EEE;
  text-align: center;
}

.overlay .btn{
  margin-top: 5px;
}

.overlay h3{
  margin-top: 0;
}

.wrapper:hover img{
  display: none;
}

.wrapper:hover .overlay{
  display: table-cell;
}

Answer №4

Consider implementing these alternative styles instead

.single-grid .content {
    background-color: #3498db;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    transition: opacity 0.2s ease-in-out;
}

.single-grid .content span {
    width: 80%;
    text-align: center;
}

Answer №5

After spending some time working on my code, I finally cracked it.

You can check out the result in action right here: https://jsfiddle.net/x7s6f9jm/

Now, I am able to insert any amount of text into the div and it looks great.

I made some adjustments to my CSS:

.grid-single .grid-content {
    position: absolute;
    width: 100%;
    height: 100%;
    top: 0;
    left: 0;
    background: #1ABC9C;
    opacity: 0;
    transition: opacity 0.1s;
    text-align:left;
}

.grid-single .grid-content span {
    display: table;
    position:absolute;
    left:10%;
    width:80%;
    height:100%;
    text-align:center;
}

.hover-content {
    display:table-cell;
    background-color:#ff6600;
    vertical-align:middle;
}

A big thank you to everyone who contributed!

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

Convert JSON data into a nested unordered list

I need help converting a JSON object into an unordered list using jQuery. Here's the JSON data I have: var myJSON = "{name:\"Director\",children:[name:\"Exe Director1\",name:\"Exe Director2\",name:\"Exe Director3&bs ...

The lifecycle of XMLHTTPRequest objects in JavaScript - from creation to destruction

After years of working with traditional compiled object-oriented languages like C++ and .NET programming, I decided to dip my toes into JavaScript for a new project. As I was experimenting with AJAX, I stumbled upon a perplexing aspect related to how objec ...

PHP - session expires upon page refresh

I'm in the process of creating a login system for my website and I've run into an issue with updating the navigation bar once a user has logged in. Every time I refresh the page, it seems like the session gets lost and the navigation bar doesn&ap ...

I'm curious about how I can determine the reason why my badge is not accepting HTML tags in its tooltip

My goal is to integrate a bootstrap badge-alert with an HTML tooltip. I am following the example provided at https://getbootstrap.com/docs/4.3/components/tooltips/ Despite my efforts, when using the code snippet below, the tooltip appears but the HTML tag ...

Change the display of the switch button to reveal a different form

I'm looking to implement a switch on/off button that displays a different form when clicked on and reverts back to the original form when clicked off. Check out my code snippet below: <h3>transfer</h3> <input type="checkbox" id="id-na ...

Angular Digest Loop for Dynamic Photo Grid Styling

I have a special filter that alters the objects being filtered. However, when I apply ng-style="item.gridSize", it triggers my custom grid algorithm. This algorithm was adapted for my requirements from a source found at this link. angular.module("custom.m ...

What is the best way to alter the background-color of an element that is contained within a GatsbyJS component, depending on the specific page where the component is being utilized?

As a first-time GatsbyJS website builder, I am working on creating reusable components for my site. One of these components is the footer, and I have already structured it. Now, I have a dilemma - I want to change the color of a specific <div> within ...

What is the best way to update the text on buttons once they've been clicked for a variety of buttons

I'm encountering an issue with multiple buttons where clicking on any button causes the text to change on the first button, rather than the one I actually clicked on. All buttons have the same id, and using different ids for each button seems impracti ...

Extract all URLs and images in a PHP script and transform them for rendering in HTML

I have come across many discussions on this topic, but I am struggling to find a solution that works effectively for both links and images. When I echo $content on my PHP page, it displays a record from my database. This string may contain URLs and image ...

Tips for transforming the appearance of an asp.net page with asp:Content into a stylish css-page

Currently facing an issue where I am unable to change the background-color in my CSS file. As a workaround, I have placed the style directly within an ASP page. <asp:Content Id="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> ...

Tips for positioning a navigation bar in the center for various screen resolutions

After hours of searching, I still can't find a solution to my problem. I'm new to this, and on my main monitor, everything looks centered just fine. 1920x1080: View larger image But on my 1280x1024 monitor: View smaller image As you can see, i ...

Ensure that the child div with a background color extends all the way to the bottom of its parent div

Having trouble getting two divs inside another one to stretch to the bottom without cutting off content? The goal is for both divs to adjust in height based on whichever one is longer, filling the outer div completely. When the left div (menu) is longer t ...

unable to distinguish authenticity from deception through filtration methods

I am looking to differentiate between true and false using filter() but it keeps initializing the value to true and false. I want all true values to appear on one side and false values on another side, with a line separating them. ...

Tips for stopping the textarea from moving around as you type and avoid it hitting the bottom of the page: Utilize JQuery and

As I type into the auto-sizing textarea, the text runs to the bottom of the page and causes it to dance uncontrollably with each key press. This forces me to constantly scroll down to see what's happening. How can I prevent this page dancing and keep ...

What is the process of implementing a particular FormControl from a FormArray in my HTML file?

My FormArray initialization code is as follows: this.contents.forEach(content=> { this.formArray.push( new FormControl(content.text, Validators.required)); }); Now, I am trying to associate a specific FormControl with my textarea by using i ...

Tips for zooming to the middle of a div element

I'm trying to figure out how to create a zoom in effect on my large div, but I haven't been successful despite searching through many resources. My goal is to zoom into the center of the user's screen rather than just at a set position. ht ...

Aligning the email form to the center with the text-align center property:

I'm in the process of creating an app landing page using the Bootstrap framework (Bootstrap 4 alpha). I have used text-align:center in the jumbotron id to center everything, but the email form is not aligning properly. Is there a way to center align ...

How can I implement horizontal scrolling on a material-ui table containing numerous columns?

As I work with the React Material-UI framework, I am using this table example as a guide. However, I am facing an issue where my table becomes overcrowded when I have numerous columns, causing the content to be cut off. I recently came across the material ...

What causes an element with position:absolute to be placed behind the ::after pseudo-element?

Being a beginner in HTML and CSS, I recently encountered a challenge while working on some exercises. My goal was to create a website with a blur effect, so I decided to design a header with the class "showcase" and nested inside it, a div with the class " ...

What is the correct method for caching fonts within an Angular application?

In the process of developing a web application similar to photoshop-minis using Angular, one key aspect I am focusing on is reducing load times. Particularly when it comes to fonts, as not all necessary fonts are available through Google Fonts. Instead of ...