Achieve full parent width with floated divs

My goal is to arrange 10 boxes horizontally on a webpage. Each box must have a minimum width of 150px and a maximum width of 299px. The challenge is to fit as many boxes as possible across the page without any gaps, ensuring that each box has an equal width (adjusting by a pixel if necessary due to rounding).

For example: If the page width is 660px, we should use 4 boxes at 165px width. If the page width is 600px, we should use 4 boxes at 150px width. If the width is 597px, then 3 boxes at 199px width should be used, since a box cannot go under 150px.

The remaining boxes at the bottom should also have the same width as the ones above.

I am looking for a way to achieve this layout. I have shared a sample fiddle link here: http://jsfiddle.net/CD4f2/1/

Please note the gap on the right side of the rows of boxes in the current setup.

I am constrained to use the following code:

<body>
<div id="mainPage">
    <div id="bar">Width of a row of boxes should match the length of this bar.</div>
    <div id="capTable">
        <div class="cap" id="cap0">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap" id="cap2">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap" id="cap4">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap" id="cap6">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap" id="cap8">
            <img class="capImage">
            <input type="text">
        </div>
        <div class="cap">
            <img class="capImage">
            <input type="text">
        </div>
    </div>
</div>

body 
{
    background-color:black;
}

.cap 
{
    background-color: red;
    float: left;
    height: 150px;
    max-width: 299px;
    min-width: 150px;
}

.capImage 
{
    background-color:blue;
    float: left;
    height: 37px;
    width: 37px;
}

#bar 
{
    background-color: orange;
}

#cap0, #cap2, #cap4, #cap6, #cap8 
{
    background-color: green;
}

#mainPage 
{
    margin: 0 auto;
    max-width: 800px;
    min-width: 150px;
}

I have tried using tables and various combinations of floats, displays, and overflows but faced the same issues with gaps. I am considering manually specifying different resolutions in CSS, although an automated solution would be preferable if achievable.

I prefer not to resort to Javascript unless absolutely necessary, as I believe I can handle it myself.

Your insights and suggestions are appreciated. Thank you.

Answer №1

I have updated the .cap divs to use percentage widths instead of pixels for a more responsive layout design.

Check out the DEMO here

Key CSS Modification:

.cap 
{
    background-color: red;
    float: left;
    height: 150px;
    width: 25%;
    min-width: 150px;
}

@media only screen and (max-width: 1024px) {
    .cap {
        width: 50%;
    }
}

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

In JavaScript, use the href property to redirect to an external URL and then automatically scroll to a specific class on the page

I have a specific scenario where I need to create a link that redirects to an external website, of which I do not own. However, I am aware that there is a div with a particular class located at the bottom of their page, linking to an article. My goal is to ...

Unable to execute a JavaScript function when triggered from an HTML form

This is the code for a text conversion tool in HTML: <html> <head> <title> Text Conversion Tool </title> <script type="text/javascript"> function testResults(form) { var str = form.stringn.value; var strArray = str.split(" ...

Preventing CSS shapes from overlapping with the next DIV

I'm currently working on creating a V-shape using CSS. While I have successfully created the shape, I am facing an issue where I cannot get the shape to appear "above" another div with a background image when it is placed before it and made negative. ...

Showcasing PHP variables within HTML using jQuery

I am currently looking for the best way to showcase information stored in a PHP variable, retrieved from a MySQL database. My initial thought was to use jQuery. Here are my questions: When a user inputs a number into a field, I receive that Member' ...

Easily move elements using a jQuery click animation

Having trouble animating a div's top position by -130px to move it off screen? Can't figure out why it's not working? I'm fairly new to jQuery/Javascript. I have a button/hyperlink with the ID #NavShrink. When clicked, I want the div # ...

Using JavaScript functions to modify the style of the DOM

Is there a way to change the style of a dom element using JavaScript when only the element id and style value are passed as parameters, without passing the actual style parameter itself? For example, is it possible to use the id "first" and set the color ...

Problem with fixed element on mobile due to viewport width

I am experiencing an issue with the fixed element's width. Here is a code snippet for reference: https://codepen.io/highfield/pen/PKpXGG <body> <nav class="navbar"> <div style="position:absolute; left:20px; top:12px"> & ...

Using Telegram markdown: Can you combine both bold *and* italic? (September 2018)

After referencing the Markdown Syntax guide on Telegram’s Wiki page, it appears to be quite straightforward to format text as bold and italic. The guide explains that: *this is in italic* and _so is this_ **this is in bold** and __so is this__ ***this ...

Maximizing Particle Performance Using JavaScript

I am experimenting with creating particles in JavaScript for the first time, and I'm unsure if the code below is optimized. When I generate 100 particles on the screen, there isn't much of an FPS drop noticeable. However, when multiple clicks o ...

Is there a way to incorporate pseudo-dynamic css into my projects?

I'm struggling with managing custom colored elements on my website. For instance, I have a hundred navigation squares on the site, each with its own unique color. The only solution I can think of is creating separate CSS classes for each color, but t ...

Creating a Vertical Stack of Three Divs in ReactJS

Just Getting Started: As a newcomer to ReactJS and web development in general, I've put effort into researching my question without success. I acknowledge that the solution might be simpler than I think, so I apologize in advance if it turns out to b ...

Troubleshooting Flexbox in Internet Explorer 11: Issue with 100% width not functioning properly within nested flex containers with a 'column'

Is there a way to ensure that an image within nested flex containers has a width of 100% in IE11, even when the container has flex-direction: column? I've tried setting the width to 100% with a max-width of calc(100% - 0.1px), but unfortunately it doe ...

The Image Slider functions smoothly in Chrome, but encounters issues in IE11

Here is a link to the code I've been working on: http://jsfiddle.net/wf32jbhx/ I attempted to host images on my SharePoint site, but they ended up stacking on top of each other instead of formatting properly. Oddly enough, everything appears fine in ...

What separates text-align:center from margin auto?

As someone who is just beginning to learn about CSS, I have come across the properties: text-align:center; and margin:auto; Both of these seem to center elements, but how exactly do they differ from each other? Any insights would be much appreciated. T ...

What is the best way to make an image clickable in Next.Js?

Is there a way to turn an image into a link in Next.Js? I have the Instagram logo that I want to use as a link to my Instagram page. I want it to open a new tab and redirect the user to my Instagram account when clicked. I would really appreciate any guid ...

Guide to concealing List elements from the Search Filter when the search input field is emptied

I am facing a challenge with an HTML list of items. Here is the structure: <ul id="fruits"> <li><a href="#">Mango</a></li> <li><a href="#">Apple</a></li> <li><a href="#">Grape</a>& ...

"Error: The function $(...).autocomplete is not recognized" in conjunction with Oracle Apex

Currently experiencing some frustration trying to solve the issue at hand. The Uncaught TypeError: $(...).autocomplete is not a function error keeps popping up and I have exhausted all resources on Stack Overflow in an attempt to fix it. This problem is oc ...

Implementing ngClass with a dynamic ID in the URL condition

I am attempting to create an ngClass condition that adds an active class to a list element. Specifically, I want it to work for both the URLs '/companies' and '/company/:id'. The issue I am facing is that the current setup does not fun ...

Connection to external sources has been deactivated

I am experiencing an issue on my website at . When attempting to click on a link, it seems to be disabled. I suspect this is due to the presence of two images at the end of the navigation bar. Removing these images causes the navigation bar to fall apart, ...

Combining an array of intricate data models to create

Currently, my setup involves using Entity Framework 7 in conjunction with ASP.NET MVC 5. In my application, I have various forms that resemble the design showcased in this image. By clicking on the "new" button within these forms, a Bootstrap modal like t ...