Positioning images in sync with the text of captions

I am trying to arrange multiple images in a row with captions underneath each of them. I want the images aligned while allowing the captions to flow naturally below the images, even if they vary in length. Despite following advice from previous posts on SO to limit caption width to match the image, I am facing issues. The problem is that when a caption spans multiple lines, it pushes the image up and all the captions align instead of the images themselves.

Here is the HTML structure:

<div class="wwapictures">
    <div class="wwaimg" id="1">
        <figure>
            <img src="image1.jpg">
            <figcaption>Caption 1</figcaption>
        </figure>
    </div>
    <div class="wwaimg" id="2">
        <figure>
            <img src="image2.jpg">
            <figcaption>Longer Caption 2<br>With Multiple Lines</figcaption>
        </figure>
    </div>
</div>

And the accompanying CSS:

.wwapictures .wwaimg img {
    padding: 10px 5px 0px 5px;
}

.wwapictures .wwaimg {
    display: inline-block;
    padding-bottom: 100px;
}

.wwapictures figure {
    width: min-content;
}

How can I ensure that the images are aligned properly while accommodating captions of varying lengths under each picture?

Answer №1

Transforming my feedback into a comprehensive solution.

  1. Ensure uniqueness of IDs in xml/html by converting #wwaimg to a css class .wwaimg
  2. Include a min-height css attribute on the figures to match the maximum expected height of the longest caption, aligning everything and creating space if the caption is shorter (assuming images are of equal height)

For instance, the following example utilizes display:inline-block and float:left to construct the grid, but you may also contemplate employing flexbox:

.wwapictures {
  position: relative;
  width: 100%;
  display: block;
  box-sizing: border-box;
}
.wwaimg img{
  position: relative;
  display: block;
  width: 100%;
  height: 200px; /* images should have same height*/
}
.wwaimg figcaption{
  position: relative;
  display: block;
  margin-top: 10px;
  min-height: 70px;
}
.wwaimg {
  position: relative;
  width: 45%;
  box-sizing: border-box;
  display: inline-block;
  margin: 0;
  border: 1px solid #f00;
  padding: 10px;
  float: left;
}
<div class="wwapictures">
 <figure class="wwaimg">
  <img src="wwa_dummy_picture.jpg" />
  <figcaption>Joe Smith<br>Co-Founder <br> Project Coordinator</figcaption>
</figure>
<figure class="wwaimg">
  <img src="wwa_dummy_picture.jpg" />
  <figcaption>Bob Joe <br> Co-Founder</figcaption>
</figure>
 <figure class="wwaimg">
  <img src="wwa_dummy_picture.jpg" />
  <figcaption>Joe Smith<br>Co-Founder <br> Project Coordinator</figcaption>
</figure>
<figure class="wwaimg">
  <img src="wwa_dummy_picture.jpg" />
  <figcaption>Bob Joe <br> Co-Founder</figcaption>
</figure>
   </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

Executing speechSynthesis.speak() in JavaScript synchronously

I am currently working on a script that will make the browser speak an array of strings in sequential order. However, I've encountered an issue where only the first and last string are being read, rather than each one sequentially. How can I adjust my ...

What could be causing external stylesheet styles to take precedence over internal and inline styles?

Currently, I am working on a website that utilizes a global stylesheet which applies styles to specific element types. Personally, I find this approach inefficient. As I develop ASP.NET user controls for different pages, I consistently encounter issues whe ...

Struggling to make Tumblr Javascript function properly

I have integrated the following code snippet: <script type="text/javascript" src="http://underlineent.tumblr.com/api/read/json"> </script> Unfortunately, my Tumblr blog is not appearing on this site. Can anyone provide assistance? The provid ...

Floating container leads to delays

My goal is to create a div that changes when I hover over it. However, when I move my mouse into the div, there seems to be some lagging and the content does not appear clearly. Below is the HTML and CSS code I have used: .unshow-txt:hover, .unshow-txt: ...

Issue with JQuery: Inability to deactivate an element after receiving an Ajax response

My dynamic dialogue box, generated via Ajax return, presents a challenge involving the dynamically changing drop-down list element $('#functionSelect'). I require this list to trigger disabling of input fields within the dialogue box upon changes ...

Angular's Motion Module

Incorporating Angular-5 into my project has introduced a plethora of animations on various pages. While the utilization of jQuery provides a straightforward approach for adding and removing classes to DOM elements, it is frequently advised against using jQ ...

After resizing, a div with a height of 100% will scroll upwards

I have a single-page website consisting of three sections, each spanning the entire width and height of the window. While resizing the window on the first or last section, everything adjusts smoothly. However, when I am on the second section, things get a ...

Tips for keeping the menu open even when you're not hovering over it with your cursor

I put together a stylish drop-down menu based on some web examples, but my manager pointed out that it can be inconvenient to use because the menu closes when the mouse moves off of it. I've tried various workarounds as outlined here, but none have in ...

Stop navigation links from resizing when the page content does not fill the height of the display

As I work on my very first website, I've encountered a strange issue with the navigational links at the top of each page. I've noticed that pages with content exceeding the maximum vertical pixels occupy a specific width, while those with less co ...

Designing geometric forms using the vertices of a Sphere in ThreeJs

My current project involves creating shapes based on vertices that have already been generated using a formula. I have successfully connected lines between these vertices using a threejs method, but now I want to take it a step further and create map tiles ...

Navigation bar fails to expand to the full width of the page

I need assistance with optimizing my website for mobile use. I am facing issues with excess white space on the right side of the navbar. Here is a visual representation of the problem: I have attempted modifying the CSS of the body and nav tags, as well ...

Implementing a design aesthetic to installed React components

After downloading a react multiselect component created by an individual, I installed it using npm install .... and incorporated the component in my react code with <MultiSelect .../>. The dropdown checkbox 'menu' is designed to allow users ...

What could be causing the tooltip to not function properly with data-html="true"?

I am having trouble with customizing a tooltip. The data-html="true" attribute is not working as expected, and I can't seem to figure out what the issue is. .tooltip-custom { display: inline; position: relative; } ...

CSS word-wrap property not functioning within a div element

I am currently using the ionic framework to iterate words from a database. However, I am facing an issue where the words span across the div and do not break when reaching the end of the div; instead, they are truncated. I have tried applying the break-wor ...

Using JQuery to manipulate content with pseudo elements

I'm attempting to modify the arrow displayed in an :after pseudo element. Here's my CSS: legend:after { content: url('/html/img/arrow-fieldset-up.png'); } I am trying to change it with the following JavaScript/jQuery: $('lege ...

Glimmering ivory during transformation

I have created a simple script that changes the background image when hovering over a div. However, I am experiencing a flickering issue where the image briefly turns white before transitioning. I have tried to resolve this problem but have not been succes ...

Refreshing a specific div within an HTML page

I am facing an issue with the navigation on my website. Currently, I have a div on the left side of the page containing a nav bar. However, when a user clicks a link in the nav bar, only the content on the right side of the page changes. Is there a way t ...

The function of modal in JavaScript is not working properly

I am encountering a problem with my web page that is running on Wildfly 12. It is a simple Java EE project that I developed in Eclipse Neon. The issue arises when I try to use Bootstrap modals, as every time I attempt to open or use the methods in a JavaSc ...

Using setInterval() does not automatically fetch JSON data at a specified interval for me

Attempting to constantly update the latitude and longitude coordinates from a dynamic API has proven to be a challenge. Despite utilizing jQuery with a set interval of 1000 milliseconds, the data retrieval from the API does not occur as expected. While i ...

Using jQuery Chosen to showcase numerous select fields simultaneously within a webpage

I have implemented a search feature using in a dropdown list for user convenience. On the post/create page, I have added 2 tabs: Link & Text Both tabs include a "subreddit_id" field that utilizes $(selector).chosen();. Interestingly, the jQuery chos ...