Achieving full white space utilization with inline block elements

When attempting to stack elements on top of each other, I encounter unwanted white space due to varying heights of the elements.

I am trying to figure out how to move boxes 6 and 7 up while keeping all corresponding elements beneath them aligned properly.

.blockParent {
    text-align: center;
    position: relative;
    width: 90%;
    left: 5%;
}

.block {
    position: relative;
    display: inline-block;
    width: 500px;
    background-color: red;
    height: 200px;
    margin: 5px;
}

.smaller {
    height: 100px;
    vertical-align: top;
}

<div class="blockParent">
    <div class="block">
    </div>
    <div class="block">
    </div>
    <div class="block smaller">
    </div>
    <div class="block smaller">
    </div>
    <div class="block">
    </div>
    <div class="block">
    </div>
    <div class="block ">
    </div>
    <div class="block">
    </div>
    <div class="block">
    </div>

</div>

This arrangement gives the following result: https://i.sstatic.net/lux0y.png

I believe that using flexbox could potentially solve this issue, although I lack experience with it. Any suggestions? Thank you in advance!

Answer №1

Flexbox was not intended to address this particular issue. Instead, you can utilize CSS multi-columns. Keep in mind that the display order will shift from horizontal to vertical.

.blockParent {
  column-count: 3;
}

.block {
  display: inline-block;
  vertical-align: top;
  background-color: silver;
  width: 100%;
  height: 200px;
  margin: 5px;
}

.smaller {
  height: 100px;
}
<div class="blockParent">
  <div class="block"></div>
  <div class="block"></div>
  <div class="block smaller"></div>
  <div class="block smaller"></div>
  <div class="block"></div>
  <div class="block"></div>
  <div class="block "></div>
  <div class="block"></div>
  <div class="block"></div>
</div>

Answer №2

.blockParent {
    text-align: center;
    position: relative;
    width: 90%;
      /* left: 5%; */
    margin: 0 auto;
}
.block {
    position: relative;
    display: inline-flex;
    width: 500px;
    background-color: red;
    height: 200px;
    margin: 5px;
}
<div class="blockParent">
    <div class="block">
    </div>
    <div class="block">
    </div>
    <div class="block smaller">
    </div>
    <div class="block smaller">
    </div>
    <div class="block">
    </div>
    <div class="block">
    </div>
    <div class="block ">
    </div>
    <div class="block">
    </div>
    <div class="block">
    </div>
</div>

https://s.codepen.io/fasiya656/debug/NOrNQN/XxkVwaPeaooM

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

What is the best way to resize an SVG to perfectly fit the parent div container?

I need to display multiple SVGs from various sources, each with different height, width, and viewbox. I want them all to render within a fixed height and width div, filling up the available space. How can I scale these SVGs to fit the container div? For i ...

Learning to extract information from elements within components in a flexbox using Angular

Is there a way to access the element width of child components within a parent component that utilizes flex box? I am hoping to determine if the list is overflowed so I can adjust the visibility of elements accordingly. If an overflow occurs, I would like ...

What is the best method for incorporating meta data, titles, and other information at dynamic pages using Node.js and Express?

I am currently exploring methods to efficiently pass data to the html head of my project. I require a custom title, meta description, and other elements for each page within my website. Upon initial thought, one method that comes to mind is passing the da ...

Chrome is the only browser displaying background images

My current issue is that the background image only appears on Chrome and not on Firefox or Edge. I am trying to have a different background image each time the page loads. $(document).ready(function() { var totalBGs = 5; var rnd = Math.floor(Math.ran ...

`The challenge of a single web page not displaying correctly`

I was working on a website for a local computer building company, and I decided to make it a single-page applet. The HTML is mostly done, and I don't have much trouble with CSS. However, I encountered an issue with hiding and displaying different sect ...

Modify a CSS style with JavaScript or jQuery

Can CSS rules be overwritten using jQuery or JavaScript? Imagine a table with rows categorized by different classes - "names" for persons and "company" for companies. How can we efficiently hide or show specific rows based on these classes? If we have a ...

The JavaScript code is failing to retrieve the longitude and latitude of the location on a mobile browser

I am having an issue with my Javascript code not properly retrieving the longitude and latitude from the mobile Chrome browser. While this code works fine on laptop or desktop browsers, it seems to be failing on mobile devices: <script> if (nav ...

How can we align images above one another in a responsive manner using Bootstrap 3?

Looking for assistance with aligning and overlapping images on top of another image in Bootstrap while maintaining responsiveness. I've attempted the following: HTML: <div class="container"> <div class="row"> <div class="col-lg-12"> ...

Elements displaying outside of block 'a' tag

I have a set of <a> elements that are styled with display:block. These contain various nested <divs>, for example: <a href="#" class="sliderframe"> <div class="container-caption"> <div class="slider ...

What is preventing the JQuery dialog from opening?

When I try to trigger a dialog box by pressing the enter key, it is not working as expected. Instead of opening the dialog, it just hides the text. Can someone help me understand what might be causing this issue? <!doctype html> <html lang="en" ...

The background color in CSS frames the text with a unique hue

Is there a way to wrap my multi-line text inside a div with a background color that automatically adjusts its size based on the text length? .multiline{ padding:0px; white-space: pre-wrap; height: 100px; width: ; margein:0px } <div style="b ...

Only conceal the table column if space is limited

A scenario involves an HTML table with two columns that need to be displayed fully, with the second column aligned to the right. Currently, this has been achieved by setting the width of the first column to 100%. However, a challenge arises where the words ...

Deselect an HTML checkbox using a corresponding label's "for" attribute

Below is the html code I am using to display a checkbox with an image: <div class="testClass"> <input id="111" name="compare_checkbox" type="checkbox" /> <label for="111"> <i class="icon" aria-hidden="true"&g ...

The HR element seems to be fixed in place and is not moving

I'm having trouble figuring out what's causing the issue, so I wasn't sure how to title it. Any help in looking at the code would be greatly appreciated. Here is the problem: The HR tag should be positioned below the last image and not cut ...

Is it possible to use just one <map> tag for multiple images in jQuery or JavaScript?

I have multiple images on my website <img usemap="#slideMap_branches" src="branches.png" width="890" height="270" alt="Slide 4"> <img usemap="#slideMap_order" src="order.png" width="890" height="270" alt="Slide 2"> <img usemap="#slideMap_c ...

Arrange text and a button side by side in a table cell using an Angular directive and an HTML template

I have successfully implemented an Angular search function on my project. You can find it here to allow users to search for courses. The search function uses a read-more directive that displays a preview of the description and keywords before allowing use ...

When the text input is selected, automatically scroll to the bottom

I found a codepen example by Chris Coyer and decided to fork it. My goal is to make the label move to the top instead of staying at the bottom, as in his original design. However, I am facing an issue with getting the cursor to move to the bottom when typ ...

Transform Table header orientation to vertical

I am struggling with a table structure similar to this <table> <thead> <tr> <th>Numbers</th> <th>Alphabet</th> </tr> </thead> <tbody> & ...

Fetching the entire webpage

When attempting to load a specific webpage using an iframe, I encountered the following issue: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title></ ...

What is the best way to save a jQuery or JavaScript variable into a JSON file?

Is there a way to save a jquery variable in a json file? I have the following: var image='/test/test.png'; I am obtaining this path through file upload: <input type="file" name="imageurl" value="imagefile"></input> Therefore, I ne ...