Different floating divisions that are tightly packed adjacent to each other

I'm dealing with dynamically created divs that have variable content, all following the same CSS rules. My goal is to organize them into 2 columns without any space in between. I attempted to use float: left/right, but there still remains space at the top and bottom.

Below is a snippet of the code (also available on JSFiddle):

.posts{
  width: 100%;
}

.one{
  background-color: red;
  height: 100px;
  width: 40%;
  float: left;
}

.two{
  background-color: blue;
  height: 120px;
  width: 40%;
  float: left;  
}
<div class="posts">
  <div class="one">
  </div>
  <div class="two">
  </div>
  <div class= "two">
  </div>
  <div class ="one">
  </div>
</div>

While the right column's div boxes turn out correctly, they create a gap between the top left box and the bottom div. I've explored various examples, but many suggest tweaking the divs individually using overflow: hidden and other techniques.

What would be the most effective way to achieve this while keeping all the divs consistent with the same CSS?

Answer №1

Important Note: Please be aware that if the browsers you are targeting do not have support for CSS columns, this solution may not work as expected:

If you're looking to achieve a layout similar to Masonry, follow these steps:


This code snippet should help you achieve the desired layout:

.container {
            column-count: 2;
       -moz-column-count: 2;
    -webkit-column-count: 2;
            column-gap: 0;
       -moz-column-gap: 0;
    -webkit-column-gap: 0;
    width: 80%;
    font-size: 0;
}

.container div {
    display: inline-block;
    width: 100%;
    margin: 0;
    font-size: 12px;
    color: white;
}

.container .one {
    height: 100px;
    background-color: red;
}

.container .two {
    height: 120px;
    background-color: blue;
}
<div class="container">
    <div class="one">one</div>
    <div class="two">two</div>
    <div class="two">two</div>
    <div class="one">one</div>
</div>

Answer №2

Have you achieved your desired goal with this code?

.posts{
    width: 100%;
}
.one,
.two {
    height: 100px;
    width: 40%;
    float: left;
}
.one{
    background-color: red;
}
.two{
    background-color: blue;   
}
<div class="posts">
    <div class="one"></div>
    <div class="two"></div>
    <div class="two"></div>
    <div class="one"></div>
</div>

Answer №3

Follow this coding structure:

<!DOCTYPE html>
<html>
<head>
<style>
.posts{
    width: 50%;
     float:left;
}
.one{
    background-color: red;
    height: 100px;
    width:100%;
}
.two{
    background-color: blue;
    height: 120px;
    width: 100%;
}
</style>
</head>
<body>

<div class="posts">
    <div class="one"></div>
    <div class="two"></div>
</div>
<div class="posts">
    <div class="two"></div>
    <div class="one"></div>
</div>


</body>
</html>

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

Implementing custom styles in JavaScript according to the specific browser or platform

When it comes to adding multiple css styles to a dom element and ensuring compatibility across different browsers, which approach is more optimal for performance? Combining all prefixed css properties together, allowing the browser to decide which one ...

Tips for verifying jquery datePicker dates?

Here are two input fields, startDate and tilldate, which utilize the jQuery datepicker plugin. The issue at hand is that when a user selects a date in the startDate field, only the next two days should be available for selection in the tillDate field. Fo ...

set ajax url dynamically according to the selected option value

My form features a select box with three distinct choices <select id="tool" name="tool"> <option value="option1">Option1</option> <option value="option2">Option2</option> <option value="option3">Option3</ ...

"Is it possible to conditionally render a component depending on the state in

One of the challenges I'm facing is customizing a filter icon from MaterialUI. My goal is to change its color to black when a checkmark is checked, and adjust its position accordingly. Additionally, when a box is checked, it should fill in setFilters. ...

Create HTML content to be inserted into an Excel spreadsheet

I am looking to generate a worksheet and insert HTML content into it using a loop. foreach(...) { Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Sheets.Add( Missing.Value, Missing.Value, 1, Excel.XlSheetType.x ...

Disappearance of the second level in a CSS dropdown menu

I am currently working on creating a dropdown menu using only CSS. Check out this example I'm using: http://jsfiddle.net/DEK8q/8/ My next step is to center the menu, so I added position-relative like this: #nav-container { position: rela ...

Modify the appearance of the button

Currently, I have a setup where I have three buttons and three panels. When I click on Button 1, Panel 1 loads while Panel 2 and Panel 3 become invisible. Similarly, when I click on Button 2, Panel 2 loads while Panel 1 and Panel 3 are hidden. I would li ...

Load a distinct JSP file from the sidebar in JSP

I have an index.jsp file containing a sidebar: <div id="sidebar"> <li ><a href="create">Create</a></li> <li ><a href="view">View</a></li> <li ><a href="Delete">Delete</a></li> & ...

Tips for refreshing a specific div element at set intervals using JQuery AJAX?

I need to make updates to a specific div element without refreshing the entire HTML page. The code below currently works, but it reloads the entire HTML page. Additionally, I am working with different layouts where I have separate files for the header, l ...

The children elements in the Flexbox div are not lined up inline with the text

Review the snippet below: div { display: flex; flex: 0 0 45%; max-width: 45%; } <div>Lorem ipsum dolor sit amet, <strong>dolor sit</strong>tempor incididunt ut la.tempor incididunt ut la.tempor incididunt ut la.tempor incididunt ut ...

Enhancing the appearance of jQuery datatables using Material Design Lite theme

I came across a helpful codepen demo demonstrating how to create a responsive data table with search filter and pagination. However, this demo is only integrated with Bootstrap. After searching the MDL discussion forum, I couldn't find any existing im ...

Within an HTML document, select one checkbox out of two checkboxes without utilizing JQuery

Is there a way to select only one checkbox out of two? I know it can be done easily using Jquery, but is there a default option in HTML for this as well? I have exactly 2 checkboxes. Thank you. code: <input type="checkbox" value="1" name="foo" /> ...

jQuery UI dynamically adjusting content layout based on browser size changes

Having an issue with my custom accordion script where resizing the browser causes formatting problems in one of the sections. I want the content to remain intact and to utilize a scrollbar if necessary to view the content. The problem is visible in section ...

Express not receiving data from HTML form submission

Check out my HTML form below: <form method="post" id="registration-form" action="/register"> <div class="form-group"> <label for="UsernameRegistration">Username:</label> <input type="text" class="form- ...

How to style tables in CSS with specific border spacing inside cells

I've been struggling with this issue for months now and even Google hasn't been able to provide a solution. My problem is that I want to add spacing between <td> and <th> tags in a table, but whenever I do, the spacing appears on the ...

Guide to personalizing checkbox design using react-bootstrap

I am working with react-bootstrap and attempting to style a custom checkbox, as it appears possible but is not working. I have followed the documentation provided here. Here is the code snippet: import * as React from "react"; import { t as typy } from & ...

Is it acceptable to nest an HTML document within the <body> tag of another HTML document?

Is it acceptable to nest an HTML document within the body tag of another HTML document? I am considering this approach in order to execute a JavaScript function on body load. Due to the dynamic generation of the main HTML code by a controller (Yii), I ...

Tips for resolving a 403 error while utilizing selenium with Python

What is the solution to fixing a 403 error? I have noticed that when trying to access a certain website, I am able to browse for about 30 seconds before encountering a 403 error. This issue only started occurring today, and I find it strange because if m ...

Can an image map area be identified using radial coordinates?

While I have experience with online Imagemap Generators for rectangular or circular areas in HTML pages, things get a bit tricky when dealing with pie-shaped sections. When trying to make each pie slice a separate area, the image map code generated by thes ...

Display a pop-up window after a set amount of time and automatically close it when clicking on an

I am looking to implement a functionality where my popup box opens automatically after a specific time interval (e.g. 5000 milliseconds) and can be closed by clicking on an anchor tag placed inside the popup box. function openPopup() { $('.popup- ...