How white space manipulation is leading to div stacking in Pure CSS

I am currently utilizing Pure CSS to design a website, but I have encountered an issue. Whenever there is white space between nested grid elements, it disrupts the layout and causes the last div to move onto the next line. In order to troubleshoot this problem, I set up a test site with minimal content just to check if it was an isolated incident, but unfortunately, I am still facing the same dilemma.

<html>
  <head>
    <link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
  </head>
  <body>
    <div class="pure-g">
      <div class="pure-u-1-2">
        <div class="pure-u-1-3">
          <p>Hello world</p>
        </div>
        <div class="pure-u-1-3">
          <p>Hello</p>
        </div>
        <div class="pure-u-1-3">
          <p>Hey there</p>
        </div>
      </div>
      <div class="pure-u-1-2">
        <p>Hi :)</p>
      </div>
    </div>
  </body>
</html>

This piece of code leads to the following outcome:

If I remove the white space in between the divs, like so

<div class="pure-u-1-3"><p>Hello world</p></div><div class="pure-u-1-3"><p>Hello</p></div><div class="pure-u-1-3"><p>Hey there</p></div>

the issue resolves itself:

This particular problem is observed on both Chrome and Firefox. Is this a flaw in Pure CSS, or am I making a critical mistake somewhere?

EDIT: I came across a solution specific to YUI Pure CSS. For nesting layouts, each column set has to be enclosed within its own .pure-g division. I elaborated more on this in the Github discussion.

Answer №1

This statement applies specifically to inline-block elements. Exclude them from the code:

<div class="pure-u-1-2"><!--
    --><div class="pure-u-1-3"><p>Hello world</p></div><!--
    --><div class="pure-u-1-3"><p>Hello</p></div><!--
    --><div class="pure-u-1-3"><p>Hey there</p></div>
</div>

Answer №2

If you're looking for more information on this topic, be sure to check out the thread linked below. It's a comprehensive resource that has been thoroughly examined and debated: HERE.

Answer №3

Whitespace plays a significant role in the display of inline elements, but once there is more than one space, it is condensed to just one whitespace.

When using CSS to divide the width with classes like pure-u-1-3 for 33.333% per item, adding three items into a container with pure-u-1-2 fills up the space entirely, leaving no room for additional whitespace.

Therefore, if extra white space is added, the last div will move down due to the lack of available space.

Answer №4

When using inline-block

in CSS, it will maintain the whitespace between elements.
However, there are various techniques available to remove the extra space between these elements.
Click here
to find some useful hacks to resolve this issue. Personally, I believe that the
font-size: 0px
hack is effective, although it may be challenging when working with EM units.

Instead of resorting to workaround solutions, I recommend utilizing
float:left instead of display:inline-block
as a better alternative to avoid dealing with any problematic spacing issues.

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

Setting a maximum character limit for an HTML textarea

I am currently attempting to limit the maximum number of characters allowed in a textarea. Using: <textarea rows="4" cols="50" maxlength="50"> It is functioning correctly in Firefox, but there is no impact in IE. This is an issue as many website u ...

Creating a responsive design for Bootstrap divs

I am trying to display 5 images in one row with 5 columns. I have used the following CSS for this layout: .col-half-offset { margin-left: 4.166666667% } This setup works well on the web, but on mobile devices, the layout does not di ...

Visibility of the button changes based on the focus of the inputs

I am faced with a scenario where I have the following DOM structure: <input type='text'> <button>Add</button> and this CSS styling: button { visibility: hidden; } input:focus + button { visibility: visible; } In thi ...

Why won't my Java servlet code execute?

included the directory structure To facilitate adding two numbers and displaying the result upon clicking a submit button, I developed a straightforward Java servlet. This servlet retrieves the necessary information when called. The development environmen ...

How to prevent page scrolling in an Android web browser

Struggling to prevent my website from scrolling on different devices. While it's relatively easy to achieve on desktop browsers using overflow: hidden;, I'm facing issues with my Android tablet where the page continues to scroll no matter what I ...

How to access the current class in Sass without referencing the parent class

.site-header .basket position: relative &-container width: 100% padding: 0 18px $basket: & &.opened #{$basket}-link border: 1px solid #e5e5e5 ...

Having trouble determining the dimensions of a newly added element

I am using jQuery to dynamically add an image but I am unable to retrieve its dimensions (width and height). Below is the snippet of code from my HTML file: <input type="checkbox" id="RocketElement" data-id="1"> And here is the JavaScript/jQuery c ...

Remove a div element with Javascript when a button is clicked

I am working on a project where I need to dynamically add and remove divs from a webpage. These divs contain inner divs, and while the functionality to add new divs is working fine for me, I'm facing some issues with removing them. The code snippet b ...

Creating a Custom Progress Bar with Bootstrap

I'm encountering an issue with aligning a Bootstrap 3 progress bar vertically within a table cell. While I have successfully aligned other cells to the middle, the progress bar still remains in its original position. How can I eliminate the excess spa ...

What's the best way to continuously increase my counter using the data received from my AJAX calls?

On the final step of my first ajax project, I have implemented a thumbs-up icon that increments a column in the database when clicked. The following code achieves this: Viewable Page HTML and jQuery: <div id="comment_id">+1</div> <div id=" ...

Is there a way to separate the sub-navigation ul from the main navigation ul?

My Content Management System generates a main-nav menu with one sub-menu. Both menus are styled using standard ul tags with a class of “navCMSListMenuUL” and then customized in my CSS. The problem arises when hovering over a link, causing the container ...

The HTML output is essential for creating the content of a Bootstrap carousel

Is there a way to populate my bootstrap carousel with the content of an HTML page instead of images? I attempted to create a div and use CSS to load the contents from ac1_div using content:url(carousel_content.html), but it was unsuccessful. <!-- our ...

What is the recommended approach for font color usage within a single block in HTML5?

In HTML5, the font tag is considered deprecated. If I have a paragraph where I want to display certain words in specific colors, using the outdated font tag may not be ideal. What are some modern and clean CSS techniques that can achieve this same effect ...

Enhance the appearance of your Dojo TabContainer when viewing in Internet Explorer

Firefox seems to handle the following code without any issues: < div style="position:absolute;top:0px;margin-top:60px;bottom:0px;width:100%"> < div id="mainTabContainer" dojoType="dijit.layout.TabContainer" style="width:100%;height:100%"> {% fo ...

Using a different background image in a React application

style={{ backgroundImage: "url(" + "http://production-partner-cdn.s3.eu-west-1.amazonaws.com/general-files/unique-image.jpg" + ")" }} I would like to show a different image in case the link for the original one is invalid ...

HTML5 body content and footer area

I'm working on a website with the main content area set to 95% width, but I'm facing an issue - I want the footer to be 100% width even though it's a child of the main content, which limits its width. Is there a way to make the footer overr ...

Loading images dynamically without changing the src attribute

My website displays 40 baby names on each page, with user-uploaded photos associated with each name. Currently, I am using iframes to show the images when a user clicks on the "show photos" button. However, this method prevents the images from being indexe ...

I require a breakdown of the JavaScript code, please

Are you struggling with a code snippet that involves CSS, JavaScript, and HTML? Let's take a look at the complete code: <!doctype html> <html> <head> <link rel="stylesheet" type="text/css" href="http://snipplicious.com/css/bo ...

What could be causing setInterval to only run once?

setInterval is only running once for me. I tried using setTimeout and creating a loop inside the function, but it's still giving the same output. I'm working with brackets and using live preview, in case that matters. const buttons = document. ...

Using mobile devices for multipart forms: managing actions that appear upon selecting a file input

My website features a multipart form where users are required to upload images. <form method="POST" encType="multipart/form-data" action="/uploads" id="forminho"> <div className="btn btn-success" id="submitFileBtn"> Select Image ...