Tips for ensuring the correct size of images and their captions within a figure element

I'm struggling with the CSS for an image inside a figure. Here is the HTML:

#content figure {
  display: inline-block;
  border: 1px solid #333333;
  height: 200px;
  margin: 10px;
}
#content img {
  height: 180px;
  background-size: auto 100%;
  background-repeat: no-repeat;
  background-position: center center;
  border: 1px solid red;
  margin: 0px;
  margin-bottom: -5px;
}
#content figcaption {
  border: 1px solid blue;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 12px;
  text-shadow: none;
  color: black;
  text-align: center;
  height: 20px;
}
<div id="content">
  <figure>
    <img style="background-image: url('http://placehold.it/300x300');"></img>
    <figcaption>image caption that is too long to fit</figcaption>
  </figure>
  <!--... /*Here it can be multiple figure after each other*/-->
</div>

I am looking for the figure to maintain a fixed height of 200px, while the figcaption adjusts its height automatically based on the text content. The image should then fill the remaining height, scaling the width proportionally. The figure's width should match that of the image within. The current code is not achieving all these requirements and I'm unsure of how to modify it accordingly.

Answer №1

To enhance the appearance of your #content figcaption style, make sure to include word-wrap: break-word;, set height: auto;, and match the width with your image. Here is an example:

#content figcaption {
  border: 1px solid blue;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 12px;
  text-shadow: none;
  color: black;
  text-align: center;
  height: auto;
  word-wrap: break-word;
  width:300px;
}

Answer №2

#content figure {
  display: inline-block;
  border: 1px solid #333333;
  height: 200px;
  margin: 10px;
}
#content img {
  height: 180px;
  background-size: auto 100%;
  background-repeat: no-repeat;
  background-position: center center;
  border: 1px solid red;
  margin: 0px;
  margin-bottom: -5px;
}

#content figcaption {
  border: 1px solid blue;
  font-family: Verdana, Geneva, sans-serif;
  font-size: 12px;
  text-shadow: none;
  color: black;
  text-align: center;
  height: auto;
  word-wrap: break-word;
  width:300px;
}
<div id="content">
  <figure>
    <img style="background-image: url('https://i.sstatic.net/8z6RE.jpg?s=48&g=1');"/>
    <figcaption>image caption that exceeds the length limit</figcaption>
  </figure>
  <!--... /*Here it can be multiple figure after each other*/-->
</div>

Answer №3

I am unable to find a way to automatically adjust the height of the image to fit the figcaption (since the figcaption height varies based on content).

One option is to set the image to cover the entire figure height and then position the figcaption on top of the figure.

Check out the code snippet on FIDDLE for reference.

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

"Efficiently manage search suggestions and arrange outcomes for autofill text input utilizing jQuery

Is there a way to eliminate the message and structure the results in a sleek dropdown using CSS without relying on Bootstrap or pre-made CSS files? http://jsfiddle.net/SCuas/96/ var aCleanData = ['test','test1','abcd',' ...

Changing between two images using HTML and CSS

I am currently designing a WordPress theme and I would like to create an effect where two different thumbnail images switch on hover. The code that I have come up with so far looks something like this : <a class="thumb" href="posturl"> <img src= ...

I aim to adjust the width of a logo and ensure its size is fixed for optimal visibility

The selected logo needs to be more visible. I have attempted to use media queries to adjust its visibility, but I am having trouble with it. I want the logo to be able to stretch in width, but I am unsure how to achieve that. Currently, this is what I hav ...

Retrieve the browser version of a client using C# (When Internet Explorer Compatibility mode is activated)

Is there a way to retrieve the actual version of the client's browser from C# code? Using Request.Browser or HTTP_USER_AGENT provides details, but in Internet Explorer (IE), when compatibility mode is enabled, it always returns IE 7 regardless of the ...

The elements at the bottom of the Google homepage do not stay in place when hovering

I'm facing an issue and seeking guidance on how to make the footer and 'Google in different languages' fixed when hovering over the buttons. I'm attempting to create this on my own without referencing the Google homepage code in Chrome ...

What are the risks of using react + bootstrap without importing bootstrap.js?

When bootstrap library is used along with React, potential unpredictable behavior and difficult bugs may arise due to DOM modifications. Is it feasible to use bootstrap without including bootstrap.js? What are the drawbacks? While I am aware of the ' ...

Getting creative with jQuery: Adding a random class using addClass()

The html/css: <style> div.myWords p { display: hidden; } p.show { display: block; } </style> <div class="myWords"> <p>Hello</p> <p>Hola</p> <p>Bonjour</p> </div><!-- myWords --> Is ther ...

Is there a way to format a block of text so that right-to-left text aligns to the right, while left-to-right text aligns

I have a content block that includes text in both left-to-right and right-to-left languages, and I need to ensure that both display correctly. If the user begins with left-to-right text, it should be displayed from left to right with left alignment If the ...

What is the best way to divide a Modal Header column into two separate sections?

Is there a way to center the text in the Modal Header and have the H1 and P tags occupy one row each? Applying d-block to each creates two columns. I also tried increasing the Height in the Modal Header without success. If you have a solution, please hel ...

Tips for creating two columns within a Bootstrap row, one with a form and the other with an image, that are both the same height and responsive

I am facing an issue with my bootstrap columns. One column has a form, while the other contains an image. The image section is set to 100% width, making it responsive when the screen size changes. However, the form remains at a fixed height regardless of t ...

Internet Explorer seems to be having trouble detecting changes made to a jQuery checkbox

Here is an example of HTML code: <input type="checkbox" id="openInNewWindowCheckBox" value ="some_value" /> Accompanied by a jQuery script: $("#openInNewWindowCheckBox").change(function(){ if($(this).is(':checked')) ...

Storing dynamic content on a server and retrieving it for future use

I'm working on a webpage that allows users to create elements dynamically and I want to save those elements to the server. When someone else visits the page, I want them to see those saved elements as well. I'm not too familiar with web programm ...

Use Python to fetch a file from a webpage without having to actually open the webpage

I needed a method to automate the download of a file from a particular website without having to manually open the website. Everything should be done in the background. The website in question is Morningstar, and a specific example link is: . On this page ...

My CSS seems to be causing an issue and preventing the function from running correctly. Now I just need to

I'm currently working on a project and following a tutorial to help me create a navigation bar. The tutorial I am using can be found here: https://www.youtube.com/watch?v=gXkqy0b4M5g. So far, I have only reached the 22:05 mark in the video. I have su ...

Show JSON information from a jQuery post request

Greetings, I am currently working on implementing a login script using Ajax. My objective is to retrieve and display data such as email and username, and then store it in local storage. Although I can successfully obtain the data in JSON format, I want to ...

Creating custom CSS for Styled Components in ReactJS

I'm struggling with CSS in React using styled components. Below is the code snippet I am working with: import React from 'react'; import { Navbar, Container, Row, Col } from 'reactstrap'; import styled from 'styled-components& ...

Using jQuery to create an interactive form with PHP integration

I've set up a PHP form to post to a database successfully. However, after including it in a jQuery web page, the form is no longer posting to the database. Check out the code for both the webpage and the PHP file below. Interestingly, the form works f ...

Tips for displaying reCaptcha responses above or below the contact form

Previously, @ADyson provided a solution on how to display returns on a new page. I appreciate the help, but I would prefer to see it under the contact form on that specific contact page. I was advised to use an echo statement and an if statement, which I&a ...

HTML script tag failing to load

As a beginner in using scripts in HTML, I'm currently following a tutorial that suggests loading the script after the body for certain reasons. In my own code, I found that I need to place the script both in the head section and after the body. Remov ...

Overcoming Challenges with Ajax Success in Bootstrap DataTable Modal

Recently, I created a webpage featuring a top fixed navbar and a CRUD datatable with pagination supported by buttons that trigger modals. However, I'm encountering an issue where upon clicking the delete button in the modal to remove a record, the mod ...