What causes variations in layouts when using the same CSS grid code across different pages?

I've encountered an issue where the CSS on two pages, with slight color changes, is behaving differently in terms of grid layout. Despite multiple attempts to troubleshoot by adjusting items, grid-template-layout, and other parameters, the issue persists. The only solution that worked was copying and pasting the code from the functioning page to the non-functioning page. I've exhaustively gone through everything, but can't pinpoint the source of the discrepancy between the two pages.

The desired outcome is 4 columns with equal widths.

However, the current state is as follows: one page displays the desired layout, while the other has uneven column widths - with the first column being larger, the second medium, and the last smaller. Additionally, when adding columns, the attributes of the largest/medium/smaller columns switch randomly.

.the-grid {
  padding-bottom: 20px;
  max-width: 1640px;
  margin: auto;
}

.the-grid-layout {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-gap: 60px;
  -webkit-align-items: top;
  -webkit-box-align: top;
  -ms-flex-align: top;
  align-items: top;
  max-width: 1640px;
  margin: 0 auto 120px;
  position: relative;
}

.item {
  position: relative;
}

.image-cont {
  background-size: cover;
  width: 100%;
  height: 50px;
  margin-bottom: 20px;
  background-repeat: no-repeat;
}

.heading-cont {
  background-color: rgb(255, 255, 255);
  text-align: center;
  padding: 30px 20px;
  transition: all 0.15s ease-in-out 0s;
}
<section class="the-grid">
  <div class="the-grid-layout">
    <div class="item">
      <a href="/content-url">
        <div class="image-cont" style="background-image:url(http://via.placeholder.com/350x150);"></div>
        <div class="heading-cont">
          <h5 class="heading">Item Title</h5>
        </div>
      </a>
    </div>
    <div class="item">
      <a href="/content-url">
        <div class="image-cont" style="background-image:url(http://via.placeholder.com/350x150);"></div>
        <div class="heading-cont">
          <h5 class="heading">Item Title</h5>
        </div>
      </a>
    </div>
    <div class="item">
      <a href="/content-url">
        <div class="image-cont" style="background-image:url(http://via.placeholder.com/350x150);"></div>
        <div class="heading-cont">
          <h5 class="heading">Item Title</h5>
        </div>
      </a>
    </div>
    <div class="item">
      <a href="/content-url">
        <div class="image-cont" style="background-image:url(http://via.placeholder.com/350x150);"></div>
        <div class="heading-cont">
          <h5 class="heading">Item Title</h5>
        </div>
      </a>
    </div>
    <div class="item">
      <a href="/content-url">
        <div class="image-cont" style="background-image:url(http://via.placeholder.com/350x150);"></div>
        <div class="heading-cont">
          <h5 class="heading">Item Title</h5>
        </div>
      </a>
    </div>
    <div class="item">
      <a href="/content-url">
        <div class="image-cont" style="background-image:url(http://via.placeholder.com/350x150);"></div>
        <div class="heading-cont">
          <h5 class="heading">Item Title</h5>
        </div>
      </a>
    </div>
  </div>
</section>

Answer №1

An issue with utilizing fr is that it will utilize the available space and split it based on requirements.

If you desire each section to occupy precisely 1/4 of the space, opt for 25% instead of 1fr.

I trust this information proves valuable.

Additionally, remember to account for any spacing gaps.

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 transitioning from the current step to the previous step in the mat-stepper component, how can we emphasize the horizontal line that separates them?

screenshot of my progress I have progressed from A3 to A2 step, but I need to add a horizontal line between them. How can I achieve this and is it possible to do so using CSS styles? ...

Guide to transforming Tailwind CSS into a class and saving it in a CSS document

Recently, I implemented Tailwind CSS to style my application. It has been a helpful tool, but I am looking to consolidate all the CSS into one file for better organization. I want to keep it separate from the HTML code. My attempt to convert the following ...

Is the initial carousel element failing to set height to 100% upon loading?

If you take a look here, upon loading the page you will notice a DIV at the top. It is labeled "content" with "content_container" wrapped around it and finally, "page" around that. Clicking the bottom left or right arrows reveals other DIVs with similar ta ...

AngularJS and Bootstrap carousel combined for a dynamic multi-item per slide display

Currently, I am utilizing Bootstrap to showcase a carousel on my website, where multiple items are displayed per slide as demonstrated in this example. The use of static images has yielded satisfactory results, as evidenced by the jsFiddle example found he ...

What is the best way to show a dropdown menu on the right side of the

I developed a demo that functions properly on both desktop and mobile devices. However, I am facing an issue where the dropdown menu is not displayed when switching to the mobile version. Here is my code snippet: http://codepen.io/anon/pen/OyNLqa ...

Combining ASP.NET MVC with HTML5 for dynamic web development

We are in the process of planning a new project and are interested in utilizing ASP.NET with the MVC pattern. Additionally, we want to incorporate the new features of HTML5. However, we have been having trouble finding sufficient information or connecting ...

Is there a way to trigger the opening of a jQuery-UI-Selectmenu dropdown using the 'ENTER' key instead of the 'SPACE' key as indicated in the documentation?

I'm currently working on implementing jQuery-UI-Selectmenu to create a dropdown feature. As per the API documentation for select menu available at https://api.jqueryui.com/selectmenu/, it mentions that the 'SPACE' key can be utilized to ope ...

Mistakes found in the 'com/badlogic/gdx/backends/gwt/GwtApplication.java file while working on an HTML 5 project

Just recently, I discovered the amazing framework known as Libgdx. As a newcomer to this framework, I am encountering a few challenges. After cloning the entire repository and running the gdx-setup-ui.jar file, I obtained 4 basic starter projects (my-gdx-g ...

What are the best ways to position elements within a div?

In my small information div, I am looking to align the content on the right side. Specifically, 065 31 323 323, <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d4c4c1c0cbdfc4e5c2c8c4ccc98bc6cac8">[email protected]</ ...

jQuery: Issue Encountered with POST Request when Offline (iOS & Chrome)

After developing an HTML5 web application with offline capabilities using AppCache, the process flow is as follows: Online: While connected to the network, the app loads base information in advance. Offline: Users can take the app offline on their tablet ...

Personalizing CSS classes in ASP.NET

In the project I'm currently developing, users will have the ability to customize the font color of header titles. These header titles are styled using values from a cssClass, which includes properties such as font-size, font-color, font-weight, and ...

Tips for positioning a highcharts pie chart and legend in the middle of a page

I'm struggling to center my highchart data in a node/react single page application. Currently, it appears off-center like this: https://i.stack.imgur.com/ccR6N.png The chart is floating to the left and I would like to have everything centered within ...

Storing data in a text or HTML file using server-side JavaScript

Currently, I am working on a JavaScript form that involves saving user-entered variables to either a .txt file or a new webpage with the variables pre-filled in the inputs. I know that JavaScript cannot directly manipulate the user's machine, but I am ...

How to utilize CSS variable references across different files in React?

In my React project, I have integrated multiple variables that I would like to be able to access from other CSS files in order to maintain a unified codebase for specific UI configurations such as colors and buttons. However, I am uncertain whether these ...

Having trouble with submitting an HTML form using JavaScript and PHP

My webpage has an HTML form that sends data to a PHP file for processing. One issue I'm facing is with a dynamically generated combo box that works fine when the page loads, but the selected value is not being passed when the form is submitted. The J ...

The WebView.HitTestResult method is currently only receiving <img src> elements and not <a href> elements

I am attempting to open a new window in the Android browser using "_blank". I have set up an event listener for this purpose. mWebView.getSettings().setSupportMultipleWindows(true); mWebView.setWebChromeClient(new WebChromeClient() { ...

I'm wondering why myDivId.toggle() is functioning properly but myDivClass.toggle() is not working as expected

Using JQuery's toggle() function, I have been able to hide and show some DIVs successfully. Recently, I discovered relationships between certain DIVs that allowed me to group them into a class. I decided to try toggling the entire class rather than ...

A Guide to Utilizing Parameters in Javascript Strings

I'm in search of a solution but struggling to find a comprehensive explanation. I am working on a code where I need the flexibility to easily update strings or other parameters. What I aim for is passing values to a string when calling it through a ...

What is the best way to align text in the center of a div?

I just created this dropdown menu, but I am encountering an issue. Whenever I resize the div or h4 element, the text ends up at the top. Even after trying to solve it with text-align: center;, the problem persists. Here is a visual representation of what ...

What is the best way to create a text shadow effect for a heading element?

Is it possible to create a centered heading like the one in this image using only CSS, without using flexbox? [EDIT] Below is an example using flexbox. However, there are some limitations with this code as it doesn't allow reusing the same class for ...