Adjusting padding evenly across an unspecified number of columns

I'm currently facing a challenge with a multi-column layout.

In a basic setup of three columns, I aim to adjust the padding so that each gap between the columns is consistent (2rem). However, the tricky part lies in crafting rules that can be applied to 2, 3, 4, or even 5 columns without any alterations.

Although I am using PureCSS for the multi-column structure, familiarity with this framework is not required as my issues are independent of it.

Here is the current markup:

<div class="pure-g">
  <div class="pure-u-1-3"><div class="myblock">content col 1</div></div>
  <div class="pure-u-1-3"><div class="myblock">content col 2</div></div>
  <div class="pure-u-1-3"><div class="myblock">content col 3</div></div>
</div>

Each column occupies 33.333% width. To create equal gaps between them, I plan to use an inner wrapper for each column, pushing the content away from the edges.

<div class="pure-g">
  <div class="pure-u-1-3 col">
    <div class="col-wrapper">
      <div class="myblock">content col 1</div>
    </div>
  </div>

  <div class="pure-u-1-3 col">
    <div class="col-wrapper">
      <div class="myblock">content col 2</div>
    </div>
  </div>

  <div class="pure-u-1-3 col">
    <div class="col-wrapper">
      <div class="myblock">content col 3</div>
    </div>
  </div>
</div>

The .col-wrapper is given some padding, but finding a neat solution that works consistently across all column counts has been challenging. My initial approach:

.col > .col-wrapper {
  padding-left: 1rem;
  padding-right: 1rem;
}

/* Overwrite padding for precise left alignment */
.col:first-child > .col-wrapper {
  padding-left: 0;
}

/* Overwrite padding for precise right alignment */
.col:last-child > .col-wrapper {
  padding-right: 0;
}

However, with this CSS, the first and last columns end up with different sizes due to the varying padding.

I've made further adjustments in an attempt to achieve uniform gaps, which almost worked but introduced discrepancies in other areas.

.col > .col-wrapper {
  padding-left: .67rem;
  padding-right: .67rem;
}

/* Overwrite padding for precise left alignment */
.col:first-child > .col-wrapper {
  padding-left: 0;
  padding-right: 1.34rem;
}

/* Overwrite padding for precise right alignment */
.col:last-child > .col-wrapper {
  padding-right: 0;
  padding-left: 1.34rem;
}

Despite these efforts, I am currently at an impasse. How would you suggest adjusting the layout to achieve consistent gaps?

Thank you for your support :-)

Answer №1

Utilizing PureCSS for the creation of a multi-column layout. While knowledge of this framework is helpful, it is not essential as my current issues are unrelated to it.

The challenge you're facing stems from your reliance on purecss, which limits your ability to explore solutions using fixed percentage-based columns.

An alternative approach would be to achieve equal width columns without purecss by utilizing flexbox instead (check out this example: http://codepen.io/imohkay/pen/gpard).

 <div class="equalHMWrap eqWrap">
    <div class="equalHM eq">boo <br> boo</div>
    <div class="equalHM eq">shoo</div>
    <div class="equalHM eq">clue</div>
 </div>

CSS:

.eqWrap {
    display: flex;
}
.equalHMWrap {
    justify-content: space-between;
}
.equalHM {
    width: 32%;
}

Finding a solution within the constraints of purecss for varying numbers of columns may prove challenging. Adjusting padding percentages based on column count can be complex, but with careful calculation, you can create specific css rules for each scenario.

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

designing a button with eye-catching CSS3 styles

Although I struggle with CSS, I managed to create a button that looks pretty good. However, it doesn't have the same shine as the buttons on Yahoo Mail or the buttons found at Check out my demo on Fiddle: http://jsfiddle.net/karimkhan/y6XGg/ I wou ...

Effortlessly Styling Children Elements in Emotion JS When Parent Element is Hovered

This particular query seems to have been posed and resolved in various ways, but the solutions I've come across either do not pertain to Emotion or the responses related to Emotion haven't yielded results for me. I am currently using @emtion/[ema ...

Concealing the submit button until a file has been selected

How can I make the submit button invisible until a file is selected? <form action="upload.php" method="POST" enctype="multipart/form-data"> <input type="file" name="imageURL[]" id="imageURL" /> <input type="submit" value="submi ...

Users using an iPhone are unable to adjust the scale or scroll on this website

I find myself wondering what I might be overlooking in this situation Here is the HTML code: <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="shortcut icon" href="../../nImg/favicon.ico" ...

Concealing scroll bars while still maintaining the ability to scroll by using overflow:scroll

Similar Question: How to hide the scrollbar while still allowing scrolling with mouse and keyboard I have created a sidebar for a web application that needs to enable users to scroll through it without displaying a scrollbar. The content is 500px high ...

Designing a visually appealing widget in a jQuery UI theme by floating two elements neatly inside

My code looks like this: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Testing PasteHTML.co ...

Revolutionary CSS and jQuery for an Exceptional Top Navigation Experience

I would like to create a top navigation similar to the one on Facebook, using CSS and jQuery. Here is an example: Additionally: Notice how the arrow in the popbox always remains beneath the selected navigation item, and all popboxes have the same width. ...

I am having trouble getting Google Maps to load

Why does the map on my website only load when the browser window is resized? Below is the JavaScript code being used: <div class="map-cont"> <div id="map" class="location-container map-padding" style="width:100%;height:400px;background:y ...

Is the scaling of a div with an image affected by odd pixel sizes?

My goal is to create a responsive grid with square images, where the first image is double the size of the others. While I can achieve this using jQuery, I am curious if there's a way to accomplish this purely with CSS. Grid: Here's an example o ...

Is it possible to apply a tailwind class that fades or transitions into something else after a specific duration?

How can I achieve a transition effect from the bg-red-300 class to bg-transparent, or a different background class, over a 2-second duration? Do I need to use javascript for this effect? I would like an element to be highlighted and then return to its no ...

Issue with excessive content causing scrolling difficulty

I've created CSS for a modal dialog, but I'm facing an issue. When the content exceeds the vertical space of the wrapper, the scrolling functionality doesn't work correctly. Although I can scroll, it's limited and I can't reach th ...

Clicking on an absolute HTML element will instantly scroll back to the top of the page

Working on a website, I've designed a custom menu that is hidden with 0 opacity and z-index -1. When a button is clicked, the menu will appear on the screen. You can visit to see the site in action. The issue I'm facing is that every time I cl ...

Integrating dynamic PHP echo strings from a database into an established CSS layout

I am currently exploring PHP and MySQL databases, and I have managed to develop a basic search engine for my website. However, I am facing an issue that I am struggling to resolve: Is there a way to integrate the PHP echo output (search results) into the ...

The footer refuses to adhere to the bottom of the page

On the Contact Page of our client's website, there seems to be an issue with the footer not sticking to the bottom of the page and hiding the submit button. https://i.stack.imgur.com/txqAS.png We attempted to fix this using CSS, but it either sticks ...

I'm currently working with Bootstrap 5. Is there a technique available to reposition the elements within a div without relying on padding or margin properties?

Developing a Website: <!DOCTYPE html> <html lang="en" class="h-100"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" ...

Aligning a navigation bar with a hamburger menu in the center

I recently implemented a hamburger menu with some cool animations into my site for mobile devices. Now, I am facing the challenge of centering the menu on desktop screens and it's proving to be tricky. The positioning is off, and traditional methods l ...

Divs gracefully appear one after the other in a sequential manner

I am attempting to showcase a sequence of 4 divs in a row using the ease-in CSS transition feature. I came across this example that demonstrates what I am aiming for: http://jsfiddle.net/57uGQ/enter code here. However, despite my best efforts, I am unable ...

Generating a Popup Alert with a DIV

Looking at this instance: HTML / CSS Popup div on text click I am curious about how to have this display on the main page upon initial visit to the website. I prefer it to load automatically without requiring a button click, and once the content is read, ...

Tips for creating unique Styles for H1, H2, and H3 with MaterialCSS by utilizing createTheme

In my application built with NextJS and styled with MaterialCSS, I have created two themes: a dark theme and a light theme using the following code: import { createTheme } from '@mui/material/styles'; export const darkTheme = createTheme({ pal ...

Unusual CSS anomalies encountered on iPhone devices

Having an issue with the animation on my homepage where the logo spins into place. It works fine on desktop and in Chrome and Firefox mobile simulators, but on actual phones it rotates an additional 90 degrees. Included are screenshots from the Firefox mob ...