What is the best way to evenly distribute items in nested CSS Grid Columns?

Check out this JSFiddle link for a simple Google.com recreation.

I'm attempting to organize the top navigation bar by creating separate containers for About, Store, Gmail, and Images. The issue seems to be related to the justify-items:center; property on the container class.

Here is an image depicting the desired layout: View Image Example Here.

HTML:

<div class="container">

<div class = "nav-container">
<nav class = "nav-container-grid">
<a class = "nested-a">About</a>
<a class = "nested-b">Store</a>
<a class = "nested-c">Gmail</a>
<a class = "nested-d">Images</a>
</nav>
</div>

<div id ="image-container">
<img src='https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' alt='Google'>
</div>

<div class="button-container">
<button>Google Search</button>
<button>I'm Feeling Lucky</button>
</div>

<div class="search-container">
<input type="search" placeholder="Search">
</div>

</div>

CSS:

.container {
  display: grid;
  grid-template-columns: 1;
  grid-template-rows: 1fr 2fr 1fr 1fr;
  justify-items: center;
}
.nav-container {
  grid-row: 1;
}

.nav-container-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
}

.nav-container-grid a {
    border: 2px solid #000000;
    text-align: right;
}

.image-container {
  grid-row: 2; 
}
.search-container {
  grid-row: 3;
}

.button-container {
  grid-row: 4;
}

.nested-a {
  grid-column-start: 1;
  grid-row-start: 1;
}
.nested-b{
  grid-column-start: 2;
  grid-row-start: 1;
}
.nested-c{
  grid-column-start: 3;
  grid-row-start: 1;
}
.nested-d{
  grid-column-start:4;
  grid-row-start: 1;
}


Answer №1

When setting your .nav-container-grid to split into 4 equal columns, you may encounter an issue where the columns resize based on the content of the largest column when applying justify-items:center.

To resolve this issue, ensure that the parent of .nav-container-grid has a width of 100%:

.nav-container { grid-row: 1; width: 100%; }

Example Code:

.container {
  display: grid;
  grid-template-columns: 1;
  grid-template-rows: 1fr 2fr 1fr 1fr;
  justify-items: center;
}

.nav-container {
  grid-row: 1;
  width: 100%;
}

.nav-container-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
}

.nav-container-grid a {
  border: 2px solid #000000;
  align-content: right;
}

.image-container {
  grid-row: 2;
}

.search-container {
  grid-row: 3;
}

.button-container {
  grid-row: 4;
}

.nested-a {
  grid-column-start: 1;
  grid-row-start: 1;
}

.nested-b {
  grid-column-start: 2;
  grid-row-start: 1;
}

.nested-c {
  grid-column-start: 3;
  grid-row-start: 1;
}

.nested-d {
  grid-column-start: 4;
  grid-row-start: 1;
}
<div class="container">

  <div class="nav-container">
    <nav class="nav-container-grid">
      <a class="nested-a">About</a>
      <a class="nested-b">Store</a>
      <a class="nested-c">Gmail</a>
      <a class="nested-d">Images</a>
    </nav>
  </div>

  <div id="image-container">
    <img src='https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png' alt='Google'>
  </div>

  <div class="button-container">
    <button>Google Search</button>
    <button>I'm Feeling Lucky</button>
  </div>

  <div class="search-container">
    <input type="search" placeholder="Search">
  </div>

</div>

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

Cease the audio from playing unless you are actively hovering over the image

Is there a way to make the sound stop playing when you are not actively hovering over the picture? Any suggestions on how to achieve this would be greatly appreciated! imgarr[i].onmouseout=function(){ this.setAttribute('src',this.getAttribu ...

Can PHP retrieve data when the form submit function is overridden with AJAX?

I have customized the .submit function of a form on this webpage. It is being loaded inside the #mainContent in an "index.php" and I want the Submit button to only replace this #mainContent. My goal is to retrieve data from this form and send it to a .php ...

The subdirectory containing the Rails assets is causing loading issues in the production environment

My event CSS is currently located in app/assets/stylesheets/pages/events.scss. Surprisingly, everything looks perfect in development mode with all the assets loading properly. However, the real issue arises when I switch to production as it seems like thes ...

Leveraging HTML div elements for forms can greatly enhance the user

I am in the process of developing a website where users can upload images, name them, and then submit the data to PHP code. Currently, I am using plain HTML and PHP for this project, but I intend to integrate the Bulma CSS library. The current HTML code l ...

Leveraging Affix in Bootstrap version 2.3.2

Hey there, I'm currently working on building a sidebar similar to the one you can find here. The issue I'm facing is that my sidebar overlaps with the footer at the bottom of the page. What I need is for the sidebar to move up when the user scrol ...

Selenium WebDriver: The challenge of using visibilityOfElementLocated when the element is not actually visible

I've encountered an issue with the Firefox Webdriver where it fails to accurately determine if an element is visible. Here is the code I am using, which incorrectly indicates that the element is visible: wait.ignoring(UnhandledAlertException.class).u ...

jquery-ui allowing to resize child divisions as well

Currently, I am in the process of developing a website that includes resizable divs with jQuery. However, I have encountered an issue where only the width of the child divs is being resized when I adjust them. For reference, you can view the site and its c ...

Submitting a jQuery Userlike form will not result in a page change

I'm facing a challenge with posting a form without the page refreshing. Despite various suggestions on SO, none of the methods seem to work for my specific case. The use of jQuery's ajax or post APIs is not an option as they change the encoding ...

Tips for modifying the text color of radio button choices in HTML and CSS

I have set a black background image for my HTML page. How can I change the radio button labels/texts to white, similar to the questions? This is the code snippet that shows how it currently looks on the page: View Image <hr&g ...

Is it possible for you to generate an array containing only one element?

I've encountered an issue with my code. It functions correctly when the JSON data for "Customers" is in array form. However, if there is only one customer present, the code erroneously creates 11 table columns (corresponding to the number of keys in t ...

Could HTML code be inserted into the <Head> section of a NextJS page?

I need to add meta tags retrieved from the backend for a certain page to the Head section of my page component. In React, I know that I can incorporate HTML using <div dangerouslySetInnerHTML={{__html: response.someHtml}} />. Can this same method b ...

A method for calculating the product of two selected numbers and then adding them together

I am currently working on a form that includes two select options; one for logo design and another for letterhead design. Users should be able to choose the quantity of each design, or both if they wish. For example, a user could select 2 logo designs and ...

Having difficulty generating a footer for a page that includes a Material-UI Drawer component

Looking to create a standard full-width footer at the bottom of my page, I need help with the implementation. Utilizing the "Permanent drawer" from Material-UI Drawer for reference here. If you're interested in experimenting with it, CodeSandbox link ...

What is the easiest way to modify the color of a basic PNG image in a web browser?

While working on a website project, I encountered instructions for mouseover styles in the design that got me thinking: It's straightforward to achieve with javascript or css image swapping... but here's the catch. There will be multiple icon li ...

How about trying out some dropdowns?

Following the issue I mentioned in my previous question titled Margin-Right on CSS not working, I am currently engaged in a creative endeavor to redesign my school's website homepage. The progress so far has been significant, but I now require some as ...

Substitute the website address using regular expressions

Looking to update the iframe URL by changing autoplay=1 to autoplay=0 using regular expressions. jQuery(document).ready(function($){ $('.playButton').click(function(){ $('.flex-active-slide iframe').contents().f ...

Email communication not being successfully transmitted

I've been attempting to add a contact form to my website, but unfortunately, it's not functioning as expected. Following this tutorial, I replaced the $myemail variable with my email address and gave it a try. While I do receive the "thank you" m ...

Discovering the following element containing an ID attribute with jQuery

Upon clicking a link, my goal is to locate the subsequent <section> with an ID attribute and retrieve its ID. In the provided code snippet and JavaScript function, the expected outcome upon clicking the link is for "section_3" to be logged in the co ...

Is there a way to ensure that the images in the slider all remain consistent in size?

I recently created a slider using bootstrap and encountered an issue with image display on mobile screens compared to laptop screens. On a Laptop screen: On a Mobile screen: The images on the mobile screen are not fitting properly within the slider, and ...

How can you convert an epoch datetime value from a textbox into a human-readable 24-hour date format

I have an initial textbox that displays an epoch datetime stamp. Since this format is not easily readable by humans, I made it hidden and readonly. Now, I want to take the epoch value from the first textbox and convert it into a 24-hour human-readable da ...