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

Get the QR code canvas image with a customized background by downloading it

I am having trouble with downloading a QR code canvas image with a background. The download works fine, but my device is unable to scan the code properly due to an incomplete border. I need to add a white background behind it to make it larger. Current im ...

Utilizing a background image in the slick slider

<div id="largeCarousel" style="display:inline-block; float:right;"> <div class="homepage-item" style="padding-bottom:52%; position:relative; box-sizing:border-box;"> <div id="largeCarouselContent" style="position:absolute ...

Initiate a new line within a flex item

<Popper id={"simplePopper"} open={this.context.loading} style={{display: "flex", alignItems: "center", justifyContent: "center", backgroundColor: 'red',opacity:'0.5',width:'100%',he ...

Combining Sass and SCSS in webpack: A step-by-step guide

Just curious about something. I have some libraries I'm interested in using (for example, font-awesome) that use scss, but I personally prefer working with sass. How can I set up my project with webpack to accommodate this? Here's my current s ...

How come "display:none" is still occupying space on the page?

Trying to figure out how to make a specific cell in a table disappear when viewing an HTML page on mobile devices, while having a width of 20% for desktop. However, the issue is that even though the table cell is invisible on mobile, it still occupies sp ...

CSS changes triggered by JQuery are ineffective

Having trouble modifying the CSS of a class in my HTML file. I've been struggling with this for quite some time and can't figure out what I'm doing wrong. (I've already attempted multiple versions, but nothing seems to work) Here' ...

Tips for utilizing an array within React and transforming it into a component

I've developed a website that pulls data from a SQL database I created, containing details such as name, address, and occupation of individuals. I successfully managed to showcase this information on the webpage by structuring an array and inserting t ...

What is the best way to eliminate the final border in a row that has been applied using the <

I have a table where the tr is styled with a border. I am looking to eliminate the border from the last td in the tr. Here is an example: <table> <thead> <tr> <th>a</th> <th>b</th> <th&g ...

Tips for implementing resizable Material-UI Dialogs

I'm working on a project that involves creating a dialog box that can be resized and dragged. While I found steps in the Material-UI Dialog documentation on how to make it draggable, I'm still looking for information on how to make it resizable. ...

Position the paper-icon-button so that it is aligned to the top-right corner of the scaffold

Looking for a way to... <core-scaffold id="core_scaffold"> <core-header-panel mode="seamed" id="core_header_panel" navigation flex> <core-toolbar id="core_toolbar"></core-toolbar> <core-menu valueattr="label" ...

Tips for successfully transferring an image through an XMLHttpRequest

I found this helpful resource at: I decided to test out the second block of code. When I made changes in the handleForm function, it looked like this: function handleForm(e) { e.preventDefault(); var data = new FormData(); f ...

What are the best techniques for resizing a Text Field with media queries?

The contact form is responsive and functioning correctly for the textarea field, but there are issues with the input type=text fields on smaller screens. I attempted to address this by incorporating media queries. Below is my code: <!doctype html> ...

The collapsible section expands upon loading the page

Trying to implement Disqus as a plugin on my test webpage, I created a collapsible section with a "Show comments" button. The idea was to hide the plugin by default and reveal it only when users click on "Show comments". Despite using the w3schools example ...

What is the best way to align the items in two lists at the center as if they were all individual

Q: How can I center the list-item elements of two unordered lists as if they were children of just one list? Example: {[1][1][1][1][2][2]} { [2][2] } CSS Alignment Challenge <div> <ul> &l ...

Modify the background color of a div by selecting a hex code from a dropdown menu

Is there a way to utilize jQuery in order to alter the background of a <div> by choosing the HEX code from a separate dropdown menu? HTML <select id="target"> <option value="#c2c2c2">Grey</option> <option value="blue">Bl ...

Transform HTML invoices into emails

I am currently working on developing an ERP system. I have an HTML invoice that needs to be converted into a PDF and sent via email. My question is regarding how to construct the invoice using jQuery & AJAX, convert it into a PDF format, and then send it ...

Limit text to two lines inside a cell in a table

Apologies for not providing any evidence of my own efforts. I'm looking to wrap text in a table cell, but limit it to just 2 lines. Expanding the width is not possible. Any suggestions on how to achieve this? ...

Troubleshooting Problems with CSS Three-Column Layout

Struggling with aligning domain names in a 3 column setup, I have been attempting this for some time now without success. I am new to this and could use some guidance. Here is the code: TPL file <div> <p class="center">{foreach key=num it ...

Attempting to preserve the CSS transform effect as I switch between menu options

Whenever I stop hovering over my element, it reverts back to its original position. However, I want it to remain in place when I am navigating through the "sousmenu" menu and only return to its original position when I am not hovering over the "sousmenu". ...

Exploring the world of sass with compound mathematical statements

There's a slight issue that I'd like to circumvent. $var: 30px; font: 25px/25px font,font,font; //Works font: ($var - 5)/($var - 5) font, font, font; //Works not font: ($var - 5px)/($var - 5px) font, font, font; //Works no ...