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

Struggling to fetch a custom attribute from the HTML Option element, receiving [object Object] as the result instead

I've been facing a challenging issue all day. My task involves making an ajax call to a predefined JSON file and trying to save certain contents into option tags using custom attributes. However, every time I attempt to retrieve the data stored in the ...

What are the specifications for the opacity, width, and height of the background image in Bootstrap's

My current project has specific requirements that I need assistance with: I need the background image of the jumbotron to fit the width of the page and have its height scaled proportionally. The opacity of the jumbotron's background image should be ...

(no longer supported) We are unsure about the usage of /deep/, >>>, and ::ng-deep, ::v-deep

Since /deep/ has been deprecated, I have found that using global styles instead is the only viable solution. Does anyone know of an alternative solution where we can still maintain encapsulation or scoped styling? ...

Enable users to upload pictures along with text descriptions

I want to enhance the user experience on my website by enabling them to upload images along with captions. Currently, users can upload images but they don't have the option to add a caption. I know I need to implement a database for this functionality ...

Optimizing website layout for mobile devices

I have a website with a fixed page layout at . It displays properly on desktop browsers but not on mobile devices. The website is adjusting to fit the browser size, but some components are changing format. Can anyone offer assistance? Thank you! ...

Establish height and width parameters for creating a dynamic and adaptable bar chart with recharts

I am currently facing an issue with recharts while trying to implement a BarChart. The setting width={600} and height={300} is causing the Barchart to be fixed in size, rather than responsive. How can I make the BarChart responsive? I attempted using per ...

Incorporating an Angular 2 Directive within the body tag of an

My goal is to create a custom directive that can dynamically add or remove a class from the body element in HTML. The directive needs to be controlled by a service, as I want to manage the visibility of the class from various components. Question: How ca ...

Tips for updating the color scheme in your CSS file by making hue adjustments to all colors

I have a CSS file and I am looking to automatically generate multiple color variations of the file by altering the hue, similar to using the "colorize" function in GIMP. I came across a tool that does exactly what I need at , however it does not support t ...

Checkbox customization that shifts position when clicked

I seem to be missing something as I can't seem to find a solution for this particular issue. I have implemented custom checkboxes on a list of sentences. Initially, they appear fine, but once you check the first checkbox, it shifts its position. I am ...

Create a visual layout showcasing a unique combination of images and text using div elements in

I created a div table with 3 images and text for each row, but I'm struggling to make all the text-containing divs uniform in size. How can I achieve this? Also, I need to center this table on the screen, and currently using padding to do so. Is there ...

Tips for activating just a single event, whether it's 'change' or 'click'

I am facing an issue with a number input tag that has two event listeners 'on-click' and 'on-change'. Whenever I click on the arrow, both event listeners get triggered. I want only one event to trigger first without removing any of the ...

Vue: The enigmatic world of ghost properties?

During my project work, I encountered the following code snippet: Main component - <ParameterModal>: <template> <modal-wrapper props="..."> <!-- ... other similar templates are present... --> <template v-else-if="moda ...

Is there a glitch preventing the connection between HTML and CSS in Notepad++ even though the link is correct?

I have encountered a puzzling situation that has left me scratching my head in confusion. Despite being a rookie, I've had experience with linking CSS files before and can't understand why it's not working this time around. This issue is per ...

What steps can be taken to implement a code generation feature on our website?

I need to provide the HTML code of my order form to third parties. After they register and pay, they should be able to copy and paste the HTML code from my site onto their own site. The date will then be saved on my site with a reference to the reseller. ...

What is the best way to align text on the right side of a mui AppBar/Toolbar component?

How can I modify the menu bar to include a log out button on the right side? Here is the current code: <main> <AppBar> <Toolbar> <Typography style={{ marginRight: 16 }} variant="h6" className=" ...

Issue with CSS: Parent container disregards child element's width limit

Trying to maintain a 1:1 aspect ratio for a div by using an image. The goal is for the div to fill the available height within a flex-container while the container's width adjusts to fit its content. The square-div displays with correct dimensions, b ...

Troubleshooting issues with sending email with attachments in PHP and HTML

I've been having trouble sending an email with an attachment file; it just doesn't seem to work and I can't seem to receive any files. Although my HTML syntax appears correct, as I haven't received any bounce-back emails, the issue per ...

Strikethrough text with a distinct color similar to that seen in email messages

In my endeavor to customize the line-through text decoration in red and change the text color to black or any other color for an email, I have experimented with various methods. These include wrapping the text in a span and setting its color to red, using ...

When the HTML code is rendered in a web browser, the CSS styles are not being

I am utilizing adminer within a docker container. When testing services, I utilize the URL mydomain.tld. In order to interact with this container directly, one option is to expose and map a port, such as mapping port 8081 to the adminer port 8080. Access ...

Perform mathematical operations on numbers using jQuery based on their index

Trying to manipulate numbers in the HTML with this less-than-ideal code. The display starts like this at the beginning: 1/ 6 And ends up like this at the final index: 6/ 6 Utilizing this in conjunction with pageslide.js as a workaround for the default ...