Looking to showcase image captions at the bottom of the viewport instead of the bottom of the page

I am looking to center the captions of each image at the bottom of the viewport, so that they stay in the same place when a user scrolls (on hover).

Sorry if this layout is incorrect, I'm still new to coding. I wasn't able to link the actual images due to them being flagged as spam here... which is affecting the desired layout.

.gallery {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-gap: 20px;
  padding: 20px;
  position: relative;
  /* Ensure .gallery is positioned */
}

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

.image-container img {
  width: 100%;
  height: auto;
}

.caption {
  position: fixed;
  bottom: 20px;
  /* Adjust as needed */
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 10px;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease-in-out;
  z-index: 2;
  /* Ensure captions appear in front of images */
}

.image-container:hover .caption {
  opacity: 1;
}
<br>
<br>
<br>
<div class="gallery">
  <div class="image-container">
    <img alt="Placeholder Image 1" src="1.jpg">
    <div class="caption">Caption for Image 1</div>
  </div>
  <div class="image-container">
    <img alt="2.jpg" src="2.jpg">
    <div class="caption">Caption for Image 2</div>
  </div>
  <div class="image-container">
    <img alt="Placeholder Image 3" src="3.jpg">
    <div class="caption">Caption for Image 3</div>
  </div>
  <div class="image-container">
    <img alt="Placeholder Image 4" src="4.jpg">
    <div class="caption">Caption for Image 4</div>
  </div>
  ...
</div>

I've tried various options, some resulting in the captions being centered at the bottom of each image, but never at the bottom of the viewport as desired.

Answer №1

Initially, the design is kept simple with minimal CSS styling. The alternate text in the image space is centered and a margin is used instead of multiple line breaks at the top. The usage of 'em' units is preferred for consistency and ease of use.

Borders are added to visually separate different elements in the example code provided.

body {
  box-sizing: border-box;
  font-size: 16px;
  margin: 0;
  padding: 0;
}

.gallery {
  margin-top: 3em;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: 1fr;
  grid-gap: 1.25em;
  padding: 1.25em;
  border: solid 1px #00FF00;
}

.image-container {
  border: solid 1px #FF0000;
  display: grid;
  place-items: center;
}

.image-container img {
  width: 100%;
  height: auto;
  border: solid 1px #0000FF;
  text-align: center;
}

.caption {
  position: fixed;
  bottom: 1.25rem;
  left: 50%;
}

.caption {
  transform: translateX(-50%);
  background-color: rgba(0, 0, 0, 0.7);
  color: #fff;
  padding: 0.625em;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease-in-out;
  z-index: 2;
}

.image-container:hover .caption {
  opacity: 1;
}
<div class="gallery">
  <div class="image-container">
    <img alt="Placeholder Image 1" src="1.jpg">
    <div class="caption">Caption for Image 1</div>
  </div>
  (more images and captions...)
</div>

A suggestion is made to simplify further by removing wrappers and placing everything in a single grid layout with captions in the last row. A fixed row count is required for this approach.

* {
  box-sizing: border-box;
  font-size: 16px;
  margin: 0;
  padding: 0;
}

body {
  padding-top: 3em;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(6, auto) [last-line];
  grid-gap: 1.25em;
  padding: 1.25em;
  padding-bottom: 0;
  border: solid 1px #00FF00;
  justify-content: center;
  align-items: center;
}

.gallery>img {
  height: auto;
  border: solid 1px #0000FF;
  text-align: center;
}

img:hover+.caption {
  opacity: 1;
}

.caption {
  background-color: #000000B2;
  color: #fff;
  padding: 0.625em;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease-in-out;
  z-index: 2;
  grid-row: last-line;
  grid-column: 1 / span 2;
  position: fixed;
  bottom: 1.25rem;
  transform: translateX(-50%);
  margin-left: 50%;
}
<div class="gallery">
  <img alt="Placeholder Image 1" src="1.jpg">
  <div class="caption">Caption for Image 1</div>
  (more images and captions...)
</div>

An alternative approach suggests eliminating all wrappers, moving captions to the last row of the grid, and adjusting their position with a negative margin. This solution deviates slightly from the original request and is initially hidden by default.

* {
  box-sizing: border-box;
  font-size: 16px;
  margin: 0;
  padding: 0;
}

body {
  padding-top: 3em;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-rows: repeat(6, auto) [last-line];
  grid-gap: 1.25em;
  padding: 1.25em;
  padding-bottom: 0;
  border: solid 1px #00FF00;
  justify-content: center;
  align-items: center;
}

.gallery>img {
  height: auto;
  border: solid 1px #0000FF;
  text-align: center;
}

img:hover+.caption {
  opacity: 1;
}

.caption {
  background-color: #000000B2;
  color: #fff;
  padding: 0.625em;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease-in-out;
  z-index: 2;
  grid-row: last-line;
  grid-column: 1 / span 2;
  margin-top: -2.5em;
}
<div class="gallery">
  <img alt="Placeholder Image 1" src="1.jpg">
  <div class="caption">Caption for Image 1</div>
  (more images and captions...)
</div>

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

Issue with Bootstrap carousel not centered on the page

Currently working on a carousel using bootstrap. The goal is to display 5 images at a time and have it advance by 1 slide each time. The main issue I'm facing right now is that the elements are not properly positioned within the container, and the tr ...

Ways to adjust the size of the parent element based on the height of a specific element

I am faced with a situation where I need to identify all elements belonging to a specific class and adjust the padding-bottom of their parent div based on the height of the matched element. For example, consider the following structure: <div class=&ap ...

Is it possible to reference the same PHP file in multiple locations?

Currently, I am working on a web development project where I have created a header.html file to store the common header for all my webpages. This header file is located in the parent directory. Within my header.html file, I have included references to bot ...

Extracting information from an external website in the form of XML data

Trying to retrieve data from an XML feed on a remote website , I encountered issues parsing the XML content and kept receiving errors. Surprisingly, fetching JSON data from the same source at worked perfectly. The code snippet used for XML parsing is as f ...

Issue with Firefox: JQuery-UI checkboxes failing to be selected when using Shift or CTRL keys

UPDATE: After reviewing mark.hch's response, I discovered that this issue only occurs on Firefox. To clarify my previous explanation: I am not seeking assistance with the event handler - it was just provided as context. My specific requirement is for ...

Assistance needed for aligning a div to the right using

Check out my website at www.whiterootmedia.com. I am trying to prevent my ads on the right from wrapping and disappearing off the screen when the width is around 800px. I believe I need to create a wrap div container, float my ads div to the right, set a ...

Linking children to their parents in a mat tree structure

I'm looking to create a mat tree based on the provided diagram. https://i.sstatic.net/cTY2w.png So far, I've managed to design the icons and boxes, but I'm struggling with drawing the connecting lines. Can anyone assist me with this part? I ...

The width property is not being passed down to the table

I'm experiencing difficulties with the scaling of my body content. When the page is initially opened, it scales to 100%. However, when I resize the screen to make it wider, the content gets bigger but when I try to make it smaller again, it remains a ...

Learn how to center content and stretch a column using Vuetify frameworks

Looking for a layout with 2 columns of equal height and centered content vertically? Here's how to achieve it using Vuetify! Check out the code snippet below: <div id="app"> <v-app> <v-row align="center"> ...

Styling in CSS for aligning a drop-down menu to the right side

I recently incorporated a CSS/JavaScript drop-down menu on my website, which I sourced from the following page: However, I am facing an issue with aligning the far-right drop-down properly. Specifically, I would like the items in the far-right drop-down t ...

Tips for sending PHP variables together with a Typeahead variable

I have simplified this code as much as possible. I have a typeahead variable that is functioning correctly. However, I also need to pass two unrelated variables $php_var1 and $php_var2 along with the typeahead variable. These PHP variables are initialized ...

CSS Panel Assignments

I am attempting to split the screen where 80% is dedicated to displaying a Google map at the bottom, and the remaining 20% displays content in a div with the ID '#data' at the top. However, the code below does not seem to achieve this layout as i ...

Access a designated div within an iframe following the submission of a form

I wanted to discuss my current setup... Currently, I have two pages in the project - index.html and cart.html. The issue arises when a form on index.html is submitted because it loads cart.html into a pop-up iframe. In this case, the entire cart.html page ...

Positioning a list or div in CSS and HTML can be achieved with or without thumbnails

My website showcases our expertise in various topics, each with a header, image, and a brief list. The page displays thumbnail images and headers for all topics, with one topic highlighted in full that users can navigate through by hovering over different ...

Populate table cells dynamically using an array of elements

I am trying to dynamically change the background color of table cells based on their values. Here is an example of the code I have written: applySchedules = function(schedules){ $.map(schedules, function(value, index){ $('#'+va ...

Guide on Deleting Specific Item Using SQL Data Source Delete Statement

I am facing an issue when trying to remove a specific item from a grid view in my SQL server database. I have configured DataKeyNames and linked a sql data source with the grid view. The delete command is set as follows: DeleteCommand="DELETE FROM product ...

Adjust the size of a nested div within a bootstrap container

My dilemma arises when nesting a product slider div inside a bootstrap col-lg-x div, as illustrated below: https://i.sstatic.net/9tCVA.png The issue lies in the layout distortion that occurs within a div.row > div.col-lg-8 structure, where images are ...

"Disable the scroll bar for a specific component in Angular while keeping it visible for the

Is there a way to deactivate the overflow: hidden style for just a single component? The style is defined in my style.scss file as shown below: .Visible-scroll{ overflow: auto } ...

Why does Angular Redirection initiate both my method and NgOnInit?

As a beginner in Angular, I am encountering an issue with Angular Redirection. Whenever I manually clear storage, the code executes ngOnInit, prompts an error, and redirects to the Login page as expected. However, when I attempt to do the same using the lo ...

The conflict between CSS link types and image links causing disruption

I am currently designing my own Tumblr theme and have encountered an issue that I cannot resolve due to lack of expertise. I was hoping someone with more knowledge could assist me. Essentially, I have implemented a feature where hovering over a link chang ...