Creating a design with CSS that allows floating boxes to completely occupy the wrapper element

This situation is really getting to me.

I am currently in the process of creating a page that will feature a fixed header and footer, with dynamic boxes sandwiched in between. These boxes need to be properly arranged to fill the space within the wrapper container.

For reference, here is an example: http://jsfiddle.net/rdMKQ/1/

HTML:

<div id="wrapper" >
    <div class="box" style="width:60px;height:60px;background-color:blue"> 1 </div>
    <div class="box" style="width:40px;height:30px;background-color:red"> 2 </div>
    <div class="box" style="width:80px;height:60px;background-color:yellow"> 3 </div>
    <div class="box" style="width:40px;height:30px;background-color:green"> 4 </div>
</div>

Non-Inline CSS:

#wrapper{width:180px;}
.box{float:left}

All the boxes float:left and the dimensions used are just placeholders for demonstration purposes

How can I position the green box (4) directly below the red one (2) to fill the gap?

Solution Requirements:

  • I cannot apply specific styles to individual boxes, the styles should be uniform across all boxes
  • The boxes cannot overlap

PS: I have attempted using the jQuery Masonry Plugin to automatically handle the spacing in JavaScript, but unfortunately, it does not offer the needed functionality.

Your assistance on this matter is greatly appreciated. Thank you in advance.

Answer №1

Yes, it is possible, but your current HTML structure will need to be adjusted.

Take a look at this example:

<div id="wrapper" >    
    <div class="box" style="width:60px;height:60px;background-color:blue"> 1 </div>
    <div class="box" style="width:40px;height:30px;background-color:red"> 2 </div>
    <div class="box" style="width:40px;height:30px;background-color:green"> 4 </div>
</div>
<div class="box" style="width:80px;height:60px;background-color:yellow;"> 3 </div>

Also, the CSS should include:

#wrapper{width:100px;height:60px;float:left;}
.box{float:left}

Answer №2

Would you be interested in a layout similar to this example? One issue is that the green box overlaps the others.

<div id="wrapper" >
    <div class="box" style="width:60px;height:60px;background-color:blue"> 1 </div>
    <div class="box" style="width:40px;height:30px;background-color:red"> 2 </div>
    <div class="box" style="width:80px;height:60px;background-color:yellow"> 3 </div>
    <div class="box green" style="height:30px;background-color:green"> 4 </div>    
</div>

Additionally,

#wrapper{width:180px;}
.box{float:left; z-index:5;}
.green{position:relative; clear:both; width:100%; margin-top:-30px; z-index:3;}

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

I attempted to utilize the <map> tag in creating a circular image that functions as a hyperlink, but unfortunately, it is not functioning correctly

I've been trying to create a round button using the code below, but something seems to be wrong with it. The issue I'm facing is that the area around the image is also clickable, even though I have used border-radius. Can someone please take a lo ...

Take out the username field from the registration form in Woocommerce

Is there a way to eliminate the username section from the registration form on woocommerce? I attempted to remove certain lines from the woocommerce/myaccount/login-form.php <?php if ('no' === get_option( 'woocommerce_registration_gene ...

The Angular Material Theme is not being properly displayed on the mtx-datetimepicker component

Issue: While using directives from ng-matero for DateTime with Angular Material for application design, the dateTimepicker element is not applying the angular material theme as desired. https://i.sstatic.net/zPBp3.png Code Example The app.module.ts file i ...

Seeking Answers About jQuery UI Themeroller

After designing a unique theme using http://jqueryui.com/themeroller/ and saving it, I have the theme downloaded. What is the next step to begin implementing the CSS for the theme? ...

Oops! There seems to be a problem with your AngularJS/JavaScript code. The error message is "Uncaught ReferenceError:

As I venture into the world of AngularJS and JavaScript, I am working on creating a 'blog' using these technologies. Here is my HTML view code- <div ng-controller="Controller1"> <form ng-submit="sub()" role="form"> Title: <textar ...

Ways to organize divs in a layout resembling a partial table

While working on my e-commerce platform with vue.js, I noticed that my product listings are not aligning properly, as displayed in the image linked below. https://i.sstatic.net/2FwaA.png My goal is to achieve the desired layout illustrated in the image b ...

Issue with displaying background image in mobile device despite successful display in browser and Chrome tools on Angular 18

Revamping my personal website (www.coltstreet.com) led me to explore media queries for displaying different background images. Testing on desktop and via Chrome tools showed positive results, but the real challenge arose when viewing the site on my actual ...

Is there a way to adjust the contents of an iframe to match the dimensions of the iframe itself?

I am trying to adjust the width of an iframe to 60%, regardless of its height. Is there a way to "zoom in" on the contents of the iframe to fit the width, so that I can then set the height based on this zoom level? I haven't been able to find any solu ...

What is the procedure for utilizing Javascript to redirect a user on a website back to the login page if necessary?

Is there a way to redirect a website user back to the login page if they try to access a secure page without logging in first? I'm looking to implement this using JavaScript and cookies. Any suggestions or ideas on how to achieve this seamlessly for t ...

Struggling with incorporating HTML5 elements (such as background colors and text colors) into my website using the WordPress editor

My research process began with browsing the internet to find solutions. I stumbled upon this website that provided some insights. Seeking further guidance, I consulted a friend who recommended using specific code, like this: <head> <link rel="st ...

Maintaining a one-line CSS menu layout, the middle element is designed with two inner elements that seamlessly stack without disturbing the appearance of the menu items

I'm facing a challenge with aligning the two elements in the "inline" class - I need them stacked so that the element containing "SECURITIES" is under "A.M.S". The issue arises when I try to add a line break after the A.M.S element, which also affects ...

Adding a see-through Bootstrap Navbar to a Fullpage.js website: A step-by-step guide

I am trying to incorporate a transparent Bootstrap Navbar into my page that is using fullpage.js. The desired outcome is for the navbar to stay on top of the page without affecting the layout of the website content. I have referenced an example here, but ...

What is the sequence in which jQuery events are triggered?

In my HTML code, I have the following: <a href="#" class="topHierarchy big-link" data-reveal-id="myModal" > Both classes, "topHierarchy" and "big-link", are used to attach jQuery event handlers (onClick). big-link triggers a popup box to show, whi ...

Top solution for maintaining smooth navigation across web pages

As I dive into the world of web development, I find myself intrigued by the idea of reusing navigation and banners across multiple web pages. However, despite my research efforts, I have yet to come across a definitive answer. My objective is simple: The ...

Having trouble adding your own styling to bootstrap-4 using a custom CSS file?

Here is the code I used to connect my two stylesheets, with the custom css file being linked second as per what I believe is the correct way to do it. <!doctype html> <html> <head> <link rel="stylesheet" type="text/css" href=" ...

What steps are needed to create a hover box that appears on the right side of my div?

I am attempting to customize the code found at this link so that the box slides to the left side instead of the right. I thought changing right: 0; to right: 100%; in the .overlay class would achieve this, but it is not working as expected. I want it to l ...

Height of inline-block list items in ul is not properly adjusted

I am working on a horizontal navigation bar using inline-block for the li tags. Here is the code snippet: <ul> <li><a href="#">HOME</a></li> <li><a href="#">FEATURES</a></li> <li><a href ...

Set boundaries for square dimensions and placement within the parent container

Struggling to confine a square within specific size constraints while keeping it neatly aligned within the parent container. Balancing these requirements has proven challenging. The layout consists of a main div with two columns on the left and a div occu ...

Having difficulty rendering JSON data on an HTML webpage

Currently, I am working on an API App that utilizes the Foursquare API. With the help of my getRequest function, I am able to obtain results in JSON format, which are then displayed in my console.log. However, the challenge lies in parsing the data from J ...

Troubleshooting the non-functioning addEventListener in JavaScript

I am facing an issue where a function that should be triggered by a click event is not working and the console.log message does not appear <script src="e-com.js" async></script> This is how I included the javascript file in the head ...