What is the best way to organize multiple DIVs inside a main DIV so that they line up perfectly with no gaps in between?

I have a main DIV and 10 smaller DIVs inside it to display articles. Each smaller DIV contains 2 paragraphs. The smaller DIVs resize based on the content within them, but I'm facing an issue where there is excessive space between the displayed DIVs in the main DIV.

Credit to "Rene van der Lende" for introducing me to the term "masonry layout."

Any suggestions on how to resolve this spacing problem?

Below are images and code snippets:

// CSS for the main DIV
.sve{
    position: absolute;
    width: 1320px;
    display: flex;
    flex-wrap: wrap;
    top: 10%;
    left: 15%;
    padding: 10px;
}

// CSS for the smaller DIVs
.sve .d1{
    position: relative;
    flex: 1;    
    padding: 10px;
    background-color: var(--boja2);
    border-radius: 10px;
    margin: 10px;
    height: fit-content;
}

// CSS for styling paragraphs
.sve .d1 p{
    padding: 20px;
    text-align: center;
    color: var(--boja1);
    border-radius: 10px;
}

// Code determining width based on text amount
.pN{
    font-size: 25px;
    font-weight: bold;
    white-space: nowrap;
}

// Code determining height based on text amount
.pT{
    margin-top: -30px;
    font-size: 20px;
}

Website Preview: (Link)

Main DIV with Dev Tools:

I aim to remove the empty spaces and tightly pack the smaller DIVs together. Your understanding and input are appreciated.

Full HTML and CSS code below:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="theme.css">
    <link rel="stylesheet" href="body.css">

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <script>
   
    </script>
</head>
<body id="b">
   <!-- Rest of the HTML content -->

Answer №1

I discovered some alternative solutions to get this functioning.

/* These styles were added for the main DIV with class (.sve) */
display: column;
    columns: 3;
    gap: 1em;
    
/* Additionally, these styles were applied to the smaller DIVs with class (.sve .d1) */
margin-bottom: 1em;

It's not perfect, but it's decent enough.

If anyone has a better solution, please share it here.

Here is how the website appears after implementing these changes

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

Differences between using CSS properties and AngularJS filters:CSS properties

When working on an angularjs application, one task may be to convert a string to uppercase. There are 2 options available to achieve this, both yielding the same result. However, it is important to consider the scenarios in which one option may be preferre ...

Finding out the exact number of pixels a user will scroll when using the mouse wheel in Javascript

Is there a way to calculate the number of pixels that will be moved by the vertical scroll bar when a user scrolls using the mouse wheel in JavaScript? In my situation, the user's scrolling will result in an offsetTop change between 200px to 1000px, ...

Assistance needed in creating a MVC3 and Razor 2-level horizontal navigation menu

I am attempting to create a 2-level horizontal menu, similar to the one featured on the following site: TV.Com Despite searching on google, I am struggling to put it all together. Below is the sample code I am working with: <div id="divNav"> <u ...

Issue with empty attribute selector not functional in Edge browser

In my current version of Edge, I am facing an issue where the empty attribute selector is not functioning correctly. You can find more information about this problem here. The selector I am using is: [ct-validation]:not([ct-validation=""]) { border-col ...

"Applying a class to a div element when it becomes visible within

I am attempting to dynamically add a class to a div element when it comes into view on the webpage. I am utilizing a minified jQuery 1.11.0 script to achieve this functionality. Below is the jQuery code I have implemented: function isScrolledIntoView(ele ...

The content spilling out of a Bootstrap 4 row

UPDATE: Here is the link to the javascript fiddle When the text on the screen becomes too large for the row, it overlaps onto other rows instead of increasing the height of the row. This could be due to setting the row heights as a percentage value in ord ...

Working with CSS styles for the <datalist> element

I currently have a basic datalist drop-down menu and I am looking to customize the CSS to match the style of other fields on my website. However, as someone new to web design, I am unsure of how to go about this process. Check out the code here: http://j ...

Steps to incorporate a personalized design onto a card element within Material UI

As a JavaScript developer, I was tasked by my brother to create a simple webpage for his business. Admittedly, frontend design is not my strongest suit so I decided to utilize Material UI and added some CSS to paste everything together. Right now, I am wo ...

Scrapy and xpath not functional - solely relying on css for operation

Attempting to scrape data from this website: After retrieving all elements using the following code: products = response.xpath("//ul[@class='products']//ancestor::li") I attempted to extract prices for all elements in the scrapy shell ...

Align two spans within a div using the margin auto property

I often find myself struggling with the basics, it seems like I can't figure this out. I want the two spans to be centered within the div with auto margins... https://jsfiddle.net/5k4pnmf7/ <div class='foo'> <span class='bar& ...

Adaptable layout - transitioning from a two-column design to a single-column layout

I'm facing an issue with two divs that are floated left to appear inline <div class==".container" style="width: 100%"> <div class="leftColumn" style="width: 50%; float:left"> test </div> <div class="rightColu ...

The concept of Nested DIVs and their impact on Element Height

Trying to divide this page into blocks with checkbox options in a 4x3 layout. The issue is that setting the "columns" to 100% height makes it 100% of the parent's parent, not the parent div. Any suggestions? <div id="left" style="width:50%;height: ...

What are some ways to ensure my web application appears correctly on Internet Explorer versions 8 and 9?

When I compare how my application looks in IE versus Chrome and Firefox, it seems like there are styling issues. I want to address this problem. I attempted to use the IE developer tool but couldn't find any options to customize styles and CSS. How d ...

Tips for automatically scrolling images horizontally in responsive layouts

Recently, I've been working on a website called . On the homepage, my portfolio images are arranged in a grid format using the <li> tag. I'm looking to align them side by side and have them scroll horizontally automatically, preferably with ...

Lose the scrollbar but still let me scroll using the mouse wheel or my finger on the fullscreen menu

I am currently working on a fullscreen menu design. However, when the menu appears, some elements are not visible unless I apply the overflow-y: scroll property to the menu. But I don't like the appearance of the additional scrollbar that comes with i ...

Break up a line within an ionic element by inserting a linebreak tag

Currently, I am constructing an app using ionic framework and facing a challenge in inserting a linebreak within an ionic tag to ensure proper display inside the designated container... <h2>{{caravan.model}}</h2> ...

Switch to light mode upon clicking the toggle?

On the top left corner of this page, there's a toggle for "Light/Dark mode". Clicking on 'Light mode' smoothly transitions to dark mode without any issues. However, once it's in Dark mode and you try to switch back to 'light mode&a ...

How to properly resize absolutely positioned elements using CSS

Can anyone advise on how to properly scale down elements with absolute positioning so they maintain their proportions when the screen size is reduced, without using media queries? I have included an image below showing the current desktop view. https://i. ...

My line occupies an excessive amount of vertical space

Working on a practice website and realizing my CSS skills are a bit rusty. Struggling with the height of a row in my container, as it's taking up too much space. Also, having trouble positioning the second row below the first one without it ending up ...

Customizing the Collapse Button CSS in Datatables

I am looking to customize the CSS of the collapse button on this particular example https://datatables.net/extensions/searchpanes/examples/customisation/buttonText.html I have already conducted numerous searches and attempted various solutions. ...