Creating flexible layouts with CSS grid that adjust dynamically based on the content

I want to utilize CSS grid to create a unique table design. The layout can be described as follows (refer to the image for visualization): https://i.sstatic.net/vj8iF.jpg

Here's what I need: the header cell should have a size of 2x / 2y, while each header row's cell is x / 2y and each header column cell is 2x / y. The internal table cell size (highlighted in red) should be x / y.

All elements except for the Header cell need to be dynamic. For instance, the data could result in 7 rows and 3 columns.

The challenge lies in making this table fully dynamic. Here's my attempt for the header column:

grid-template-rows: repeat(auto-fill, 145px);

And for the header rows, I've included:

grid-template-columns: repeat(auto-fill, 145px);

Despite these settings, something seems to be missing. How can I ensure that the table adjusts dynamically based on the data provided and adheres to the specified cell size limitations?

Answer №1

Although this may not have much practical use, I decided to experiment out of curiosity:

  • The CSS variables x and y from your question are retained,
  • Modify the grid-template-columns similar to your approach but with an additional column width in the first column like
    grid-template-columns: calc(var(--x) * 2) repeat(auto-fill, var(--x))
    ,
  • Set the initial row positioning using
    grid-template-rows: calc(var(--y) * 2)
    ,
  • Position the rows below the header using implicit grids - utilize grid-auto-rows: var(--y).

Check out the demo below:

:root {
  --y: 75px;
  --x: 75px;
}

.container {
  display: grid;
  grid-template-columns: calc(var(--x) * 2) repeat(auto-fill, var(--x));
  grid-template-rows: calc(var(--y) * 2);
  grid-auto-rows: var(--y);
}

.container > div {
  border: 1px solid red;
}
<div class="container">
  <div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><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

Why is there excessive whitespace appearing in Internet Explorer 9?

Visit mimictrading.com/index.php When viewed in Firefox, the website displays as intended. However, in IE9, there seems to be a strange space at the top and above the recent topics list. I suspect that it might be related to the 'header' divisio ...

What is the best way to customize the Bootstrap CSS for an <a> element?

I recently started a blog and wanted to streamline my design process by using Bootstrap to avoid having to deal with media queries. However, I encountered an issue where the <a> element was displaying as blue. I have another CSS file that is loaded a ...

Column Flow in CSS Grid: Maximizing Auto-Fit - How to Optimize Row Layouts?

Having some trouble understanding how auto-fill and fit work in grid layouts. I am familiar with basic CSS, but new to working with grid layouts. I have a set of checkbox items with labels that I want to display in columns depending on screen size, with c ...

`Cannot locate the CSS file on my local server`

My Node.js application uses the Express.js framework, and I am attempting to execute the following code: app.use('/public', express.static(path.join(__dirname,'public'))); However, I encountered the following error message: The CSS ...

Adjust the size of the input field as text is being typed in

Can a text input box be automatically resized as text is entered? I've been looking online but haven't come across any solutions. ...

Is there a possibility of encountering problems when collapsing table rows versus individual cells?

By using the css visibility property, you have the ability to set it to collapse for specific table rows. Nevertheless, there are two methods to accomplish this task. The first is to implement the logic on the <tr> element: thead > tr { visi ...

html5 - Safari browser: Hover effects for rounded images

Encountering an issue on my website with a video link here. The problem is specific to Safari. Below is the HTML code: <div class="item"> <div class="img-wrapper"> <img class='img'> </div> </div> An ...

Hover function not functioning properly

I can't figure out why this hover effect isn't working, there doesn't seem to be a negative z-index or anything like that. At most, it just flashes on hover; .menu{ border-radius: 50%; width: 100px; height: 100px; background: white; box-sha ...

Leveraging PHP to manipulate HTML elements

I have an example of a basic HTML page: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8> <title>Document</title> </head> <body> <p id="demo">Nothing to see here</p> & ...

What is the best way to display the press down (:active) effect before releasing the finger?

Is there a way to display the :active CSS effect on mobile devices while the user's finger is still touching the screen? I have noticed that the effect only shows after the finger is lifted, but I prefer the way Amazon's buttons display this effe ...

Insert a percentage sign into a right-aligned text box

I am trying to figure out how to permanently display a % symbol at the far right side of a textbox that shows a percentage. Can anyone help me with this? ...

Why isn't the virtual directory in IIS 7 pointing to the root website?

I need to set up a virtual directory in IIS 7 that will point to the root of a website on our server. Here is an example of the structure: Websites: | --- AssetsServer | - /images/ | - /css/ | - etc. | --- demoserver ...

Use Ajax to dynamically update the href attribute of an ID

I have a page with songs playing in a small audio player, and the songs change every 1-2 minutes or when I choose to (like a radio). Users can vote on each song using the following format: <span class='up'><a href="" class="vote" id="&l ...

In search of a solution to ensure equal width for all items in a 2x2 grid

I have a CSS grid with two rows and two columns (refer to the code snippet). Is there a way to make all the items in the grid maintain the same width without specifying it individually? Are there any css-grid properties that can ensure consistent widths ...

Can a website trigger an alarm on a user's iPhone without their permission?

Even though I have limited experience with HTML, CSS, and Javascript, I am curious if it is possible to create a basic website that can trigger an alarm on the user's device by simply clicking a button. For example, imagine a scenario where a user is ...

Ensure a button is automatically highlighted within an active class script

I have created a set of buttons that allow users to change the font family of the text. I would like the border and text color to update automatically when a specific option is selected. Currently, the JavaScript code works well, but when the page first l ...

Utilizing AngularJS $anchorScroll for navigating to an anchor tag on a different page

On my AngularJS Home Page, I am trying to scroll to an anchor tag on another page. Both pages are loaded as partial html files into the ng-view component based on the URL. When I start the server, the home page loads and then I need to navigate to the FAQ ...

Exploration of jQuery Dropdown Menus

Encountering a problem with my dropdown menu. Check out the code here: http://jsfiddle.net/xY2p6/1/ It seems like a simple issue that I can't figure out, but as shown in the link, the functionality is not working properly. I need help linking the hid ...

Issues with connecting static files in express.js

Hey there! I'm having an issue with the redirect not working when I click the "Submit" button on the login page. The code in my index.js file looks like this: app.use(bodyParser.urlencoded({ extended: true })); app.use(express.static(path.join(__dir ...

Is it possible to manipulate the originating hyperlink of a remote page to make it appear as if it originated from a different page?

Imagine having a website: www.example.com/test.php $source = $_SERVER['HTTP_REFERER']; There is a site that links to yours. www.randomsite.com/external1.html <a href="www.example.com/test.php">Link</a> And another one that does th ...