Grid element not displaying properly as intended

I'm having trouble centering the content of item in the middle column, it just won't budge.

.home-grid-container {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: 0.10fr 0.98fr auto;
  height: 100vh;
}

.home-header {
  grid-column: 1 / span 3;
  grid-row: 1 / span 1;
  background: #3f51b5;
}

.home-main {
  grid-column: 1 / span 3;
  grid-row: 2 / span 3;
  background: #81d4fa;
}

.item {
  grid-column: 2 / span 1;
  grid-row: 2 / span 3;
}

.home-footer {
  grid-column: 1 / span 3;
  grid-row: 5 / span 1;
  background: #3f51b5;
  div {
    text-align: center;
    margin: 2vh;
  }
}
<div class="home-grid-container">
  <div class="home-header">
    <h1>
      <img src="/src/imgs/sitelogo.png" />
    </h1>
  </div>
  <div class="home-main">
    <div class="item">
      Simple, Fast, Powerful
      <input type="button" value="100% Free" />
    </div>
  </div>
  <div class="home-footer">
    <div>All Rights Reserved</div>
  </div>
</div>

https://css-tricks.com/snippets/css/complete-guide-grid/

Answer №1

The elements you wish to center are descendants of the grid container, but not direct children.

Since grid layout only applies to parent-child relationships, the element with the class .item is not within that scope and will not respond to grid properties.

However, these elements are inline-level children of a block container, so using text-align: center will achieve the desired centering effect.

.home-grid-container {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: 0.10fr 0.98fr auto;
  height: 100vh;
}

.home-header {
  grid-column: 1 / span 3;
  grid-row: 1 / span 1;
  background: #3f51b5;
}

.home-main {
  grid-column: 1 / span 3;
  grid-row: 2 / span 3;
  background: #81d4fa;
}

.item {
  grid-column: 2 / span 1;
  grid-row: 2 / span 3;
  text-align: center;    /* NEW */
}

.home-footer {
  grid-column: 1 / span 3;
  grid-row: 5 / span 1;
  background: #3f51b5;
}
<div class="home-grid-container">
  <div class="home-header">
    <h1>
      <img src="http://i.imgur.com/60PVLis.png" width="50" height="50" alt="">
    </h1>
  </div>
  <div class="home-main">
    <div class="item">
      Simple, Fast, Powerful
      <input type="button" value="100% Free" />
    </div>
  </div>
  <div class="home-footer">
    <div>All Rights Reserved</div>
  </div>
</div>

jsFiddle demo

Answer №2

To utilize the grid for a child element within your container, simply inherit the same attributes.

.home-grid-container {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: 0.10fr 0.98fr auto;
  height: 100vh;
}

.home-header {
  grid-column: 1 / span 3;
  grid-row: 1 / span 1;
  background: #3f51b5;
}

.home-main {
  grid-column: 1 / span 3;
  grid-row: 2 / span 3;
  background: #81d4fa;
  
  /* inherit the container-grid setup */
  display: grid;
  grid-template-columns: inherit;
  grid-template-rows: inherit;
}

.item {
  grid-column: 2 / span 1;
  grid-row: 2 / span 3;
}

.home-footer {
  grid-column: 1 / span 3;
  grid-row: 5 / span 1;
  background: #3f51b5;
  div {
    text-align: center;
    margin: 2vh;
  }
}
<div class="home-grid-container">
  <div class="home-header">
    <h1>
      <img src="https://dummyimage.com/200x50/cccccc/ffffff.png" />
    </h1>
  </div>
  <div class="home-main">
    <div class="item">
      Simple, Fast, Powerful
      <input type="button" value="100% Free" />
    </div>
  </div>
  <div class="home-footer">
    <div>All Rights Reserved</div>
  </div>
</div>

Answer №3

It has been noted by others that because the item element is not a direct child of the grid container, you cannot apply grid properties to it.

To address this issue, one potential solution could be to extract the item from the home-main div and make it a direct child of the grid. However, it appears that this may not be a feasible solution in this particular case.

The Grid Layout Module Level 2 introduces Subgrids which are designed to resolve this specific problem. While Subgrid is currently only a draft spec, in your scenario you might use the following approach:

.home-main {
  display: subgrid;
  grid-column: span 3;
}

Alternatively, there is another method to achieve the desired outcome:

display: contents (caniuse)

According to Caniuse:

display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. This can be useful when a wrapper element should be ignored when using CSS grid or similar layout techniques.

To ensure that the grid placement properties apply to the item, you can simply add display: contents; to home-main (currently supported in Firefox):

.home-main {
  display: contents;
  ...
}

(Please note that this will override the grid properties on home-main, but it is unnecessary for placing the item)

.home-grid-container {
  display: grid;
  grid-template-columns: auto;
  grid-template-rows: 0.10fr 0.98fr auto;
  height: 100vh;
}

.home-header {
  grid-column: 1 / span 3;
  grid-row: 1 / span 1;
  background: #3f51b5;
}

.home-main {
  /*grid-column: 1 / span 3;
  grid-row: 2 / span 3; */
  display: contents;
  background: #81d4fa;
}

.item {
  grid-column: 2 / span 1;
  grid-row: 2 / span 3;
  background: salmon;
}

.home-footer {
  grid-column: 1 / span 3;
  grid-row: 5 / span 1;
  background: #3f51b5;
}

.home-footer div {
    text-align: center;
    margin: 2vh;
  }
<div class="home-grid-container">
  <div class="home-header">
    <h1>
      <img src="/src/imgs/sitelogo.png" />
    </h1>
  </div>
  <div class="home-main">
    <div class="item">
      Simple, Fast, Powerful
      <input type="button" value="100% Free" />
    </div>
  </div>
  <div class="home-footer">
    <div>All Rights Reserved</div>
  </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

Showing headings in the table vertically

I have a header1 and header2 with corresponding data1 and data2 that I want to display differently. h h e e a a d d e e r r 1 2 data1 data2 To enhance the presentation, I wish to add borders around the head ...

Guide to positioning a border with CSS

How can I align a border to the right in this code? This is the CSS snippet that I currently have: p { text-align: right; color: #757575; Background-color: #FFFFFF; font-size: 2.4em; width: 50px; } <p>Home<br> <a href="aboutu ...

Excessive image display | HTML and CSS synergize

I am having some trouble with my image gallery. Each image is displayed as a vertical column and has zooming effects when hovered over. Clicking on an image should show it in full screen with a caption. The problem arises when dealing with images that are ...

Why does HTML validation matter after the webpage has finished loading?

After coming across an article recommending a workaround for a method that isn't considered strictly valid (using target="_blank") by using JavaScript to apply the rules after the page has loaded, I began contemplating if this approach is ethical. It ...

Is it possible for HTML/CSS to automatically adjust content size to fit or fill a container?

I'm interested in finding a CSS-only technique that can ensure content within a container scales to fit, regardless of whether it's text or images. Take a look at the example below: .container { display: inline-block; width: 2in; hei ...

Transferring HTML information to Flask

I'm struggling with passing a value from a text box in a web application to a Flask application. Despite my efforts, the request object in Flask does not seem to contain the data I need. Can anyone provide assistance with this issue? Below are the rel ...

Is it possible to switch between different fabricJS canvases seamlessly?

Consider this scenario where I have three canvas elements: <canvas id="c1" width="400" height="300"></canvas> <canvas id="c2" width="400" height="300"></canvas> <canvas ...

"jQuery's .each() method is only iterating through the last element in

I am encountering an issue with this function not operating correctly... only the last Element shows the box. NOTES: <aside> is set to position: fixed; and I understand that this is not the "correct" use of <article> tags, but it helps me dist ...

Discovering the destination URL that an HTML button submits to

While attempting to scrape data from instacart.com, I encountered the requirement of entering a zip code to determine the displayed information. My intention is to utilize Python requests to submit a random zip code. However, I am unsure of which URL to po ...

Auto play feature malfunctioning in Onsen-UI Carousel attribute settings

When utilizing onsen UI in my mobile web application, I encountered an issue with the autoplay property not functioning properly within the carousel. <ons-page> <ons-toolbar> <div class="left"> <ons-toolbar-button on ...

Bypassing CSS rules for elements not present in the DOM

In case an element is absent from the DOM, I want to include margin space. Conversely, if the element is present, no margin space should be added. I have experimented with CSS for this purpose: A - To ensure there is no margin-top if the H2 title is displ ...

Clicking on the ng-repeat will trigger the ng-click event, which populates all the data using ng

I need help including an HTML page using ng-click within ng-repeat. However, it is currently loading all the content for every ng-repeat element. My specific requirement is to only bind(ng-include) the clicked element. Please see the attachment for m ...

Interactive image rotator featuring navigation buttons for next and previous slides

I recently followed a tutorial from W3Schools Now, I am looking to enhance it by adding previous / next buttons for the indicators, rather than for the slider itself Here is what I aim to accomplish: https://i.sstatic.net/qH1PQ.png Below is the code sn ...

Positioning Elements at the Bottom with Bootstrap 5

Here are the codes you requested: <div class="d-flex"> <h1>Hello World</h1> <p class="ms-auto small">Stupid Text</p> </div> <hr class="my-0"> I am attempting to vertically ali ...

The dragging and dropping feature for HTML input type="file" is not functioning properly in Edge and IE11

I've developed an Angular app using this sample where I have included only an input control on the screen. Within my project, I constructed a custom file-uploader component. While I am able to drag and drop files in Chrome, I encountered issues with d ...

Is there a way to use JavaScript to choose options within a <select> element without deselecting options that are disabled?

Here's the code snippet I am working with at the moment: <select id="idsite" name="sites-list" size="10" multiple style="width:100px;"> <option value="1" disabled>SITE</option> ...

Arranging Multiple Files in Sequence Using HTML5 File API Instead of Uploading All Simultaneously

I am currently working on a BackboneJS/Marionette App and I want to enable users to upload multiple files. Right now, the functionality works when users select multiple files simultaneously, but I would like to give them the option to select one file init ...

What is the best way to create an HTML form on-the-fly from a JSON object?

Could someone please assist me in understanding how to dynamically generate an HTML form based on a JSON object using JavaScript? ...

Inspect every div and perform an action if the criteria are met

I need assistance with adding text inside a specific div based on certain conditions. Currently, all the div elements are being populated with the added text. Please review my code for more clarity on what I am trying to achieve. Can you suggest the most e ...

Displaying colors using Javascript

When using node.js to create an HTML file from a js file, I am encountering an issue where the colors are not displaying in the output. I have generated hex values for the colors, but they do not appear in the HTML file as expected. var format; function ...