Provide rationale for elements connected to a fixed space (varied width) arrangement

I am facing a situation where I have a container with multiple elements inside it. These elements need to be justified and spaced out by a fixed amount (e.g. 20px). This means that the width of each element should adjust accordingly.

For instance, consider the following:

HTML

<div class="container">
    <div>
        <img src="...">
    </div>
    <div>
        <img src="...">
    </div>
    <div>
        <img src="...">
    </div>
</div>

CSS

div.container {
    text-align: justify;
}

div.container div {
    display: inline-block;
    margin-right: 20px;
}

div.container div img {
    width: 100%;
}

The end result should resemble this image (displaying examples with 2 and 3 elements; dynamic width but fixed space [20px]):

This layout should work seamlessly with a varying number of child elements.

Is there an elegant solution using CSS in this scenario?

EDIT: It's worth noting that the fixed space mentioned is a %-value!

Answer №1

If you have the option to use Flexbox, one way to style your flex items is by adding flex: 1 and a margin property with a fixed value. Here's an example:

CLICK HERE FOR AN EXAMPLE


div.container { 
    display: flex; 
}

div.container div {
    height: 50px; /* Just for demonstration */
    flex: 1;
    margin-left: 20px;
}

div.container :first-child { 
    margin-left: 0; 
}

It's worth noting that in this scenario, using flex: 1 is essentially the same as using flex-grow: 1;.

Answer №2

If you want to achieve this layout, you can make use of the CSS properties display: table and display: table-cell:

.box {
  display: table;
  width: 100%;
  border-spacing: 10px 0;
  border-collapse: separate;
  background: lightcoral;
}
.box div {
  display: table-cell;
}
.box img {
  display: block;
  width: 100%;
  height: auto;
}
<div class="box">
  <div><img src="//dummyimage.com/200x100/000/CCC"></div>
  <div><img src="//dummyimage.com/300x100/000/CCC"></div>
  <div><img src="//dummyimage.com/400x100/000/CCC"></div>
</div>
<hr/>
<div class="box">
  <div><img src="//dummyimage.com/200x100/000/CCC"></div>
  <div><img src="//dummyimage.com/400x100/000/CCC"></div>
</div>
<hr/>
<div class="box">
  <div><img src="//dummyimage.com/600x100/000/CCC"></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

Issues are arising with CSS .not(selector) functionality when applied to a span element

One of the challenges I'm facing is formatting a header with a span tag that contains a date and some accompanying text. The goal is to make the text bold without affecting the formatting of the date. In my CSS file, I have tried using :not(cls) to t ...

Arranging DIV elements into rows and columns within a table

After working all night to fix a problem, my goal is to create this timetable: https://i.sstatic.net/dAt1J.png Specifically, I'm struggling to find a solution for rows 5, 6, and 7 in the first column. The image is from a program that only runs in IE ...

Enhancing User Input with jQuery Appending

My goal is to develop a user-friendly To-Do List with checkboxes for tasks listed under each checklist item. The plan is to compile the entire list and then send it to the server for database storage upon submission. I envision having input boxes structu ...

Setting the borderRadius for a web view on Android Maps V3: A complete guide

In my application, I am using Android Map V3 and have encountered a bug specific to the Kit-Kat version. The chromium kit throws up an error message - nativeOnDraw failed; clearing to background color, which prevents the map from being displayed. Despite ...

Tips for fixing CORS error when working with the fetch API

I'm having trouble retrieving data from the metaweather.com API using JavaScript's fetch function. I attempted to include before the API URL, but it has not resolved the issue. I followed a video tutorial on how to fetch data, but I'm stil ...

Retrieving the data from a checkbox

I have a table that I want to update using checkboxes. Each row in the table has a checkbox, and when I check one of them and click submit, the checked row should update with a specific value in a column. Here is what I have done: <tbody> <?p ...

Ways to change the background color of a specific row and an entire table with JavaScript

In my application, I have implemented a feature where clicking on the "Add New Item" button adds rows dynamically using Javascript. Each input field in the dynamically added rows has a unique ID, and this functionality is working smoothly. When a number f ...

The orientation of the displayed images has turned vertical, breaking away from the traditional horizontal

My problem lies in trying to horizontally display the images fetched from the database. I attempted using "inline-block" but it proved ineffective. Despite looking for alternative solutions, none seemed to be a fit for my situation. $db = mysqli_connect(" ...

Exploring Scrollbar Components with CSS Selector in Selenium

Here is an xpath used to access scroll bar elements: /html/body/div/div[3]/div/div[2]/div[2]/div[1]/table/tbody/tr/td/table/tbody/tr[5]/td/div/table[4]/tbody/tr/td/table/tbody/tr/td[3]/div/div/div/div[4]/div[1]/div[3] I captured this xpath with Firepath, ...

Troubleshooting issue: Bootstrap 4 input group not properly showing invalid feedback text

I've been exploring the features of Bootstrap 4 - beta and I've noticed that when using .is-invalid with .input-group, it doesn't seem to be visible. <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.cs ...

Adjust transparency according to the information extracted from the AnalyserNode

Currently, I am exploring ways to animate text opacity based on the volume of an audio file. While browsing online, I came across this specific codepen example, which showcases a similar concept. However, as I am relatively new to JavaScript, I find it ch ...

Tips for building dynamic drop downs with JavaScript?

I'm currently working on a personal project and I've hit a roadblock when it comes to saving and utilizing the selections made from dropdown lists. For instance: <select id = "CarType" onchange = "cartype()"> <option value = "car"&g ...

Update the blue glow effect on checkbox inputs within Bootstrap 4 to create a unique visual experience

This query has been asked repeatedly, and I have exhaustively attempted numerous solutions. Here is my code snippet: <div class="btn-group" id="btn-container" data-toggle="buttons"> <label class="btn classic-design"> <input type ...

Ensure that both the div element and its contents are restricted to a maximum width of

I'm having trouble arranging the display of information next to a plant image. I want to ensure that the information stays on the right side of the image when the screen reaches a specific minimum width. The issue arises when the information includes ...

How can I redirect a Spotify API request to save data directly to my local filesystem

I am developing a program for personal use that utilizes Spotify's API. My intention is not to make this software public, but rather to access and analyze my own playlist data. However, I am facing an issue with Spotify's authentication process. ...

The use of .hidden in Tailwind-CSS does not result in the application of flex or block styles to the element

My intention is to hide the div on small screens and display it on larger screens starting from md: However, it seems like the .hidden class has a higher priority or may be interfering with some of my dependencies. <div className="hidden md:flex&qu ...

What is the trick to incorporating nested tabs within tabs?

My tabs are functional and working smoothly. To view the jsfiddle, click here - http://jsfiddle.net/K3WVG/ I am looking for a way to add nested tabs within the existing tabs. I would prefer a CSS/HTML solution, but a JavaScript/jQuery method is also acce ...

Aligning content in the footer vertically

After receiving help previously, I found my work to be extremely disorganized, making it difficult for me to follow someone else's progress. Currently, I have managed to align my content vertically, but now my footer is being cut off halfway and my so ...

What is the most effective way to create these lines using CSS?

Is there a way to create the background image shown in the link below? https://i.sstatic.net/YB4N7.png ...

obtain the specific attributes of the button that was clicked using jQuery

My goal is to preview an image before uploading it to the server, and this is the HTML code I am using: <html> <script> function readURL(input) { if (input.files && input.files[0]) {.......}} $("#selectedFile").change(functio ...