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:

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

The default value for an input of type date should be set to the current date

I am working on a project that involves an input field with the type of "date". I have implemented Materialize to provide a user-friendly date picker. My goal is to set the default value of this input field to the current date when it is initialized. Here ...

Element Style Does Not Align with Computed Z-Index

Currently, I am in the process of developing an HTML5 video player. However, I have encountered a problem where my custom controls become unclickable once the video element is set to fullscreen mode. This issue arises because the video's z-index is se ...

Strange glitches and slow loading problems plaguing website. (GoDaddy)

TackEmporium.com Lately, I have been revamping my website, which was originally given to me by a friend. I have been making slight adjustments to update its appearance while keeping its classic look with enhanced resolution and cleaned-up HTML5 code. Rec ...

html2canvas encountered a CORS error when trying to retrieve images from an external domain

I have been attempting to export a react component as a PDF file. Here are the steps I have followed: Converting the component into an image using html2canvas Creating a PDF document Attaching the image to the PDF The component contains images with URLs ...

I am experiencing an issue where certain HTML classes are not applying the CSS code properly within my PHP file

I am facing an issue with my code where some of the HTML classes are not applying the CSS styles correctly, such as the first class "class="foot"". Below are the files that I have: * { margin: 0%; padding: 0%; ...

Designs for Creating Webpages

As a beginner in the coding world, I have little experience with HTML and Python. My goal is to create a personal webpage, starting with a template and customizing it to fit my needs. Any suggestions on where to find free templates or where to begin my s ...

"Unlocking the Power of mediaElementjs: Easy Steps to Accessing the Player Instance

I'm facing a small issue with the MediaElement.js player. To access the player instance, I usually use the following code (which works in HTML5 compatible browsers): // Retrieve player this.playerId = $('div#shotlist-player video').att ...

Display the title of an image beneath the image using CSS

Since upgrading my CMS, I've encountered an issue where the captions of images are not showing below the image for thousands of articles. For example: <img src="123.jpg" alt="Texttext" class="caption" style="disp ...

How can I retrieve an attribute from another model in Ember using the current handlebar in the HTML file?

I'm attempting to achieve the following: {{#if model.user.isAdmin}} <div> My name is {{model.user.name}} </div> {{/if}} within a handlebar that is being used in a controller unrelated to users: <script type="text/x-handlebars" data- ...

Exporting an HTML table to Excel while excluding any hidden <td> elements

I have a web application with an HTML table that displays data to users. I wanted to export the table to Excel and found a jQuery code for it. However, the exported data includes information that is hidden in the table. Is there a way to export the tabl ...

Employing AJAX to send form data to a pair of destinations

Can anyone help me out? I am having trouble using AJAX to submit my form to one location and then to another. Once it's posted to the second location, I want it to send an email with PHP to a specified recipient, but for some reason, it's not wor ...

Updating a hidden checkbox in an HTML input: A step-by-step guide

Currently, I have an HTML input page set up to input scores into a grid. status valueA valueB checkboxA checkboxB 1 4 5 2 x x 2 3 3 1 x 3 5 7 2 4 ...

CSS responsive design: concealing elements does not prevent the page from being displayed

Imagine a scenario where I need to display a template in two different versions based on the user's device. For instance, I have utilized the following code: <div class="desktop"> <body> Hi Desktop user </body> </div> ...

Disable the functionality of the next and previous buttons for the Bootstrap carousel when clicking on the outer

Here is the code for the carousel: The next/prev button functions under its respective div, how can I stop it? When I click on the div below the carousel, the carousel continues to move as usual. Below the carousel div, there is another one with a tabbi ...

What are the best ways to incorporate mistakes into my JavaScript mortgage calculator?

I am struggling to set up my calculator so that it triggers an error message if the APR goes over 25% or falls below 0. Also, the Loan Term should be greater than 0 and less than or equal to 40, otherwise display an error message. I have attempted differen ...

Tips for closing a popup without exiting the entire webpage

I'm facing an issue while trying to create a video playlist using two HTML files (index.html and video.html). In my video.html file, I have set up a popup for playing videos. However, whenever I close the popup or click anywhere on the page, it redire ...

Showcasing menu options in a double-column layout - MUI Menu

Just starting out with React. I'm using MUI menu to display items in two columns, but I've noticed a significant gap between the menu items when using grid display with repeat 2 and 1fr. display:'grid' , gridTemplateColumns : 'repe ...

Eliminating the 'white-space' surrounding concealed images

I am currently working on a project where I have a list of images that need to be hidden or shown based on the click event of specific <li> elements. While I have managed to achieve this functionality successfully, I am facing an issue with white spa ...

Ways to style a div element in CSS to achieve a unique shape

Hello there! I'm looking to achieve a tilted background div effect. Anyone have any tips or ideas on how I can do this? I'm new to web development and would appreciate the guidance. https://i.stack.imgur.com/wyj1X.png ...

Updating SVG colors using VueJS

I'm struggling to change the color of an existing static SVG image. Here's the code I have: <img class="icon-shop" src="@/assets/icon-shop.svg"/> <style> .icon-shop { width: 32px; fill: orange; stroke: oran ...