creating a div to "embrace" all its inner elements

My HTML code resembles this:

<div class="figure">
  <img src="some_image.png" />
  <div><span class="caption">Fig. 1: Some caption</span></div>
</div>

Is there a CSS trick that can make the div with class figure adjust its width dynamically based on the image and caption it contains? I want to add a border around them without manually setting the width in pixels.

Check out an example below (div.figure expands to fill available space)

div.figure { 
  border: 1px solid black; 
}
<div class="figure">
<img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo-med.png?v=6f86a5fa447f" />
<div><span class="caption">Fig. 1: Some caption</span></div>
</div>

Answer №1

SOLUTION:

A <div> behaves as a block-level element in HTML, similar to the figure tag in HTML5. To make it behave like an inline-block element, you can add the CSS property display: inline-block; to your figure.


CODE SNIPPET:

figure {
  border: 1px inset tomato;
  display: inline-block;
}
<figure>
  <img src="http://placehold.it/300x300" alt="my-photo"/>
  <figcaption>
    Fig. 1: Some caption
  </figcaption>
</figure>


FURTHER READING:

How well do you know CSS display? by Chen Hui Jing - June 18, 2016

Display by Sara Cope - March 16, 2015

Answer №2

To make the div element inline-block instead of block by default, just include display: inline-block; in the div.figure CSS class. Here's an example:

div.figure { 
  border: 1px solid black;
  display: inline-block;
}
<div class="figure">
<img src="http://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo-med.png?v=6f86a5fa447f" />
<div><span class="caption">Fig. 1: Some caption</span></div>
</div>

Answer №3

.shape {
  border: 1px solid #000;
  width: auto;
  display: inline-block;
}

It's quite clear - setting the width to auto means it adjusts automatically. The inline block feature is crucial here, restricting the shape to be only as wide as its content, allowing more elements to align next to it.

Answer №4

One straightforward approach to achieve this is by including the float: left property, which positions the div next to adjacent elements. When an element is floated, it only takes up as much width as its content (unless otherwise defined).

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

Utilize orientation detection technology to ascertain the exact placement of an image

On my HTML page, I have a centered header (h1) with a title that displays in portrait mode above an image. When switching to landscape orientation, I want the image to move to the left side of the title. I've experimented with using <br> along ...

Creating a table with adjustable row heights is a great way to enhance the user

Can the height of an HTML table row be adjusted dynamically by dragging and dropping, similar to how it's done in this demo (https://reactdatagrid.io/docs/row-resize), but for free? ...

The left margin class in Bootstrap seems to be having trouble functioning properly within a React

Navbar <div className="collapse navbar-collapse" id="navbarSupportedContent"> <ul className="navbar-nav ml-auto"> <li className=&quo ...

Creating personalized widths for bootstrap classes with SASS

I am interested in enhancing the bootstrap source code by creating additional w-XX classes. Specifically, I aim to include both w-40 and w-60. According to the documentation, modifying the $sizes variable is necessary for this customization. In my custom. ...

Adjust the dimensions and position of the notification box using Twitter Bootstrap

I am struggling to center and adjust the size to 80% of an alert box, but the text-center class is not achieving this for me. <div class="text-center"> <div class="alert alert-info" style="width: 80%;" role="alert"> <i class="fa fa- ...

Feature allowing simultaneous playback of several audio files when activated within Aframe webVR

I have been experimenting with ways to make all the sound files in my A-frame VR project autoplay when the scene loads, but also include an on-window-click function for browsers that do not support autoplay or require user interaction. Although I believe ...

Assign a new class to the child element of **this**

Can you please explain how to apply a class to the child elements of this? I am looking to give a different background color to all Compliant items. For example: <div class="tab"> <tr><td class="compliance">Compliant</td>< ...

Tips for managing error messages within inline input fields in Bootstrap 4 and CSS3

I currently have 2 input fields side by side that need to be validated using vee-validate in my Vue.js application. https://i.sstatic.net/xOZT2.png The validation error message is appearing at the end of the input-group instead of within it. https://i.s ...

The background image is overly magnified

I've been having an issue with the background image on my div looking too zoomed in and losing its clarity. Below is the HTML and styling that I have attempted: <div class="grid-x intro"> <div class="cell large-12 medium-12 small-12 ...

Creating a PHP-enabled HTML button that spans multiple lines

Currently, I am working on creating a list of buttons with h5 text. The issue I'm facing is that all the buttons have the same width, but when the text exceeds this width, the button expands. I would like the text to span multiple lines without affect ...

Can you explain the concept of a host server?

Hello, I am new to using cPanel and I recently uploaded a script to my domain directory. While trying to install the script, I was prompted to input my host server information. You can view the screenshot here: https://i.sstatic.net/rrVEX.png ...

The voting system will increase or decrease by 1 to 5 during each round

Recently, I added a voting system to my website inspired by this source. It's functioning well, but there is an issue where each vote can sometimes count for more than one. You can view the source code on the original website and test it out live here ...

Bootstrap dropdown seems to be having a stubborn streak and refusing to cooperate

I'm having trouble getting this code from the bootstrap website to work properly. The dropdown menu doesn't seem to be functioning correctly and I've tried various solutions without success. Here's the snippet of code: <head> &l ...

Issue with spacing in CSS sticky header on mobile devices

When implementing CSS position sticky for a floating header, I have noticed that on some mobile devices there is a small gap of around 1px between "element_b" and "element_c" while scrolling. #element_a{ width: 100%; position: -webkit-sticky !impo ...

optimal scaling at the center, not the edge

In this code snippet, my goal is to create an effect where the div in the middle of the visible scroll section appears at full scale (scale(1);), while the other div elements scale down towards scale(0); as they approach the edges. I have included a debug ...

A pair of HTML/PHP forms coexisting on a single page

Initially, the first form will prompt for a customer ID, which will then be extracted from a database. Following that, the second form will request the new address to be updated in the database. The goal is for this entire process to take place on a single ...

The text in the <p> tag will remain the same color

I am encountering an issue with changing the color in the <p> tags within the code snippet below. Specifically, I am trying to change the TITLE text to gray but have been unsuccessful so far. If anyone could provide assistance on this matter, it wou ...

Display or conceal a div depending on whether the user is on a mobile or desktop browser

In order to optimize the display on different devices, I organized my content into two divisions. The left division contains quantity, price, and item name, while the right division includes product names. For mobile browsers, I would like to hide the pro ...

Utilizing the power of positional relationships in Outlook's HTML emails

Encountering an issue with Outlook when viewing messages. Our website sends out emails with HTML content, including divs positioned relative to display text over images. While other email apps show everything correctly, Outlook does not. Are there a ...

Strategies for managing grid overflow situations

Check out my codepen showcasing the current issue I'm dealing with: https://codepen.io/marcdaframe/pen/qeBWNd <div> 123 abc <div class="container"> <div class="wrapper"> <div>One</div> <div>Two</div> ...