aligning divs in a horizontal distribution is ineffective when the divs become fixed in place

When distributing divs horizontally equally, issues arise when these elements are attached without spaces in between (particularly in Chrome and Firefox).

For instance: http://jsfiddle.net/pdelorme/au9ouko0/

HTML :

<div class="ul">
<div class="li">Item 1</div>
<div class="li">Item 2</div><div class="li">Item 3</div>
<div class="li">Item 4</div>
</div>

CSS :

.ul {
    text-align: justify;
}
.ul > .li {
    display: inline-block;
}
.ul:after {
    content: "";
    width: 100%;
    display: inline-block;
}
.li {
    background-color: hotPink;
    color: #fff;
}

The visual outcome is as follows :

=======================================
Item 1       Item 2 Item 3       Item 4
=======================================

This layout works correctly except when Item 2 and Item 3 are adjacent.

Desired output:

=======================================
Item 1     Item 2     Item 3     Item 4
=======================================

The challenge lies in the fact that since my HTML is condensed, the spaces between divs get stripped away automatically, preventing even distribution.

<div class="ul"><div class="li">Item 1</div><div class="li">Item 2</div><div class="li">Item 3</div><div class="li">Item 4</div></div>

ends up looking like this:

=======================================
Item 1 Item 2 Item 3 Item 4
=======================================

Suggestions on how to address this issue?

Answer №1

Consider utilizing the flexbox module and align elements using space-between. This method does not depend on display: inline-block, so it is not affected by server-side page compression.

Here is a demonstration fiddle: http://jsfiddle.net/au9ouko0/4/

ul {
    display: flex;
    justify-content: space-between;
}

Output (border added for visibility)

Check out browser support for Flexbox here: http://caniuse.com/#feat=flexbox

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

When pasting Arabic text into an input box, the words in HTML appear to be jumbled and shuffled around

When I replicate this text يف عام and input it into a box, the output is shown as follows عام يف ...

The power of PHP's echo function comes alive as it seamlessly connects

I am currently working on developing a product catalog using PHP. Below is the HTML flex-container code: <div class="products"> <?php echo file_get_contents("http://192.168.64.2/CodeWay/Pages/product.php"); ?> & ...

The ball refuses to fall into the designated boxes

I designed a basic webpage featuring 3 boxes that, when clicked on, trigger the dropping of a ball into them. Below is the code I used: <!DOCTYPE html> <html> <head> <script type="text/javascript"> function popup (n) { ...

Is there a way to modify the location of the datepicker/calendar icon within my bootstrap form?

When using react-bootstrap with the <Form.Control type="date"> element, I noticed that the calendar icon or date picker is automatically positioned to the right side/end of the form field. However, I am interested in moving the calendar ico ...

What is the best practice for adding a DOM element at a precise location within an ng-repeat loop?

I am currently working on developing a podcast player application using AngularJS. At this point, I have successfully created a list of available podcasts that can be played, utilizing ng-repeat. The code for the list is shown below: <ul ng-controller ...

How can I style my tables to have different border spacing between the table head and table body?

When it comes to tables, I want to customize the column spacing in a specific way. I need space between columns in the table header but no gaps between columns in the table body. How can I achieve this by including border space only in the thead section? ...

Difficulty transferring form information to PHP utilizing Ajax

Originally, I had a traditional HTML form that submitted data through a button, redirecting the user to the page where the PHP code ran successfully. Now that everything is functioning as expected, I aim to pass the form variables to PHP using Ajax in ord ...

Modify the default navbar from Bootstrap to display as a fixed navbar

Is there a way to change my default navbar into a fixed navbar when I scroll? I have tried adding data-spy="affix" data-offset-top="200", which works but doesn't look quite right. Should I create a new CSS class or is there an easier solution availab ...

A step-by-step guide on rotating a solitary image within HTML

Is there any way to make these three images in my table rotate continuously? I've looked everywhere for a solution but can't find anything. I want the rotation to mimic that of a planet rotating on its axis. Check out this one: BARCELONA <t ...

The never-ending loop of a jQuery function continues indefinitely

Having some trouble with this code. It's supposed to take the first line, check it, remove it, and then move on to the next line. However, if all lines are empty, the code just keeps running endlessly without stopping. function getSocksListInput() { ...

Modify the color of the footer area and eliminate any underlines present

In the footer section, the tags are displaying in blue color and I would like to remove the line under the li tags. If necessary, I can provide additional HTML or CSS code for reference. Below is the snippet of HTML and CSS related to the footer: My HTML ...

Error: Badge not responsive in Boostrap 4

I have a project to create a table of contents using BOOTSTRAP4. I researched and used https://getbootstrap.com/docs/4.6/components/list-group/. However, the Badge does not align correctly as expected, it remains fixed with the content. <ul class=&qu ...

The functionality of jQuery .rotate() appears to be malfunctioning

I'm struggling to get this code working properly. My jQuery version is 2.1.0. Here's the code snippet: $("a.shownav img").rotate(180); Any suggestions on how to make it function correctly without relying on a plugin? ...

Confusing Accessibility Rules: A Guide that Leaves Me Scratching My Head

I've been reviewing a set of rules to make sure we're in compliance with the Americans with Disabilities Act. One guideline has me scratching my head: Use the title attribute when text is not accessible I'm confused - how can text be ina ...

Add design to the footer during a specific event using Javascript

Here is the CSS code I am working with: .footer { font-family: "lato", sans-serif; padding: 20px; line-height: 1.2; text-align: right; background: #eee; color: #0D47A1; font-size: 13px; height: 80px; position: fixed; right: 0px; botto ...

Obtaining two divisions that simultaneously display partials in a parallel manner

It's proving difficult to align two divs containing rendered partials side by side within a form. Strangely enough, when I replace the divs with plain text, they respond perfectly to my style changes. However, as soon as I include the render code, the ...

Error encountered while pressing key on vscode led to failure in requesting textDocument/documentLink

Greetings! I'm currently using VSCode on both my mac and Windows 10 machines. Recently, they have both started experiencing the same issue after a recent update/rollback. Every few keystrokes, the output box pops up with the "HTML Language Server" se ...

Resize Pictures in Carousel for Smartphones

Currently, I am in the process of optimizing my website for mobile devices. The NivoSlider, which is used throughout the site, does not seem to scale well on smaller screens. Despite trying various methods in the mobile section of my stylesheet such as " ...

"Utilizing JQuery to enhance task management by implementing a delete function

I need help figuring out how to create a function that can delete tasks from my todo list. Currently, each task has its own remove button, but I want to implement a single remove button at the bottom that removes checked or crossed out tasks. Any suggestio ...

Looking to verify a disabled select element and adjust the opacity of that element when it is disabled

$_product = $this->getProduct(); $_attributes = Mage::helper('core')->decorateArray($this->getAllowAttributes()); ?> <?php if ($_product->isSaleable() && count($_attributes)):?> <dl> <?php foreach($_attrib ...