Is it possible to dynamically adjust image size based on comment box dimensions?

Seeking guidance on a grid photo gallery with mismatched sizes and aspect ratios of images.

I am trying to determine how to adjust the height of the <img> element to accommodate the varying heights of the <figcomment> (depending on the number of tags).

  • If I set the <img> height to '100%', the <figcomment> overflows the <figure>.
  • If I use 'height: auto;', landscape images leave excessive space above the <figcomment>.

Is there a way to properly size the <img> so it fills the <figure> without pushing the <figcomment> outside?

Placing the <figcomment> above the <img> and setting <figure>'s 'overflow: hidden' achieves the desired outcome, but preferably I would like the tags beneath the image?

<figure>
  <a href="/photo/349">
    <img src="/images/77.jpeg">
  </a>
  <figcaption>
    <a href="/tag/dog">dog</a>
  </figcaption>
</figure>
<figure>
  <a href="/photo/251">
    <img src="/images/104.jpeg">
  </a>
  <figcaption>
    <a href="/tag/tink">tink, </a>
    <a href="/tag/dog">dog, </a>
    <a href="/tag/human">human</a>
  </figcaption>
</figure>
<figure>
  <a href="/photo/361">
    <img src="/images/88.jpeg">
  </a>
  <figcaption>
    <a href="/tag/adam"&t;gt;adam, </a>
    <a href="/tag/dio"&t;gt;dio, </a>
    <a href="/tag/dog"&t;gt;dog, </a&t;
    <a href="/tag/human">&t;human, </a>
    <a href=/tag/car">car</a>t;
  </figcaption>&t;
</figure>&t;
section {
  display: grid;
  column-gap: var(--colGap);
  row-gap: calc(var(--colGap) * 1.618);
  grid-template-columns: repeat(auto-fill, var(--photoW));
  justify-content: center;
}
figure {
  width: var(--photoW);
  height: calc(var(--photoW) * 1.618);
  padding: 5px;
  border: 2px solid var(--accent);
  border-bottom-left-radius: 8px;
  border-bottom-right-radius: 8px;
}
img {
  width: 100%;
  height: auto;
  object-fit: cover;
}
figcaption {
  padding: .3em;
  margin-left: -7px;
  width: var(--photoW);
  color: var(--dark);
  font-size: 18px;
  text-align: center;
}

Answer №1

After some consideration, I discovered a solution to the problem by adjusting the image size to completely fill the figure and then using absolute positioning for the figcaption.

img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
figcaption {
  position: absolute;
  right: 5px;
  bottom: 2px;
}

While this approach is effective, I am curious if there are more sophisticated alternatives available?

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

Change the size of a particular div upon hovering over another element

I'm trying to figure out how to scale my div with text when a user hovers over a button, but I've tried various operators like >, ~, +, - with no success. section { background: #000; color:#fff; height: 1000px; padding: 150px 0; font-family ...

Issue with Bootstrap 3 navbar collapse not expanding properly

I am currently working on a responsive navbar that collapses for mobile users. The collapse icon is displayed when the screen size is reduced, however, it does not expand upon clicking. <div id="headerfront" class="navbar navbar-static-top"> ...

Is there a way to undo the transition when hovering out?

Is it possible to achieve a reverse transition animation on hover out using CSS? I want the "Menu" text to slide to the right blue line when I hover out, and after a delay of 400ms, slide back from the left grey line. Can this be done? .menu { displ ...

Challenges arising from the placement of tooltips among multiple elements

I am currently working on adding tooltips to images on my website, but I am encountering a issue. The images are floated to the left with three images per row and multiple rows. The tooltip is meant to appear on the right side of the image, which works fin ...

Unequal height among each div's elements

I'm struggling with creating a footer divided into 3 blocks, but they do not have equal heights, causing the border-right line to appear uneven. Here is a screenshot of the issue: Not equal height of elements Any suggestions on how to fix this? Is m ...

Ways to navigate to a tabbed section that is currently inactive

One of my challenges involves managing a tabbed form with multiple tabs: <ul class="tab-links"> <li class="active"><a href="#tab1">Pending</a></li> <li><a href="#tab2">All</a></li> ...

Using JavaScript to manipulate the DOM in MSIE (Internet Explorer) and ensuring seamless processing

I am encountering an issue with simple HTML5 DOM processing not working on MSIE 9. It functions properly without a DOCTYPE tag, however, when I include the DOCTYPE html (indicating html5), MSIE 9 fails to process it. Is there an alternative approach I can ...

The initial line of the HTML canvas features a varying width

Why does the first line on the canvas have a different width than the others? I'm having trouble figuring it out. Can anyone provide some assistance? var x = document.documentElement.clientWidth; var y = document.documentElement.clientHeight; var ...

The TailwindCSS breakpoints are not being recognized in the styling

I am currently working on a NextJS 14 project where I am integrating Tailwind CSS. Here is a snippet of the code I am using: <div className="text-amber-dim md:text-amber flex-1 h-screen flex items-center justify-center"> <div classNam ...

Tips for overriding CSS rules marked as !important by a script

I recently implemented a JavaScript plugin from an external service that comes with its own CSS rules. However, these rules are not compatible with my layout as the width needs to be changed from 540 to 700px: #chat-container.bbhorizontal .main .booking { ...

fa-arrow-circle-o-down and fa-arrow-circle-down

Is it possible to modify the fa-arrow-circle-o-down icon to have the same arrow type as the fa-arrow-circle-down icon? The current arrows on these two icons are different and I want them to be consistent. ...

Tips for embedding Google Maps route planner on your website's iframe

I am currently in the process of developing a brand new WordPress website that includes a "Contact" menu option. Within this menu, I have integrated a map using Google Maps iframe code. Now, my next task is to incorporate a route planner below the map. To ...

My HTML file is experiencing issues with the Bootstrap model implementation

I have integrated the CDNs (CSS/JS) provided by Bootstrap into my HTML file. I then copied and pasted the code for a demo modal from their website into the body tag. However, when I view the page in the browser and click on the button, the modal does not ...

Masterful Text Pairing: Using Zen Coding Techniques for Crafting Intricate Nesting Structures

Result : <div id="header"></div> <div id="body"> <hello> <world></world> </hello> </div> Mapping to : div#header+div#body>hello>world Which command should be used to achieve the desi ...

Access several different dynamic URLs through a single Ajax request

I have a scenario where I am showing multiple dynamic links within the same div. The first link works perfectly fine with ajax loading the content, but the subsequent links do not work. How can I make it so that the content of all links can be loaded withi ...

The button press does not seem to be functioning

I am facing an issue with a button located on a modal pop up window. The click event of the button does not seem to be firing. This button is placed within a gridview. Strangely, when I place the button outside the gridview it works perfectly, but inside ...

Identify the custom HTML tag that is causing issues in IE8

Our angular.js app heavily utilizes custom tags, and I am faced with the challenge of ensuring compatibility with IE8. Here's what we have set up: Angular (1.1.5) HTML5 mode Custom tags for directives are used extensively (without replacing them) I ...

Delaying the animation of slider elements

I am trying to create an image slider with texts and a button that slide up. However, I want each element to appear with a delay, but it's not working as expected. Currently, all elements load at once before the animation starts. To view my code, cli ...

What is the best way to rearrange images within a grid container?

I apologize for asking numerous questions, but could someone please guide me on how to position an image slightly to the right of center or just 1 inch to the left of center? http://jsfiddle.net/96d7udd7/ Below is the HTML code snippet: <figure class ...

Dynamically Generate HTML Elements with jQuery

My current goal is to generate an HTML element using JavaScript with jQuery by pulling information from an Array of Objects that contain details like the element tag, class, and other attributes or styles. What are the Benefits of Using jQuery over Raw HTM ...