Strategies for aligning an undetermined amount of dynamic divs in a container with 100% width

I am faced with a challenge involving a container that is 100% width and contains up to 6 fluid items, each occupying 1/6 of the container's width (16.66%)

When there are fewer than 6 child divs, I want them to maintain their width proportion but be centered horizontally within the container.

Adding margins or padding on the container affects the item widths as it changes the total width they are based on.

While absolute positioning and JS could solve this issue, I prefer a CSS solution if possible.

Looking for advice on the best approach in this situation.

I have provided a visual example on jsfiddle: http://jsfiddle.net/9L4Qd/

Snippet from jsfiddle:

body, html {
    height: 100%;
}
body {
    margin: 0;
    padding: 0;
}
#container {
    width: 100%;
    height: 100%;
    background-color: #000000;
}

#container div {
    width: 16.66%;
    float: left;
}

<div id="container">
 <div style="background-color: #ffcc00;">
    Option 1
 </div>
 <div style="background-color: #cccc00;">
    Option 2
 </div>
 <div style="background-color: #ffcc00;">
    Option 3
 </div>
 <div style="background-color: #cccc00;">
    Option 4
 </div>
 <div style="background-color: #ffcc00;">
    Option 5
 </div>
</div>

Thank you,

Dave

Answer №1

Try using display:inline-block instead of floats

JSfiddle showcasing 6 divs

JSfiddle featuring 5 divs

CSS Styles

body, html {
    height: 100%;
}
body {
    margin: 0;
    padding: 0;
}
#container {
    width: 100%;
    height: 100%;
    background-color: #000000;
    text-align: center;
    font-size: 0;
}

#container div {
    width: 16.66%;
    display: inline-block;
    font-size:1rem
}

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

menu with dropdown options within dropdown options

I am experiencing an issue with creating a nested menu within my navigation header. Specifically, under the "Reports" section, I want to have two submenu items named "Global Shop" and "Ndustrios". When hovering over either of these submenus, additional men ...

What is the best way to modify the text color in an MUI Text Field?

I'm completely new to using MUI and React, and I've run into an issue with changing the text color in my input field. Even though I specified inputProps={{sx: { color: "CCCCCC" }}}, the text color remains black while the label and searc ...

create a word with only **one** bold letter in the word ionic-angularjs

Can someone please guide me on how to style only a specific letter within a word using angularJS? Specifically, I want to make the first letter bold while keeping the rest of the word in normal font. For instance, if I have an array of words like: var Wor ...

Creating a stylish layout with Bootstrap 4: equal height divs filled with color and a sleek gutter in between

I am struggling to create a bootstrap 4 layout as per my design screenshot. I have researched other Stack Overflow posts without finding a solution. The .container-fluid layout has a grey fill color, achieved using my custom class .bg-grey. As per the d ...

"Challenges encountered while trying to format a table responsively with Bootstrap CSS

My objective is to display two tables side by side, with the right table overflowing horizontally. However, I am currently facing an issue where the second table is being pushed below the first one. Any assistance on resolving this problem would be highly ...

Is there a way to smoothly incorporate a new animation into a page, while ensuring that existing elements adjust to make room for it?

I am looking to smoothly fade and move a hidden element (display:none) within the page, shifting other elements to animate into their new positions as necessary. When I reveal the hidden element by changing it to "display:initial," it should seamlessly a ...

Inheritance of currentColor in SVG URLs

Seeking the same solution as mentioned in How can an SVG image inherit colors from the HTML document?, specifically when applied to an svg image used as content on a :before pseudo-element. (The intended outcome is for both checkmarks to adopt the red col ...

Gradually enlarging the size of the font as time progresses

$( "h1" ).hover( function(){ for(var i=0;i<255;i++){ setTimeout(function(){$("#fat").css("font-size", (i + 12) + "pt")},200); } }, function(){$("#fat").delay(200).css("font-size","12pt")} ); My goal is to gradua ...

Current recommended method for updating innerHTML with properly formatted text?

My understanding is that using innerHTML on an element can be risky, as malicious users could potentially inject harmful content such as <script> tags. This question shows there are multiple alternative tags, some of which are non-standard. In my s ...

Utilizing the Swiper Library for my carousel creation, I am striving to centrally position my card element but encountering difficulty in achieving this

I'm currently using the Swiper Library to create a carousel. I'm having trouble centering my card element in the middle of the screen. Below is an important snippet of code for the component that I need help with. I am utilizing styled components ...

What is the best way to design a responsive menu that is perfectly centered?

I'm facing challenges with designing a menu that includes 4 items, needs to be centered next to each other with spacing in between, and should stack vertically on mobile devices. The desired look for the menu can be viewed in this image... https://i. ...

Is there a way to access VMWare localhost on a mobile device?

Currently, I am utilizing VM Ware for my development tasks and I am working on creating some responsive design code. Instead of using the web-developer tools' responsive layout emulator, I would like to view it in action on my mobile device. I am look ...

The hero footer stubbornly refuses to grace the bottom of the page

I've been working on a webpage design using the Bulma CSS framework. Everything seems to be going smoothly, but I'm encountering an issue when trying to add a footer to my page - it's not staying at the bottom. Should I create custom CSS f ...

Select your vehicle from the drop-down menu

Seeking assistance with a Javascript drop-down selector for vehicle make, model, and year. The goal is to dynamically generate a link based on the user's selections. I'm struggling with implementing the year selection without replacing the model. ...

Creating dynamic visual representations on a website by generating real-time graphs

Every minute, new data is added to my csv file. I would like to display this data as graphs on a webpage. The file contains 12 parameters and I am interested in creating charts that show the relationship between each parameter and time. ...

Issue with connecting JavaScript to HTML button

I am currently working on a 10-second countdown feature for a game using Javascript and HTML. Currently, the code runs upon page load. However, I am facing difficulty in linking it to a button so that it triggers upon clicking. ...

How can you center the body of a webpage while using the zoom in and zoom out features in HTML5?

What is the best way to maintain body alignment while zooming in and out on a web page? <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> body { font-family: 'Merriweather Sans&apos ...

IE causes link_to :remote => :true to malfunction

I'm facing an issue with remote links in Internet Explorer, and it's crucial to resolve it promptly as the deadline is today. The problem arises when we utilize AJAX to initiate a remote call to an action and evaluate the JavaScript returned. Wh ...

Ways to transfer data to a different PHP file without the need to navigate to the actual page

How can I make a button send information to another PHP folder while staying on the same page? <button class="btn btn-outline-dark w-60"><?php echo "<td> <a href='carrinho.php?acao=adc&id=$PegaID'>Adicionar ...

Provide a unique <li> attribute for the JavaScript function to utilize

Is there a way to pass specific attributes from dropdown options to a javascript function? I have tried using .data() and .attr(), but the console keeps showing "undefined". Any suggestions on how to achieve this in a cleaner and simpler way would be gre ...