A CSS-based puzzle game inspired by the classic game of Tetris

CLICK HERE TO ACCESS PLAYGROUND


CHALLENGE

To create a two-column div layout without any spaces using CSS is the challenge at hand.

STARTING LAYOUT

Each box is represented as:

<div class=A1">A1</div>

https://i.sstatic.net/FlZ8k.jpg

DESIRED LAYOUT

https://i.sstatic.net/5XiFF.jpg


Seems simple, right?

However, there are specific conditions to follow:

  1. The heights of each div are variable.
  2. The number of divs is variable.
  3. The height of the left and right column should be as equal as possible, leaving minimal trailing white space.
  4. Modifying the HTML structure is allowed.
  5. The divs do not need to be displayed in order.

While JavaScript solutions are accepted, the true genius will be able to achieve this using pure CSS.

NOTE: The divs are generated using a .NET repeater, but this should not affect the solution.


UPDATE

Utilizing the flex model mentioned by Paran0a, progress has been made by calculating the height of .Wrap with a script. However, accurately determining half the width is challenging due to the possibility of a large last box.

var h = 0;
$('.Wrap > div').each(function() {
    $(this).height(Math.random()*100);        
    h += $(this).height();        
});
$('.Wrap').height((h/2));

VIEW UPDATED DEMO

Answer №1

Would you consider implementing flex-box into your project?

Check out this quick demo.

http://jsfiddle.net/oLzw742p/3/

$(function(){
var test = [],
    num = 1,
   randomNo = Math.floor(Math.random() * 8) + 2;
for (i = 1; i <= randomNo; i++) {
    test[i] = $('.Wrap').append('<div class="A'+(i)+'">A'+(i)+'</div>');
};
$('.Wrap > div').each(function() {
    $(this).height(Math.random()*100);
});
});
.Wrap {
  display: flex;
  width: 500px;
  height: 400px;
  background: #E0E0E0;
  flex-direction: column;
  flex-flow: column wrap;
}

.Wrap > div {
  font-family: sans-serif;
  font-size: 20px;
  width: 200px;
  box-sizing: border-box;
  background: orange;
  padding: 10px;
  box-sizing: border-box;
  margin: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.Wrap div:nth-child(5n+1) {background: blue;}
.Wrap div:nth-child(5n+2) {background: red;}
.Wrap div:nth-child(5n+3) {background: green;}
.Wrap div:nth-child(5n+4) {background: purple;}
.Wrap div:nth-child(5n+5) {background: black;}
<div class="Wrap"></div>

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

Is there a way to simulate a click event within a Jasmine unit test specifically for an Angular Directive?

During the implementation of my directive's link function: $document.on('click.sortColumnList', function () { viewToggleController.closeSortColumnList(); scope.$apply(); }); While creating my unit test using Jasmine: describe(&apo ...

Having difficulty aligning ListItem to the right within a List

I am working with an array that contains objects which I need to display in ListItems of a List. My goal is to show these ListItems from the array Objects in a layout where odd numbers are on the left and even numbers are on the right. However, I am facing ...

What causes the cleanup function in React hooks to be triggered upon reopening a previously closed tab?

There seems to be an issue with closing a tab and then undoing that action. This causes all the cleanup functions in the component to execute, resulting in the abortion of new fetches needed to load the component. This behavior is observed only on Safari ...

Introduction to Grid Layout Using Bootstrap - Rows

Encountering an issue with the vertical alignment of items in the code snippet below when using Bootstrap 4. Initially, the items are stacked vertically, but they align horizontally when either the "row" or "row align-items-start" classes are applied. &l ...

Managing data overload in Node.js can lead to Promise Rejection Warnings

I'm currently developing a feature where scanning a barcode adds the product information to a table. Once the data is in the table, there is a button to save it. Each row requires generating a unique stamp and inserting into tables named bo, bo2, and ...

Executing JavaScript code on ASP.NET page load

Inside my HTML code, there is a radio box styled using ASP.NET RadioButtonList with specific attributes. The second list item is set to be selected by default, however, the problem arises when the page loads as the function dis() is not being called. I wan ...

Identifying text within a paragraph using JavaScript regex, excluding any URLs mentioned

How can I use JavaScript on the client side to find a search term in a paragraph while excluding any matches that are part of a URL? I attempted to use the following regex but encountered an error: "A quantifier inside a lookbehind makes it non-fixed widt ...

What method can be used to dynamically resize two side-by-side iframes in proportion to a specific ratio?

Hey, I've got this code snippet: So there are two iframes positioned side by side with a specific ratio as indicated in the link provided. My aim is to maintain that ratio regardless of the screen size where it's viewed. The first iframe is named ...

Encountered a problem initializing Rangy.js on Internet Explorer

Currently, I am developing an angular application that utilizes textAngular along with rangy-core and rangy-selectionsaverestore. However, I am encountering some errors specifically on the latest version of Internet Explorer: Module 'WrappedSelection ...

What is the best way to retrieve information from a json file using axios in a React project?

I'm having trouble retrieving JSON data. { ID string `json:"id"` Amount int `json:"amount"` Month string `json:"month"` PayFailed bool `json:"pay_failed"` } I’ve written the foll ...

Exploring the process of iterating through a JSON post response and displaying its contents within image divs

After receiving a JSON post response, I am looking to iterate over the data and display it in image divs. Can anyone provide guidance on how to achieve this using JavaScript? Thank you. Here is the JavaScript code that handles the JSON post response: cor ...

The alignment of the elements within the jumbotron is off

Hey there! I'm currently struggling to align the jumbotron with my calendar icon. Unfortunately, the elements of the jumbotron are not lining up properly. Could someone lend a hand in guiding me through this issue? Any ideas would be greatly appreciat ...

How can you prevent HTML from interpreting the characters '<' and '>'?

I am adding some content through JavaScript using innerHTML in a label, but nothing is showing up. I am retrieving data from an API response. { "answer_a": "<footer>", "answer_b": "<section>", ...

Issue with karma-ng-html2js-preprocessor failing to generate modules

Struggling to configure the karma-ng-html2js-preprocessor. While Karma has been successfully detecting all my JavaScript files, it's having trouble generating a module from the HTML preprocessor. Take a look at my options object below. I've spec ...

Dealing with interruptions in the sequence of results from a database request

Greetings! I am currently utilizing a customized base on Wordpress to organize and manage dog show results. The current setup displays the resulting array in a monolithic manner, which can be seen in the image here: https://i.sstatic.net/ka1SZ.png Howeve ...

The parameter 'string | JwtPayload' cannot be assigned to the parameter 'string'

Utilizing Typescript alongside Express and JWT for Bearer Authorization presents a specific challenge. In this situation, I am developing the authorize middleware with JWT as specified and attempting to extricate the current user from the JWT token. Sampl ...

Utilizing the power of JavaScript in an HTML file to develop a straightforward price calculator

I am new to the world of HTML coding and currently working on an assignment for my online computer course. I reached out to my professor for help, but unfortunately, he hasn't been very responsive. The task at hand is to create a basic cost calculator ...

Issue with vertical navigation bar

Currently working on creating a vertical navigation bar without any styling. It's challenging to figure out how to align the items properly side by side. You can check out the basic functionality at . When clicking on 'Android', I want the g ...

What is the reason flatMap does not delete empty arrays?

I'm encountering an issue with my flatMap function that is supposed to fetch and return data for each item in an array. It should also remove any empty arrays, but for some reason, the empty arrays are still present in the final array. Here is the fu ...

Issue with Font Requests in Browsers when Using Angular 10 and SCSS with @font-face

I have a project built with Angular 10 that utilizes SCSS for styling. In my _typography.scss file, I have defined some @font-face rules pointing to the font files located in the assets/fonts directory. However, when I run the application, the browser does ...