Is there a way to make the width of stacked multiple divs dynamically adjust to accommodate the longest content within them?

I am trying to adjust the width of stacked multiple divs dynamically based on the longest content, like shown in this image: https://i.sstatic.net/QhGCe.png

After doing some research, I found that using inline-block is recommended. Here is my initial attempt with Bootstrap 4:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<style>
    div.cls{
        display: inline;
        margin: 5px;
    }
</style>
<div class="container">
    <div class="row justify-content-start">
        <div class="cls">
            <b>aaaaaaaaaaaaaaa</b>
        </div>
        <div class="cls">
            111
        </div>
    </div>
    <div class="row justify-content-start">
        <div class="cls">
            <b>bbb</b>
        </div>
        <div class="cls">
            222
        </div>
    </div>
    <div class="row justify-content-start">
        <div class="cls">
            <b>ccccc</b>
        </div>
        <div class="cls">
            33333333333
        </div>
    </div>
    <div class="row justify-content-start">
        <div class="cls">
            <b>dddddddddd</b>
        </div>
        <div class="cls">
           4 
        </div>
    </div>
</div>

Unfortunately, this approach did not work as expected:

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

So, I decided to change my strategy. Why not utilize a flex-grid layout with rows to stack two divs: abcs and 123s?

    <div class="row justify-content-start">
        <div>
            <div>
                <b>aaaaaaaaaaaaaaa</b>
            </div>
            <div>
                <b>bbb</b>
            </div>
        </div>
        <div>
            <div>
                111
            </div>
            <div>
                222
            </div>
        </div>
    </div>

This new implementation worked successfully. https://i.sstatic.net/Qut7R.png

However, I find the current method to be quite lengthy and messy. Is there a more efficient way to achieve the same outcome?

Your assistance would be greatly appreciated. :-)

Answer №1

To achieve the desired layout, you should utilize a table structure:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" >
<div class="d-table">
    <div class="d-table-row ">
        <div class="cls d-table-cell">
            <b>aaaaaaaaaaaaaaa</b>
        </div>
        <div class="cls d-table-cell">
            111
        </div>
    </div>
    <div class="d-table-row">
        <div class="cl d-table-cells">
            <b>bbb</b>
        </div>
        <div class="cls d-table-cell">
            222
        </div>
    </div>
    <div class="d-table-row">
        <div class="cls d-table-cell">
            <b>ccccc</b>
        </div>
        <div class="cls d-table-cell">
            33333333333
        </div>
    </div>
    <div class="d-table-row">
        <div class="cls d-table-cell">
            <b>dddddddddd</b>
        </div>
        <div class="cls d-table-cell">
           4 
        </div>
    </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

Admin-on-rest sidebar navigation menu

Hello everyone! I am new to ReactJS and admin-on-rest. I am currently studying from this documentation and I am interested in creating a navigation submenu similar to the one shown here. I have tried searching on Google but haven't found what I need. ...

Developing a custom edit template with Infragistics through coding

Currently, our team utilizes the Infragistics grid without binding datasets until runtime. Instead, we set up the grid settings in code as per the preference of our senior developer. While effective, this method can seem a bit lengthy. I am interested in ...

React: Despite my efforts to return a value from render, nothing was actually returned

My current project involves creating nested components to display the dataset I have. export const MyComponent = (props) => { const groupMilestoneByYear = (data) => { // Take Milestone Data array as input and group it by Year let yearGroup ...

jQuery AJAX chained together using promises

In my current project, I am facing an issue where I have 4 get requests being fired simultaneously. Due to using fade effects and the asynchronous nature of these requests, there are times when empty data is received intermittently. To address this issue, ...

Exploring the world with Select2 world_countries search feature

I successfully integrated the world countries list from https://github.com/stefangabos/world_countries into select2 to display all countries. Does anyone have any insights on how to enable searching within the listed countries? var countriesList = &apo ...

Error: The property "indexOf" cannot be accessed because n is not defined

After rendering a page and retrieving data from Firebase upon clicking a button, I encounter an error upon refreshing the page: Unhandled Runtime Error TypeError: can't access property "indexOf", n is undefined Source pages\[id].js (1 ...

Retaining original input value even when validation is applied with input type="number"

Encountering an unusual scenario while using angularJs and input type="number". It seems that if the initial value of an input falls outside the specified min and max range, the value gets wiped out. To better visualize the issue, I created a fiddle - htt ...

AngularJS Custom Navigation based on User Roles

I am currently developing a small web application using angular. My goal is to implement role-based navigation in the app. However, I am facing an issue where the isAdmin function does not seem to be getting called on page load, resulting in only the foo a ...

Having trouble implementing render props for a toggle button that reveals/hides child components

My goal is to display or hide a couple of child components based on a toggle click using the original HTML structure provided. However, I am encountering an issue where the EditToggle link needs to be displayed next to the h2 element inside the "subtitle-c ...

Difficulty rendering wireframe with Three.js MeshBasicMaterial

I am experiencing an issue with my geometry created using the three.js API. When I export an obj file from Blender and import it, the object renders faces instead of wireframe as desired. Could this problem be due to a mistake in my import or export proces ...

Overlap of the X-axis domain line with the stroke of a d3 bar graph

When a bar is selected in my d3 bar chart, it should have a white border. Currently, the border is achieved using stroke, but the bottom border gets hidden under the x-axis line. // establish container size var margin = {top: 10, right: 10, bottom: 30, le ...

Using TestCafe selectors to target a classname that has multiple matching nodes

I need help finding a TestCafe selector that can target an element with the same class name that appears multiple times in my HTML code. Here's what I have tried so far: https://i.sstatic.net/ZS691.png Despite trying various selectors, I have been u ...

Prevent child element from being rotated along with parent element

I am facing an issue with two div elements: a parent element that is rotated and a child element that I want to remain unaffected by the rotation of its parent. To tackle this problem, I tried rotating the child element in the opposite direction of the pa ...

Pattern matching using regular expressions can detect any number from 1 to 9 except for 2

Looking for a regular expression pattern that can match any number from 1 to 9, excluding the number 2? Here's my attempt: ([1-9][^2]) Unfortunately, this pattern doesn't seem to be working for me. ...

Confirming the accuracy of multiple fields in a form

Looking for help with validating both password and email fields on a registration form. I've successfully implemented validation for passwords, but need assistance adding validation for the email section as well. Can both be validated on the same form ...

Failure to trigger Ajax callback function

I am currently working on a form page that will be submitted using Ajax. The steps I have planned are: 1. Use Ajax to check if the email already exists 2. Verify if the passwords match 3. Switch to another "screen" if they do match 4. Final ...

How can I retrieve my array state in a different router page using VUEJS and VUEX?

I have created a page with two routes - one for the home page and another for the configuration where users can decide what content should be displayed on that container. In the configuration panel, I was able to retrieve input values and stored them in my ...

What could be causing the texture distortion in THREE.js?

I am encountering an issue with the textures loaded using the TextureLoader, as they seem to be causing tearing errors within the texture. Below is the code snippet I am using for the material: var textureName = "Melamine-wood-001"; var textureUrl = ...

Unable to get autoplay to function for HTML video despite muting the audio and following various browser guidelines

After meticulously following these instructions on incorporating a background video banner into my rails project, I encountered an issue with autoplay. Despite setting up the video to display correctly on my homepage, the autoplay feature doesn't seem ...

In Angular 4, the Bootstrap modal now only opens after a double click instead of opening on the first click

Working on an eCommerce application, there is a cart icon that triggers a modal screen displaying user-selected product data when clicked. However, the issue I'm facing is that upon initial page load, the modal screen opens only after double-clicking; ...