Limit the width of the div to match the width of its only child element

My goal is to incorporate image captions on my website, but I've run into an issue with compatibility. Since the figure tag isn't supported in old versions of IE such as IE8 or below, I'm considering using a <div> containing an <img> along with the caption.

The problem arises when the caption text is wider than the image itself. In this case, the width of the enclosing <div> adjusts to match the width of the caption, creating unsightly white space next to the <img>. Ideally, I'd like the caption to wrap over multiple lines instead.

I could manually set the width of the <div> to be equal to the image's width, or add <br /> tags within the caption to force line breaks. However, I'm searching for a more elegant and efficient solution to this problem.

To better illustrate the issue, you can view an example on this jsfiddle link.

Answer №1

View the Demo: http://example.com/demo/1/

CSS Styles:

.image-container {
    display:table;
    width:1px;
    float:right;
    border:1px solid #aaa;
    border-radius:5px;
    overflow:hidden;
    margin:10px;
}
.image-container img {
}
.image-container span {
   margin:10px;
}

HTML Code:

<body>
<h2>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</h2>
    <div class="wrapper">
        <div class='image-container'>
            <img src='https://www.example.com/image.jpg' alt=''><span>Description of the image</span>
        </div>Nullam ac mi non arcu mollis vestibulum eu id orci. Nam euismod gravida odio sed faucibus. Quisque nec justo ut diam imperdiet laoreet. Fusce vel lectus risus. Etiam at ligula in libero rhoncus tristique vitae a felis. Duis eget augue posuere, cursus ex sit amet, vulputate tortor. Sed et ante interdum, porta nisi eget, malesuada turpis. In non tincidunt nulla. Phasellus dictum scelerisque sapien quis tempor. Cras feugiat, velit ut fermentum sollicitudin, neque eros accumsan massa, sit amet interdum lacus purus non sem. Aenean condimentum risus vel dui ullamcorper varius. Curabitur porttitor ultrices dignissim.</div>
    </div>
</body>

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

Align anchor tag text in the center vertically after adjusting its height using CSS

I'm having trouble vertically aligning the text within an anchor tag that has a height and background color set. The goal is to have equal amounts of space above and below the text, but currently all the extra height is being added below the text. I&a ...

Launching a program through a web browser - a step-by-step guide

I am looking to create a specific sequence of events: A web browser opens, and a user logs in (the exact action is not crucial) The user is then redirected to a new page where another program should automatically open This process is similar to what happ ...

Positioning a logo in HTML/CSS so that it remains fixed at the top left corner regardless of the screen size

So, I'm facing a bit of a challenge here! I'm working on coding a website with HTML/CSS and I want the logo to remain fixed at the top left corner. The issue arises when resizing the browser window on a Windows machine – my logo ends up getting ...

Creating a popup using JavaScript in an ASP.NET page can be challenging when it comes to passing values

The list page in my parent window displays multiple rows, each representing an individual person. Next to each person's name is an icon that links to the "change status" page. When clicking on the icon, a popup page should open where the status of th ...

Incorporating specialized pseudo CSS styling to a class

Having a bit of a tricky situation here, I am dealing with the following class: <li><a href="test.html" class="hvr-overline-from-left">Test</a></li> Here is the corresponding CSS: .hvr-overline-from-left { display: inline-block ...

Animating HTML elements with JavaScript and CSS when hovering the mouse

Recently, I stumbled upon the SkyZone website and was impressed by the captivating JavaScript/CSS/HTML effects they used. I was inspired to include similar effects on my own website. (Visit the Home Page) One noteworthy feature on their website is the n ...

Vue.js error: Unable to locate requested resources

Recently, I've been encountering a curious issue when trying to access a specific component in my application. When I manually type the URL http://localhost:8080/detailed-support/start into the browser, the content loads without error. However, none o ...

Photo browser with RTL support and customized previous/next buttons using Framework7

Having an issue with the functionality of the "photo-browser-prev,photo-browser-next" button. It seems to be malfunctioning. Can you please take a look and assist? The photo browser is showing 2 out of 3 images, but the previous button is disabled. Howev ...

Add the bannercode snippet found on a webpage

I am currently facing an issue with appending a banner code to the end of a URL. The scenario is as follows: An individual receives an email with a link to a website (SITE1), where the URL includes a banner code, such as www.site1.com?banner=helloworld O ...

Utilize the entire width of the page by evenly dividing it into different components and considering the occurrence of maximum

I have a code where I inserted a component (product), and I want it to take up the full width of the container. If there is only one component, it should use the full width, and if there are 2 or more, they should evenly distribute across the whole width. ...

Unusual actions from the jQuery Steps extension

I am currently utilizing the jQuery Steps plugin (FIND IT HERE). The issue I'm facing lies within an IF statement that is causing the wizard to return to the first step instead of staying on the current indexed step. While all other IF statements are ...

JavaScript - Hover function not functioning properly

Having some difficulty with the .hover() function I am fetching dribbble shots that are displayed on page load. However, I am experiencing issues when trying to add a class via hover on these shots. Interestingly, the hover functionality works perfectly ...

What is the best way to insert my words within the boundary?

I am seeking assistance on inserting text within the border I created using an image. As I continue to add more text, I want it to display within the border as I type. Any guidance you can provide would be greatly appreciated. Thank you! li { list-sty ...

Obtain the text from an altered stylesheet in Firefox

I am currently programmatically updating stylesheets for a web page using JavaScript. My method involves having a local copy of the page and all associated assets stored on a server. After I finish making changes to the stylesheets, my goal is to save the ...

Live Server in VS Code not refreshing automatically as expected

My problem is with VS Code and Live Server for HTML CSS. I expected the page to automatically reload after editing my HTML, but that's not happening. Even though I have Auto-Save enabled, Live Server isn't updating or refreshing my page automati ...

Unable to apply CSS styles to a form input using Ajax

Alright, so I've got this Ajax form that successfully fetches the data and displays it in #register_msg. However, I'm also trying to apply some styles to the input forms. Here's my Ajax code, along with the form. But for some reason, the st ...

Transmitting information from HTML to a server using node.js

Having trouble sending data to the server? When running server.js in Node, I find that req.body.username and req.body.password are always undefined after clicking the submit button. The HTML file opens in a browser, so it seems like that is causing the iss ...

The art of applying styles through styled components

I am attempting to customize the styling of my component with the following code: export const StyledCascader = styled(Cascader)` background-color: gray; ul.ant-cascader-menu { background: red !important; } `; Despite using styled components ...

Step-by-step guide on concealing elements and subsequently displaying them upon clicking the containing DIV

It's a bit tricky to explain without visuals, so I suggest checking out the JSFiddle link provided. Essentially, when a specific div is clicked, it should expand to reveal some inputs and buttons. However, the issue I'm facing is that upon loadin ...

Unlocking the power of grails arrays within Angular

In my Grails application, I have the following controller: package tabla class TablaController { def search() { def v = Vendedor.list() [vendedor:v] }} I am sending this data to the view: <!DOCTYPE html> <html> <script src="http ...