Is it possible to have a variable number of dynamic CSS Grid columns with the option of including a header or footer

Implementing a dynamic number of columns with CSS Grid is a breeze.

.grid {
    grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
}
// These .cells are beautifully aligned
<div class="grid">
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
</div>

However, what about more intricate layouts that include a header and/or footer?

// This approach does not work
.grid {
    grid-template-columns: 1fr, repeat(auto-fit, minmax(100px, 1fr)), 1fr;
}
<div class="grid">
    <header></header>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <footer></footer>
</div>

It seems that achieving this dynamic nature without creating a new grid by wrapping the cells in a container is a challenge, which goes against the purpose of adopting grid in the first place.

Using template areas also does not provide a solution as the header and footer are optional.

Is it feasible to achieve this dynamic behavior with CSS Grid?

UPDATE
After some exploration, it appears that achieving this layout is indeed not possible using CSS Grid alone. I ended up using JavaScript to modify a CSS custom property to update the grid columns dynamically.

ANSWER
The example by Paulie_D only serves to highlight the issue rather than resolve it.

Essentially, achieving a dynamic layout with CSS Grid is not feasible in most scenarios, as specific columns need to be defined. This limitation somewhat diminishes the advantages of using CSS Grid in my opinion.

// HTML
<div class="grid">
    <header></header>
    <div class="cell"></div>
    <div class="cell"></div>
    <div class="cell"></div>
    <footer></footer>
</div>

// CSS
.grid {
    grid-template-columns: 1fr 1fr 1fr;
}
header,footer {
    grid-span: 1 / -1;
}

Answer №1

It seems like there may be some confusion regarding your issue, let me clarify:

If you are looking to have a header and footer that span the full width of the page, with a 3 column grid in between, you can achieve this by setting the <header> and <footer> elements' grid-column property to span 1 / -1. This way, you are explicitly specifying that you want them to occupy a width extending from the first column to the end of the last one.

Answer №2

In the realm of CSS Grids, achieving this is not an option.

CSS Grid is designed to divide columns based on specified rules, or evenly distribute length/height if rules are not provided.

If you wish to include a header, footer, or other static blocks, it's recommended to wrap them in a separate container.

Within this separate container, you can create a new grid with specific rules for the header/footer as needed.

Can this be accomplished without JavaScript?

Absolutely, CSS alone is sufficient for styling and arranging grids.

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

Unable to modify SVG color to a white hue

I am currently working on an Angular project where I am utilizing SVG to display some icons. These icons originally have a default grey color, but I want them to change to white when hovered over. Interestingly, while changing the color to red, green, or y ...

I'm working on using CSS to design a layout similar to the one shown in the image below, experimenting with flexbox to achieve the

https://i.sstatic.net/4kNAd.png I am currently facing an issue with the layout of my navigation bar, as it is displaying in a column instead of a row like the format shown in the image. I have tried using flexbox and setting the display to inline-block in ...

Is there a way to create a multi-page website simulation using jQuery?

My current project involves creating a single page website, but I am looking to give the illusion of a multi-page site by using CSS/jQuery to hide and reveal different sections when specific links in the navigation menu are clicked. Here is the code snipp ...

Discovering the quantity of pagination tabs in Selenium WebAutomation

A critical element in the table "ContentPlaceHolder1_gvChannelList" is Pagination, located at the last row. I need to determine how many pages/columns are present in this pagination table. The code driver.findElements(By.id("//*[@id='ContentPlaceHold ...

Stop the execution of javascript code based on the screen width

On my website, I have two menus located in different sections of the page. One menu is shown when the screen width is greater than 555px, while the other menu appears when the screen width is less than or equal to 555px. Although the menus are essentially ...

Reveal hidden elements once the form has been submitted

At this moment, the info, stat and foo elements are hidden. Strangely, when I submit the form, they don't become visible. However, if I incorporate the unhide() function within the <button onclick="unhide()"></button>, it works p ...

Dynamically adjust the image link in responsive mode to improve user experience

Is there a way to dynamically change the image links in responsive mode on WordPress? I am displaying thumbnails with a fixed size of 280*200, but I want to switch to medium-sized thumbnails (480*200) when the resolution is between 480px to 600px. Is there ...

Bootstrap5: Left-aligned Navigation Bar Pills and Right-aligned Text

I am trying to align all my navigation pills to the left, and then add a single text element that stays at the end of the navbar even when the page is resized. Navbar Image My attempt involved adding a div so that the navbar pills would take up 50% width ...

Clicking on an absolute HTML element will instantly scroll back to the top of the page

Working on a website, I've designed a custom menu that is hidden with 0 opacity and z-index -1. When a button is clicked, the menu will appear on the screen. You can visit to see the site in action. The issue I'm facing is that every time I cl ...

Guide to altering the color of the row that is clicked in a table using css and jquery

Is there a way to change the text color of a row in a table when it is clicked, similar to an example like this but without changing the background color? The code I have tried is not working for me. Below is a snippet of the code I am using: $("#myTab ...

Issue with displaying PDF files on Google Chrome due to a software glitch

Recently, I encountered a puzzling issue on Google Chrome. I am not sure if the error lies with Google or within my code. When I set the div as display: none; and then show it again, the PDF view only shows a grey background. However, if I zoom in or out, ...

Utilizing Vue.js i18n for a multi-line text display

Currently, I am implementing i18n single file component in order to provide translation support for my application. In order to achieve this, I have been utilizing the i18n tag as shown below: <i18n> { "fr": { "text": "Lore ...

beforeprinting() and afterprinting() function counterparts for non-IE browsers

Is there a way to send information back to my database when a user prints a specific webpage, without relying on browser-specific functions like onbeforeprint() and onafterprint()? I'm open to using any combination of technologies (PHP, MySQL, JavaScr ...

showcase every value upon submission in the form with options to edit and delete

Is it possible to display all values submitted in a form with edit and delete buttons? Currently, only one value is being displayed at a time. Whenever a new value is displayed, it replaces the old one. How can this be fixed? You can fin ...

MailChimp consistently applies the "color: inherit !important" style to links, causing them to appear blue when viewed in Outlook

No matter how hard I try, MailChimp insists on styling any <a> tag with an href attribute to have color: inherit !important; text-decoration: inherit !important. This is causing my links to appear blue and underlined in Outlook, which is quite frustr ...

An unsolicited border encompassing an image that does not resemble a CSS border or outline within the realm of Bootstrap 4

Utilizing Bootstrap4 Cards with .PNG image sources has resulted in an unexpected border appearing. It's not a border or outline issue. https://i.sstatic.net/KHgnP.png Here is the HTML <div class="row"> <div class="col-md-3"> <d ...

What is the best way to specifically target header elements within article elements using a CSS selector?

As someone who is not well-versed in CSS, I am looking to target specifically the header elements within article elements. I have some understanding of selectors such as #, ., or ,. article header article, header article.header article#header I believe t ...

Tips on incorporating HTML element tags within a JSON value

I have successfully bound JSON data to an HTML table using Ajax and JavaScript in my project. The table has four headers: S.no, name, year, and download link. The first three columns are syncing and working correctly without any errors. However, for the la ...

Extract script from webpage using Python and Beautiful Soup

In the process of scraping 5 different shopping websites, I've successfully gathered valuable data from StackOverflow and YouTube. However, I have encountered a roadblock with one particular website. After utilizing various methods such as AJAX, Googl ...

Tips for moving a title sideways and adjusting the bottom section by x pixels

I am seeking to accomplish a design similar to this using CSS in order to create a title of <h1>DISCOVER <span>WEB 3</span> DIMENSIONS</h1>. How do you recommend achieving this? Thank you, I have searched on Google and asked frie ...