Adjusting the size of an image and its accompanying span within the container div

I have a fixed width and height div where I want to include an image with a caption (using img and figurecaption) without exceeding the parent's dimensions.

Here is what I attempted: `

parent-> 
height: 100px;
width: 100px;
margin: 0;
img->
display: block;
height: 100%;
width: 100%;
figurecaption->
text-align: center

`

However, when the image is larger than the specified height and width, the caption overflows. How can I prevent this from happening? Thank you.

Answer №1

If you want to ensure that both the image and the caption stay within the boundaries of the parent container, there are a few options available to you. One approach is to set the image's max-height and max-width to 100% and then position the caption on the bottom. If you prefer to keep them completely separate, you can use the following code:

#container {
  height: 400px;
  width: 300px;
  border: 1px solid #000000;
  text-align:center;
}
#image {
  max-height: calc(100% - 50px);
  max-width: 100%;
}
#caption {
  background: #282828;
  max-height: 50px;
  width: 100%;
  text-align: center;
  color: #ffffff;
}
<div id="container">
  <img id="image" src="http://www.fixedstars.com.au/images/runBack.jpg">
  <div id="caption">This is the caption</div>
</div>

This code ensures that the image's maximum height is equal to 100% minus the height of the caption. If you want the caption to always be at the bottom of the container, regardless of the image's size, you can set the container to position: relative and the caption to position: absolute; bottom:0;.

Answer №2

If you want to resolve this issue, you need to ensure the Image has a width and height set to 100%.

For your convenience, below is a complete demonstration.

We hope this solution proves beneficial for you.

<style>
.mydiv {
    border-color: red;
    border-style: solid;
    height: 50px;
    width: 80px;
}


.imgStyle {
    height: 100%;
    width: 100%;
}
</style>

<html>
<div class="mydiv">
<img src="http://www.boltoday.com/wp-content/uploads/2014/10/only-3d-natural-1024x768.jpg" class="imgStyle" />
Your Text Goes Here
</div>

</html>

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

retrieve the php variable value and display it in an input field using javascript

I am looking to combine the variable $variable with the input value using JavaScript $variable= file_get_contents('./env.txt'); <select id="customer_id" name="customer_id" w300 required pj-chosen" data-msg-required=&q ...

Box Pattern with Pop-Up Modal

Check out the grid of squares I've coded below: <div class='square-container'> <div class='square'></div> <div class='square'></div> <div class='square'></div& ...

Is there a way to extract all the aria-label attributes from elements that share a common class?

I am currently working on a program that tallies the number of reactions from Facebook posts, with each reaction stored in an aria label containing detailed information. I am encountering difficulty retrieving the values of these unique aria labels due to ...

How can jQuery be used to display the size and type of linked files in title attributes?

For instance: Prior to <a target="_blank" href="http://www.adobe.com/devnet/acrobat/pdfs/reader_overview.pdf"> Adobe Reader JavaScript specification </a> As the file is in PDF format, the title should read title="PDF, 93KB, opens in a new ...

What is the method to deactivate multiple links using jQuery?

Below is the HTML code: <a title="Login" data-href="/MyAccount/Access/Login" data-title="Admin" data-entity="n/a" id="loginLink" class="nav-button dialogLink"><b>Login</b></a> <a title="Register" data-href="/MyAccou ...

How should image dimensions for a background image be properly specified in CSS?

In my CSS file, I have defined styles for mini icons like this: /* Total counts - mini icons */ span.count-facebook { background: #fff url(../images/mini-icons/facebook.png) no-repeat 1px center; } span.count-plusone { background: #fff url(../images/mini- ...

Issues with Bootstrap Container on Smartphones

Currently tackling the responsive design aspect of my website, however, I've encountered a bug specifically with mobile devices. Interestingly, testing it on my desktop browser proves smooth sailing, but when attempting on my iPhone, I faced some chal ...

PHP multiline string using PHP language

I am struggling with echoing a large amount of PHP and HTML code. Despite trying the obvious solutions, I am unable to make it work: <?php echo ' <?php if ( has_post_thumbnail() ) { ?> <div class="gridly-image"><a href="< ...

"Covering the basics of HTML with this set of

Seeking a solution for incorporating common HTML elements into web pages? Look no further. In this scenario, the "Footer Section" and "Bulletin" are recurring components across all site pages. While it may seem minor, the goal is to consolidate these state ...

What are the steps to design a basic circle in CSS3 that allows for easy movement and resizing of the background image?

I attempted to achieve a specific effect with my code snippet: .mask { width: 200px; height: 200px; border-radius: 50%; -webkit-border-radius: 50%; -moz-border-radius: 50%; background: url(http://lapen ...

I recently implemented a delete function in my code that successfully removes a row, but now I am looking to also delete the corresponding data from localStorage in JavaScript

I have successfully implemented a delete function in JavaScript that deletes rows, but I also want to remove the data from local storage. This way, when a user reloads the page, the deleted row will not appear. The goal is to delete the data from local s ...

In order to properly adjust the HTML content when resizing the window, it

I'm facing an issue with a toggle setup for displaying content differently based on screen width. Specifically, when the screen size decreases below 600px, it switches to the correct content. However, upon increasing the screen width again, it fails t ...

I need a layout similar to the navbar on http://thecoloradan.com/ but I am not asking for instructions on how to do it

Have you seen the stunning navbar on this website? It appears to merge with the background upon loading, then smoothly shifts to the top of the page as you scroll. The transition is accompanied by a lovely animation. I am curious about the steps needed to ...

Obtain the texture from a user upload and apply it to a geometry in three.js

As a beginner in three.js and JavaScript, I am excited about the possibilities and have been creating small Three.js scenes to improve my skills. My ultimate goal is to build a 3D website, but I am currently stuck on how to allow users to upload an image f ...

Tips for Accessing a New Website Using the Floating Layer Close Button

How can I trigger the opening of a new browser window when a visitor clicks the close "x" on my floating layer ad? The close button is within an HTML link code ("a href"), but simply putting a URL in there does not seem to work. <script language=" ...

The modal content is not displaying within the dropdown menu

When I call the Bootstrap 5 modal from a dropdown, it only shows a grey background instead of the content. What could I be missing? You can find the code in this JSFiddle link: <div class="container"> <div class="row"> ...

Aligning elements in the middle of a div, from side to side

If you imagine a container div that is 100px wide, and inside it there are 3 smaller divs each 20px wide. I am wondering how to align these smaller divs so that they are centered within the container div, with a 20px gap on each side? ...

Each time I open my modal (Select Dynamic), my ajax query is triggered repetitively

Currently, I am developing a project in Laravel and focusing on the CRUD operations, specifically working on the Update functionality. I have a button within a modal that allows me to edit dates, including a dynamic select option. The table with the butto ...

Alter the color of the initially chosen option in the dropdown selection

I'm struggling to modify the color of the initial selection while keeping the remaining selection-options at their default black color. Unfortunately, I haven't been successful in achieving this. Currently, all colors are #ccc. What I want is fo ...

Choose a specific category and its corresponding subcategory from the drop-down

Is it feasible to implement a category in the drop-down menu using jQuery? For example: Southern Suburb suburb1 suburb2 suburb3 suburb4 Northern Suburb suburb5 suburb6 Appreciate your help! ...