In this article, we'll explore the process of evenly distributing grid columns in relation to

I have implemented two types of layouts using the display grid feature. Check out the screenshot

However, I noticed that the first columns in both layouts are not equal in width.

The width of the first column in Layout 1 is: 239.40px

Whereas, the width of the first column in Layout 2 is: 240.70px

Is there a way to set them equally using the fraction unit (fr)?

Below you can find the code snippet:

.grid-container {
  display: grid;
  grid-gap: 25px;
}

/* News Module 1 */
.grid-container--news1 {
  grid-template-columns: 2fr 1fr 1fr 1fr;
}

.grid-container--news1 .grid-item:nth-of-type(1) {
  grid-row: 1 / 4;
}

.grid-container--news1 .grid-item:nth-of-type(2) {
  grid-column: 2 / 4;
}

/* News Module 2 */
.grid-container--news2 {
  grid-template-columns: 1.3fr 1fr 1fr;
}

.grid-container--news2 .grid-item:nth-of-type(1) {
  grid-row: 1 / 3;
}

.grid-container--news2 .grid-item:nth-of-type(2) {
  grid-column: 2 / 4;
}

.grid-item {
  background: steelblue;
  color: 
<div class="row gx-3 gx-md-4">
        <div class="col-12 mb-3">
          <h3>News Module 1</h3>
          <div class="grid-container grid-container--news1">
            <?php for ($i=1; $i < 7; $i++): ?>
            <div class="grid-item"><?php echo $i; ?></div>
            <?php endfor; ?>
          </div>
        </div>

        <div class="col-12">
          <h3>News Module 2</h3>
          <div class="grid-container grid-container--news2">
            <?php for ($i=1; $i < 5; $i++): ?>
            <div class="grid-item"><?php echo $i; ?></div>
            <?php endfor; ?>
          </div>
        </div>
      </div>
    </div>

Answer №1

It seems unlikely that achieving equal spacing using fr units will be successful.

Rounding errors could occur, and since your gap sizes are fixed in absolute px units, the two layouts will not have the same amount of free space.

One solution could be to set the width of the first column as a percentage, which may result in both layouts aligning properly.

Alternatively, you can calculate the desired width based on the container's width minus the gap sizes.

For example, in the initial grid, we have:

2fr + 3*25px + 3fr = 100%

The width of the first column can be calculated in CSS as

calc((100% - 75px) * 2 / 5)

You can apply this calculation for the width of the first column in both layouts, keeping the remaining fr settings unchanged.

Answer №2

Here's a simple trick to adjust column widths using "%" instead of "fr".

Instead of this

.grid-container--news1 {
  grid-template-columns: 2fr 1fr 1fr 1fr;
}

/* News Module 2 */
.grid-container--news2 {
  grid-template-columns: 1.3fr 1fr 1fr;
}

You can do this:

.grid-container--news1 {
  grid-template-columns: 40% 1fr 1fr 1fr;
}

/* News Module 2 */
.grid-container--news2 {
  grid-template-columns: 40% 1fr 1fr;
}

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

Utilize a pair of drop-down menus to concurrently filter a data table

I am interested in implementing two select option filters on a datatable at the same time. Currently, they function well when individually selected, but do not work together for filtering by two variables simultaneously. Currently, selecting a second filt ...

Issues with Contenteditable functionality in JavaScript

My goal is to make a row editable when a button is clicked. $(":button").click(function(){ var tdvar=$(this).parent('tr').find('td'); $.each(tdvar,function(){ $(this).prop('contenteditable',true); }); }); <s ...

Using Angular Ionic 3 to apply the disabled attribute

I have a situation in my main.ts where I need to disable a textarea in the HTML if an object is initialized. I've attempted various methods like: ng-attr-disabled="!myObj"; ng-attr-disabled="{myObj!= null}"; and also directly using ng-disabled. I e ...

What is the best way to modify a React icon that has been imported using styled components?

Using the styled component library to customize my website's style. I want to make adjustments to my imported react icon component. <i class="fab fa-facebook"></i> It is simple to modify this using normal CSS by targeting the &l ...

How can I set a background image to automatically adjust to the width of the window, be full height, allow for vertical scrolling, and

How can I set a full-screen background image that adjusts to the body width without horizontal scrolling, maintains height proportionate to width, and allows for vertical scrolling? Here is my current code: html, body { margin: 0px; padding: 0px; } bo ...

Dealing with Bootstrap 3 compatibility issues in IE8: Finding solutions for mobile-like behavior

Currently, I am in the process of completing this website at . Most aspects of the site display properly on all standard browsers, however, there seems to be an issue with IE8. It appears as if the layout is behaving like a mobile view, with all containe ...

Is there a way to modify or alter the layout specifically for mobile devices?

Currently, my desktop version displays the image first followed by content (h1, p, button), and then repeats in that order. However, I would like to restructure the HTML for the mobile version to make it more user-friendly. How can I achieve this without d ...

Ways to structure a div to resemble a table

I'm currently working on a form that has multiple input fields, and I'm looking to structure them in a specific way. The layout I want to achieve can be seen in this example: https://jsfiddle.net/dmilos89/58ea82gj/2/ The example above utilizes ...

Ways to stop Content from Showing below a div container

Hey, I just created a login panel using CSS and HTML. After applying some floats, all the content ended up displaying under the panel. I attempted to use clear: both, but that didn't resolve the issue. My goal is to have all other contents displayed a ...

Click a button to show or hide text

I am attempting to create a button that will toggle text visibility from hidden using 'none' to visible using 'block'. I have tried the following code but unfortunately, it did not yield the desired outcome. <p id='demo' s ...

What is the best way to create a "tile-based board" for this game?

I am working on a project to create a board made up of tiles, but I am facing difficulty in filling up the entire board area. Currently, I have only managed to create 1 column of the board, as shown in the image. Can someone guide me on how to fill the ent ...

"Revolutionize your layout with Bootstrap 4's cutting-edge inline grid

Can anyone help me figure out how to get these input fields to display in the same line? I'm using Bootstrap 4 but can't seem to make it work. Here is how I want it to look: enter image description here Here is my HTML code: <body> &l ...

The layout is disrupted due to an input field with a label in Semantic UI

My goal is to create an input field with a right-aligned label in Semantic UI. The documentation provides an example of a cornered label that functions correctly, but when attempting to use a regular label, the layout of the input field gets disrupted. To ...

How can I make a div using jQuery fade in when an input field is in

I want to create a tooltip that changes its innerHTML and fades in when an input on my form is focused. Additionally, I need to check if the tooltip has been faded in as it initially loads faded out. In essence, I am looking to switch the tooltip content ...

Tips for creating a seamless Bootstrap collapse effect

I have noticed a slight flicker in the content collapse when using this code. Is there a way to make it collapse more smoothly? <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"> <script ...

Limit Content to be contained within a Bootstrap Container/Div

How can I shrink the content inside a container without distorting images? When the screen size decreases, all the images and content inside start to lose their alignment and sizes begin to change. I am aware that this may make the content harder to see. ...

Stylish slanted divs achieved using Bootstrap 4

I'm currently in the process of constructing a block with 6 slanted divs using Bootstrap. Take a look at the image provided below: https://i.sstatic.net/vvg0b.png After attempting to use rows and columns, I noticed that they weren't aligning co ...

Content Security Policy directive violation: Chrome extension policy error occured due to refusal to execute inline event handler

I've been working on a chrome extension to perform a simple task, but I've hit a roadblock with one line of HTML code that's causing issues with setting the correct permissions. Despite my efforts, I'm stuck on what exactly needs to be ...

Issue with Bootstrap columns being misaligned on mobile devices

On my website, I've implemented a Bootstrap Grid design with three columns under the container-fluid BS attribute, along with .row .no-gutters. While the boxes are perfectly aligned in the desktop view, they stack vertically on mobile and tablet devi ...

The CSS on the IIS 7 server is not functioning properly, unlike when it is on a

Upon debugging my ASP MCV 4 Application on Visual Studio 2012, I have encountered an issue with CSS not displaying properly after deploying it to IIS 7. It seems that some styles are missing in the live environment compared to how they appeared locally. I ...