Is It Possible to Have Four Equally-Spaced DIVs with Variable Widths?

I am currently facing a challenge in creating a simple sub-navigation system for my website. I want to have either 3 or 4 equally spaced DIVs across the top of the content area. Despite being a common requirement, CSS does not offer straightforward solutions for this problem.

1) For example, having 3 Variable-Width, Equally-Spaced DIVs:

[[LEFT]                [CENTER]               [RIGHT]]

2) Alternatively, with 4 Variable-Width, Equally-Spaced DIVs:

[[LEFT]    [LEFT CENTER]    [RIGHT CENTER]    [RIGHT]]

My workaround for the first issue with 3 DIVs involves floating the left and right DIVs and setting an arbitrary size for the middle DIV while using "margin: 0 auto". This approach is not ideal but provides a rough solution assuming no changes are made to the navigation.

To address the second problem with 4 DIVs, I center a DIV as before, float two DIVs within it, and set arbitrary sizes for alignment:

[[LEFT]    [[LEFT CENTER]    [RIGHT CENTER]]    [RIGHT]]

However, this method becomes complex when language or image modifications are implemented on the site, necessitating re-calculation of alignment values. It merges structure with presentation, making it overly complicated.

Any assistance in simplifying this task would be greatly appreciated.

EDIT 07/20/2012 5:00PM

I attempted a "table-cell" solution using percentages, which worked to some extent. However, I encountered another issue due to each DIV serving as a container for two additional DIVs, displaying icon-label pairs either floated or displayed inline-block.

For reference: http://jsfiddle.net/c3yrm/1/

The final element in the list appears incorrectly aligned.

Your continued help is highly appreciated!

EDIT 07/20/2012 7:16PM

Thanks to the guidance from arttronics, a final solution has been achieved: http://jsfiddle.net/CuQ7r/4/

Answer №1

For more information, check out the jsFiddle Pure CSS Demo

To solve the issue, we utilized the float property for each breadcrumb and implemented a straightforward calculation to determine the width percentage of each breadcrumb based on the total number of breadcrumbs.

Answer №2

To simplify the calculation, consider utilizing percentages:

[[LEFT=24%]2% margin><2% margin[LEFT CENTER=24%]2% margin><2% margin[RIGHT CENTER=24%]2% margin><2% margin RIGHT=24%]]=100%/??px

You can then set a width for the container and use

display:inline;

If adding borders to visualize the divs causes extra space, adjust the element's width by 1% or alter their background colors.

Answer №3

ol {
    width: 400px;
    /*width: 800px;*/

    display: table;
    table-layout: fixed; /* the secret sauce for equal width */
    background: #ccc
}
ol > li {
    display: table-cell;
    border: 1px dashed red;
    text-align: center
}

Check it out here: http://jsfiddle.net/QzYAr/

Answer №4

A method that I have discovered is utilizing flex boxes (or inline-flex).

For a detailed explanation and example of how this can be implemented, you can visit this link.

In my opinion, flex boxes will likely become the preferred approach for managing these types of layouts in the future. However, until other browsers align with Mozilla's utilization of the flex-basis attribute (including values such as min-content, max-content, fit-content, etc.), issues may persist particularly with responsive designs. For instance, when the window size is reduced, lengthy content like "a_really_really_long_word" may not completely display within the designated space, leading to some content being cut off on the right side of the screen if precautions are not taken.

By incorporating the flex-wrap property, it may be possible to ensure that all elements fit within the layout. You can refer to this resource for another illustration of this concept specifically catered to Mozilla browsers.

I typically opt for flex boxes in scenarios such as letterheads or tables where the width is relatively fixed and spacing is automatically adjusted. Conversely, for websites requiring content to condense significantly, I often utilize nested float and inline-block objects based on recommendations from other responses provided here.

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

Troubleshooting a problem with the Bootstrap dropdown menu

Can anyone assist with an issue regarding Bootstrap 4.4.1? I am facing a problem where two consecutive dropdown links+menus are not displaying correctly. The second dropdown menu is showing the content of the previous link instead of its own, even though t ...

Changing the color of a Highcharts series bar according to its value

Playing around with Highcharts in this plunker has led me to wonder if it's possible to dynamically set the color of a bar based on its value. In my current setup, I have 5 bars that change values between 0 and 100 at intervals. I'd like the colo ...

Enhance Your Website with HTML and JavaScript

Here is the code snippet I am working with: sTAT **javascript** var acc = document.getElementsByClassName("item-wrapper"); var i; for (i = 0; i < acc.length; i++) { acc[i].onclick = function(){ this.classList.toggle("selected"); this.nextElementS ...

Class for Bootstrap CSS to adjust card image when resized

I'm stumped, I can't seem to find the right CSS element to trigger the transition from displaying the image on the right to below the text. It's a simple change that I didn't anticipate, but I kind of like it. In horizontal view, I wan ...

What is the best way to make a textview span its layout in an android app?

Is there a way to style text in a TextView layout itself without using any programming? I have about 20 XML files and no activity associated with them. I've tried using tags but it doesn't seem to work. android:text="Lets look how to make < ...

How can I validate HTML input elements within a DIV (a visible wizard step) situated within a FORM?

I recently made a decision to develop a wizard form using HTML 5 (specifically ASP.NET MVC). Below is the structure of my HTML form: @using (Html.BeginForm()) { <div class="wizard-step"> <input type="text" name="firstname" placeholder ...

Display map.svg on the browser and execute a function when any area is clicked

Creating an online parking system that is compatible with desktop, iPad, and Android devices is my current project. I have a .svg formatted map for the parking area. I am looking for advice on how to display this map in a browser and execute JavaScript fu ...

What should we name this particular style of navigation tab menu?

What is the name of this tab menu that includes options for next and previous buttons? Additionally, is there a material-ui component available for it? https://i.sstatic.net/0m9rH.png ...

Ways to turn off textarea resizing

Is there a way to prevent horizontal resizing on a textarea while still allowing vertical resizing? I find that the textarea often disrupts the design of my contact us page. If anyone has a solution for disabling the horizontal resize feature, please shar ...

Having difficulty in closing Sticky Notes with JavaScript

Sticky Notes My fiddle code showcases a functionality where clicking on a comment will make a sticky note appear. However, there seems to be an issue with the Close Button click event not being fired when clicked on the right-hand side of the note. I have ...

"Bootstrap is functioning properly on my local environment, but it seems to

Utilizing the MVC framework and bootstrap has been successful for optimizing my website locally. However, when I upload it to the server, none of the CSS is being rendered. Additionally, the front page, meant to be a carousel slider, appears as a vertical ...

align the button to the left using bootsrap configuration

Is there a way to align a button to the left using bootswatch/Litera, which is a bootstrap 4 beta theme? I attempted to use the float-left class on the button and place it inside a div, but the outcome remained the same. https://i.sstatic.net/qs4zU.png ...

Looking to design a popup using CSS styles

How can I design a pop-up window using CSS for a specific div? I also want to display additional information within another div inside the popup. ...

Shorten a data point in JSON code

I'm currently in the process of developing a stock widget that utilizes JSON to retrieve data from the Yahoo API/YQL console. I am specifically working with values extracted from the key ChangePercentRealtime, however, the values are longer than what ...

Angular 6 is showcasing dates in a quirky manner

In my Angular app, users can enter comments. The comments are displayed automatically with the date they were added. However, there is an odd behavior that I've noticed: When a user enters a comment, the time displayed is incorrect. (Check the ...

Issues with padding and margin on iOS devices

On my iPhone (iPhone 6, iOS 8), I noticed that the loader is positioned slightly above the button. However, when I use the Chrome mobile emulator, I can't see this issue. I'm puzzled about how to resolve it. What do you think could be causing thi ...

How many hyperlinks are there on this webpage?

Is there a way to determine the number of links on a web page without counting social media networks like Google, LinkedIn, Facebook, and Twitter? ...

Modify the color of the matSnackbar

I'm having trouble changing the snackbar color in Angular using Angular Material. I tried using panelClass in the ts file and adding it to the global css, but the color remains unchanged. Any suggestions on how to resolve this? I am still new to this ...

The justify-content-around property in Bootstrap-4 does not seem to be functioning as expected when

I've been working with bootstrap-4 and I'm struggling to understand why justify-content-around functions properly with flex-row but not with flex-column. I tried applying flex-column justify-content-around to div.item2 but it's not producing ...

Concealing Contact Form Using Javascript

Upon making adjustments to a website, I encountered an issue with the contact form. The form is meant to be hidden on page load and only appear when the envelope icon is clicked. However, currently the form is visible by default, and clicking the envelope ...