CSS styling to create flexible divs

Recently, I stumbled upon the website http://www.zergnet.com/ and I'm intrigued by how they manage to align divs next to each other seamlessly despite having varying heights. I attempted to replicate this effect by generating random divs with different heights using the code below, but unfortunately, they all ended up leaving empty space above them. How can I modify this code so that each div is positioned only 5px from the top? What adjustments should be made to achieve a layout similar to that of the website?

    <?php
    for($x = 10; $x > 0; $x--){
    $rand = rand(200, 300);
    echo '<div 
           style="
           margin: 0 5px 10px 5px;
           width:200px; height:'.$rand.'px;
           background-color:red;
           width: 260px;
           float: left;">
<img src="https://www.google.com/images/srpr/logo4w.png" width="260" height="'.$rand.'"></div>';}?>

Answer №1

To achieve this design, utilize HTML and CSS.

View the DEMO http://jsfiddle.net/yeyene/wxVfj/1/

Your HTML structure should include...

HTML

<div id="container">
    <div class="item_wrapper">
        <div class="item">...</div>
        <div class="item">...</div>
    </div>
    <div class="item_wrapper">
        <div class="item">...</div>
        <div class="item">...</div>
        <div class="item">...</div>
        <div class="item">...</div>
    </div>
    <div class="item_wrapper">
        <div class="item">...</div>
        <div class="item">...</div>
    </div>
</div>

CSS

html, body {
    margin:0;
    padding:0;
    font-size:12px;
}
#container {
    float:left;
    width:630px;
    background:green;
}
#container .item_wrapper {
    float:left;
    width:200px;
    margin:0 5px;
}
#container .item {
    width:100%;
    background:#ccc;
    margin:5px 0;
}

Answer №2

Take a look at this awesome jquery plugin called masonry. It can help you achieve the layout you want and more

To create a similar layout using CSS, you can set up 3 columns and place child divs inside each parent div. Here's an example:

<div>
   <a href="#">Content goes here</a>
   <a href="#">Content goes here</a>
   <a href="#">Content goes here</a>
   ...and so on
</div>

CSS code:

div {
   -moz-column-count: 3;
   -moz-column-gap: 10px;
   -webkit-column-count: 3;
   -webkit-column-gap: 10px;
   column-count: 3;
   column-gap: 10px;
   width: 480px; 
}

div a {
   display: inline-block;
   margin-bottom: 20px;
   width: 100%; 
}

Check out this example: http://jsfiddle.net/9x3NX/

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

Tips for adding a checkbox to a form using Handlebars and SQL

I'm working on a form that allows users to edit a product: https://i.sstatic.net/4PJis.png After making edits, the updated information is uploaded by modifying the SQL table named "product". https://i.sstatic.net/erdiX.png However, I've encoun ...

Creating intricate HTML components using jQuery/Javascript

My current challenge involves receiving data from JSON and dynamically displaying it on a web page within complex HTML structures, such as tables with lists. Every time new JSON data is received, I need my HTML elements to be dynamically regenerated. For ...

What is the best way to ensure a PrimeVue TabPanel takes up the full vertical space and allows the content within the tabs to be scroll

I have created a sandbox demo to illustrate my issue. My goal is to make the content of tabs scroll when necessary but fill to the bottom if not needed. I believe using flex columns could be the solution, however, my attempts so far have been unsuccessful. ...

The JavaScript file couldn't load because of a mismatch in MIME types

I keep encountering the following error : Loading failed for the <script> with source “http://localhost:3000/task-manager/public/js/signup.js”. The resource from “http://localhost:3000/task-manager/public/js/signup.js” was blocked due to MIME ...

Show the "Splash" picture, then switch to a newly uploaded image and show it for a set amount of time

I am in the process of developing an HTML/JavaScript page that will showcase a splash image (splash.jpg) until it gets replaced by another image file called latest.jpg. Once this latest.jpg is displayed, I want it to remain on the screen for 90 seconds bef ...

The mobile navigation bar may not display correctly on certain iPhones and iPads

Currently using a custom theme on Prestashop 1.6 where minor changes have been made to the CSS file, focusing mostly on colors and fonts. Interestingly, the mobile menu functions flawlessly on Android devices that were tested. However, some Apple devices s ...

Scrolling down a jQuery div after each new item is added.OR

I have a div acting as a chat feature, where messages are appended based on user actions. I am trying to make it so that the div automatically scrolls down after each message is added, but I'm encountering some issues with this functionality. You can ...

"Creating a navigational bar using Bootstrap in a one-page application

I am facing an issue with Bootstrap's navbar in my single-page application. The dropdown navigation menu for mobile is not collapsing properly after clicking. Although I tried to fix it by adding data-toggle="collapse" data-target="#navbar" attributes ...

Positioning children within a div element

Howdy! I have a neat little setup with two divs stacked on top of each other, each containing child elements. My goal is to hide the first div and have the second div seamlessly take its place while keeping the child elements in their original position. C ...

Select interest groups with Mailchimp ahead of time

My current project involves utilizing Mailchimp to integrate a subscribe form onto my website, which includes nearly 40 interest groups as checkboxes. To showcase the form in a popup upon clicking, I have implemented jQuery Modal for this purpose. Howeve ...

Troubleshooting problem with displaying background images on iPad and iPhone devices

A recent project involved building a website for a new client, but there seems to be an issue with the background rendering on his iPad and iPhone. The image is displaying incorrectly, showing only a small portion from the top right corner rather than the ...

How can I delete a video on mobile in WordPress?

Looking for some assistance with my webshop www.spidercatcher.dk. I'm trying to have a background video display only on PCs, while showing a still photo on phones and tablets. I've tried using a poster tag but I can't seem to scale it proper ...

View the user's ID profile

Having trouble accessing user profiles? For example, as User 1, you want to view User 2's profile but when entering the URL (e.g. localhost/$userID), only information from your own profile (User 1) is displayed. Below is the HTML CODE for displaying ...

The cursor dictates the movement of the bullets with precision

I am currently working on a basic arena shooter game using JavaScript and HTML. My next step is to implement the shooting mechanic, and I planned on modifying some code from my enemy class to do so. Here's the snippet: // Calculating differences betwe ...

The width setting for the Facebook Page Plugin is not taking effect

I'm implementing the Facebook Page Plugin but facing an issue. Despite setting the width to 500, it remains stuck at 280px. <div id="facebook-feed" style="text-align: center"> <div class="fb-page zdepth-1" data-width="400" data-h ...

Navigation bar not adjusting to the size of the mobile device's screen

My website can be found at . I'm trying to make my navbar mobile-friendly and usable on all devices. However, when I tested it on my iPhone, the navbar appears extremely tiny. Please inspect the code using dev tools to see what I mean. Below is the co ...

Tips for transforming a vertical list into a horizontal one with the help of Bootstrap and media queries

I have been attempting to change this list to a horizontal layout for smaller screen sizes without success. I have tried various methods but nothing seems to be working. Even though it may be a simple task, as a beginner in Bootstrap, I am currently workin ...

Having difficulty adjusting the appearance of the navigation bar dropdown

I'm currently facing an issue with changing the background color of my nav-bar dropdown to black, as well as adjusting the width of the dropdown options to fit the screen. Despite trying the code provided below, I am unable to change the dropdown bac ...

Turning a multi-row table header into div elements using Javascript

Does anyone have a JavaScript algorithm that can analyze a table header and then output the equivalent DIV elements as either a string or object of DOM? The table header has the following structure: Here is an example of the HTML: <thead> <t ...

onclick doesn't run the function

I have been encountering an issue with my HTML code that is supposed to clear web storage data upon clicking the "Clear Storage" button. Despite using this code snippet, the function to clear the storage does not seem to be triggering in Chrome and Firefo ...