CSS - Issue with aligning image captions

I created an HTML snippet that has the following structure:

                <div class="large-4 columns">
                    <div class="promo-box">
                        <a href="#">
                          <img src="/our-projects-sfk.png">
                          <div class="caption">
                            <h5><Our Projects</h5>
                            <p>For 20 years, we have been delighting families with a three-day dream trip to Kristiansand Zoo for children battling cancer every summer.</p>
                          </div>
                        </a>
                    </div>
                </div>

However, I am facing a challenge in setting the width of the div and caption based on the image width because I do not know the image width beforehand. I tried various methods such as setting display: inline-block, display: table, using figure and figcaption tags, but none of them provided the desired outcome. Can you help me figure out how to make the caption width match the image width?

Here is the CSS code I attempted to use:

.promo-box {
  display: table;
}

.promo-box img {
  width: 100%;
  height: auto;
  vertical-align: top;
  margin-bottom: 3px;
}

.promo-box .caption {
  display: table-caption;
  caption-side: bottom;
  box-sizing: border-box;
  padding: 10px;
  margin: 0;
}

Unfortunately, the result was not as expected:

https://i.sstatic.net/Y9JBu.png

Answer №1

When dealing with a .promo-box that is structured as a table, it's important to designate the image and caption containers as table-rows and apply text-align: center to the caption container.

Additionally:

Take a look at this codepen for reference: http://codepen.io/anon/pen/zNyQGQ

I have also included a fixed width for the container - this prevents the image from stretching to match the length of the caption text.

FOLLOWING A COMMENT:

If needed, remove the width specification on the container and adjust the CSS for the caption like so:

.promo-box .caption {
  display: table-caption; /* modified */
  width: 100%; /* modified */
  box-sizing: border-box;
  padding: 10px;
  margin: 0;
  text-align: center;
}

Check out the updated version on codepen: http://codepen.io/anon/pen/LxMwBK

Answer №2

If you're looking to ensure the text aligns perfectly with the image width, you can achieve that without involving any table or table-caption elements.

By utilizing img and .caption, both will automatically adjust to fit within the full width of .promo-box.

.promo-box {
  border: 1px solid red;
  width: 50%;
}
.promo-box img {
  width: 100%;
  height: auto;
  margin-bottom: 3px;
}
.promo-box .caption {
  margin: 0;
}
<div class="large-4 columns">
  <div class="promo-box">
    <a href="#">
      <img src="http://dummyimage.com/400x250">
      <div class="caption">
        <h5><Our Projects</h5>
        <p>For 20 years, we have been delighting families with a child battling cancer by providing them with a three-day dream trip to Kristiansand Zoo in the summer.</p>
      </div>
    </a>
  </div>
</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

Is jQuery causing the table to exceed the page width?

I have a table in my HTML document that is displaying too many columns in a single row (~25), causing the table to stretch beyond the size of the page and creating unwanted scrollbars. I am seeking a solution to shrink the content of each column while also ...

Steps for updating a text section beneath a carousel

I'm looking to enhance my webpage with a bootstrap carousel, which is pretty standard. However, I want the content under each slide to change dynamically, but I'm struggling to make it work. As a newbie, I could use some guidance. I've atte ...

What is the best way to ensure that the body element is as large as the html element?

My goal is to have the body extend as the page size increases. I attempted setting the height of the body to 100% and the height of the html element to 100%, but it didn't produce the desired outcome: In this illustration, blue represents the body an ...

Incorporating Materialize CSS and MaterialUI within a single React Application

Presently, I have a straightforward non-React application built with HTML, Materialize, and JavaScript that I am considering converting to React while also making the switch from MaterializeCss to MaterialUI. However, I am not looking to make this transiti ...

Deliver the event target within the data-bind click HTML

I am having trouble sending the event target when passing parameters in data-bind. <button class="tablinks" data-bind="click:$root.notify.bind(this, 1, 'foo');" id="defaultOpen">PRINCIPAL</button> self.notify = function (str, id, e) ...

Javascript issue: opening mail client causes page to lose focus

Looking for a solution! I'm dealing with an iPad app that runs html5 pages... one specific page requires an email to be sent which triggers the Mail program using this code var mailLink = 'mailto:' + recipientEmail +'?subject=PDFs ...

Seamless animated loading gif as a looping background visual

Is there a way to find a GIF that can be used as a repeated background on an HTML element, creating the illusion of a seamless block? https://i.sstatic.net/5BKxW.gif (source: opengraphicdesign.com) I am looking for an 80x80 GIF that can be repeated as ...

In my development environment, the page does not have scroll functionality, but in the production environment, it is scrollable

Whenever I open a table or any other element with overflowing content, I encounter an issue with the scrolling bar. Even though the CSS includes overflow-y: scroll;, the scroll bar on the right remains in gray and does not allow me to scroll down when the ...

stop cascading style sheets from being overwritten

In my ASP.NET web forms project, I am utilizing Telerik controls. The Problem In one instance, I need to retrieve HTML data from the database, which includes a lot of CSS, images, and HTML content. Here is an excerpt of my code: <telerik:RadLabel ...

Attempting to execute code on a database using PHP and MySQL

Hey there! I am just diving into the world of HTML and PHP, attempting to create a page that can execute an SQL query to transfer data from one table to another. However, I'm facing some difficulties in making it work as I need a user-provided date t ...

What is the best way to iterate through anchor links surrounding an image and dynamically load content into the figcaption element using AJAX?

Seeking assistance for an issue related to my understanding of the $this keyword in jQuery. I am facing a problem where I have 3 images on a page, each wrapped in an anchor link. My goal is to loop through these links, retrieve the URL of each anchor lin ...

How to use jQuery to select an element by using 'this' on a button

I have a total of 5 divs, each containing 5 different texts. <div class="text"> <%= theComment.text %> </div> I am working with node.js, MongoDB, and Mongoose. In addition to the divs, I also have a button labeled EDIT with a cl ...

What could be causing my handle button to slide off the timeline towards the right?

I'm facing an issue with my volume bar component where the slider button is rendering outside of the timeline instead of on top of the progress bar. I need assistance in adjusting its position. // Here is the code for my volume bar component: import ...

Is it possible to utilize a separate, standalone stylesheet for media queries?

My understanding of media queries evolved when I discovered them yesterday during my research on creating mobile-optimized pages. I learned that a media query refers to a stylesheet that complements and supersedes the current one, which differed from what ...

What is the method for invoking a function with arguments within an HTML `<p>` element?

I am looking to display like and dislike percentages on cards. <v-card v-if="software[2] == searched || searched == ''" class="software-card" > <h3>{{ software[2] }}</h3> ...

Modify the NAME attribute when clicked using Jquery

I am attempting to modify the NAME attribute of a DIV with the text from a textbox using jQuery. Take a look at my code snippet: http://jsfiddle.net/e6kCH/ Can anyone help me troubleshoot this issue? ...

Exploring array data retrieval in Angular

I need to display various inch sizes for different bike models. I have a json file containing an array of bike data. [{ "name": "Mountainbike", "img_url": "assets/img/mountainbike.jpg", "mount_size": [{&quo ...

A guide on utilizing ClassName to insert new items into a DIV

Original HTML code: <span class="specLink"> <specialty><a title="Plastic Surgery" href="link2.aspx">Plastic Surgery</a></specialty> </span> <br /> <span class="specLink"> <specialty2><a titl ...

Connect the CSS file in React index.html to the Flask backend

I am attempting to connect a CSS template that is serving static backend content through Flask with my React frontend. Here is the structure I am working with: client/ src/ components/ menu.jsx public/ ...

Place picture in the bottom right corner of the div without using absolute positioning

I am looking for a way to position an image in the lower right corner of a div, but I want to avoid using absolute positioning. The reason for this is that when I use absolute positioning, the text wrap is affected due to the float right applied to the ima ...