What is the best way to make grid-column-start and span work seamlessly together?

I'm having some trouble with creating a table using the grid function in CSS. Specifically, I am stuck on how to add dynamic categories at the top without hardcoding styles for each category count.

I have tried using the grid-column-start property to start the categories on column 2 and span 2 columns, but it's not working as expected.

Does anyone have any suggestions on how to solve this issue? Thank you.

.wrapper {
  display: grid;
  grid-template-columns: repeat(5, 100px);
}

.box {
  background-color: #444;
  color: #fff;
  outline: 2px solid #000;
  padding: 5px;
}

.category {
  grid-row: 1;
  grid-column-start: 2 / span 2;
}

.header {
  grid-row: 2;
}


/*hacks to manually place grid boxes*/

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

.category2 {
  grid-row: 1;
  grid-column: 4 / span 2;
}

.row1 {
  grid-row: 3;
}

.row2 {
  grid-row: 4;
}
What I want:
<p>
  <div class="wrapper">
    <div class="box category1">Category</div>
    <div class="box category2">Category</div>

    <div class="box header">#</div>

    <div class="box header">header1a</div>
    <div class="box header">header1b</div>

    <div class="box header">header2a</div>
    <div class="box header">header2b</div>

    <div class="box data row1">1</div>

    <div class="box data row1">data1a</div>
    <div class="box data row1">data1b</div>

    <div class="box data row1">data2a</div>
    <div class="box data row1">data2b</div>
  </div>
</p>
<p></p>
What I have:
<p>
  <div class="wrapper">
    <div class="box category">Category</div>
    <div class="box category">Category</div>

    <div class="box header">#</div>

    <div class="box header">header1a</div>
    <div class="box header">header1b</div>

    <div class="box header">header2a</div>
    <div class="box header">header2b</div>

    <div class="box data">1</div>

    <div class="box data">data1a</div>
    <div class="box data">data1b</div>

    <div class="box data">data2a</div>
    <div class="box data">data2b</div>
  </div>
</p>

Answer №1

An example of incorrect syntax is:

grid-column-start: 2 / span 2;

The correct format for this property is to specify a grid line, like so:

grid-column-start: 2;

This rule applies to all four line-based placement longhand properties:

  • grid-row-start
  • grid-row-end
  • grid-column-start
  • grid-column-end

Reference link: https://www.w3.org/TR/css3-grid-layout/#line-placement


The shorthand versions of these properties are:

  • grid-row
  • grid-column

Since these shorthand properties encompass both -start and -end properties, they can accept two values.

For instance:

  • grid-column: 2 / span 2

is considered valid and translates to:

  • grid-column-start: 2
  • grid-column-end: span 2

Reference link: https://www.w3.org/TR/css3-grid-layout/#placement-shorthands

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

Conceal a division based on its numerical position

I'm having trouble figuring out how to hide multiple divs based on an index that I receive. The code snippet below hides only the first div with the id "medicalCard", but there could be more than one div with this id. document.getElementById("medical ...

Utilize the display flex property within a nested flex container

I can't seem to get my link text centered both vertically and horizontally, it keeps aligning to the left. I was under the impression that using flex-based alignments would solve this issue. I have gone through the information on using a flex item as ...

Is it possible to open one collapse item and simultaneously close all the others using Bootstrap 5?

Can someone explain how to properly use the Bootstrap 5 collapses component? I want it so that when one collapse item opens, all others close. The documentation mentions setting a parent attribute, but I'm not entirely sure how that works. Here's ...

Utilizing Javascript / jQuery to eliminate specific CSS styles

I am facing an issue with the CSS code for a table positioned at the bottom of the screen. The current code includes a filter specifically for IE 8, but I need to make it compatible with IE 10 as well by removing the filter and adding a background color. ...

Tips for setting up Greasemonkey to automatically click on an unusual link on a particular website

I'm not a master coder by any means; I can handle some scripts but that's about it. Treat me like a total beginner, please! ;-) I want to automatically expand two specific links on a webpage using a GM Script. On a French dating site in the prof ...

Is there a way to make a TABLE expand to match the height of its surrounding element? (or, tackling sluggishness in IE with JavaScript)

I am facing a challenge with a web page where I have a table nested inside of a TD tag. Despite the unconventional approach, I need to ensure that the height of the nested table matches the height of the TD cell containing it upon page load. Currently, I a ...

Is it normal that my website isn't scrollable on an iPhone?

I have a hunch that the issue is related to the CSS, or perhaps it could be due to including the parallax JavaScript library. You can view the site at Below is the SASS code snippet: @import "compass/css3"; /* Sticky footer styles ---------------------- ...

Discover the underlying element that is being blurred when clicked

Is there a method to determine which element triggered the blur event when clicked outside? I want to update a ui-select value to match what the user typed in if they click outside of the dropdown. However, if they click on a dropdown item, I want to disc ...

MaterialUI Box reigns supreme in the realm of background images

In my React component, I have a form that is structured like this: <Box display="flex" justifyContent="center" alignItems="center" style={{ minHeight: "100vh", backgroundColor: "gray", opacity: "0.8" }} > ... </Box> ...

Using CSS3 to Enhance the Look of Multiple Background Images

While I've come across similar inquiries, I haven't found a satisfactory explanation yet. My aim is to grasp the best CSS practices for implementing multiple repeating backgrounds. Essentially, I want a background image to repeat across the entir ...

Troubleshooting issues with jQuery background-position animation and CSS sprites functionality

Trying to add animation to my navigation links background using a Css sprite. Check out the picture here: $(document).ready(function(){ $('nav a') // Move the background on hover .mouseover(function(){ $(this).stop().animate({ba ...

Combining D3.js tooltip positioning with CSS overflow styling

I am encountering an issue with displaying tooltips in D3 when combined with CSS overflow. Is there a solution available for this particular problem? CSS code: #chart1 { overflow-x: auto; overflow-y: hidden; max-width: 800px; max-height: 22 ...

How can you refresh the .replaceWith method in jQuery?

Is there a way to reset the .replaceWith function so that the html, css and javascript elements are restored to their original state? I am looking to have an icon replace the text when the user clicks on "contact", and then have the text return when the u ...

Issue encountered when integrating AJAX with PHP and HTML5 form

I've been working on solving a puzzle involving a contact form that uses AJAX and PHP. I decided to try reverse engineering the PHP code from this page (). However, when testing it out, the message gets stuck at "sending message...." after clicking "S ...

Tips for concealing a tab upon selecting an option from a dropdown menu

<ul class="nav nav-tabs"> <li class="active"><a data-toggle="tab" href="#song">Song</a></li> <li id="image-tab"><a href="#image" data-toggle="tab">Image</a></li> </ul> <div class="tab-cont ...

A cookie designed to store and retain the preferred CSS stylesheet choice

I'm looking to create a cookie that will store the preference for a specific CSS stylesheet. I currently have a function in place that allows me to switch between stylesheets: function swapStylesheet(sheet){ document.getElementById('pagestyl ...

Enhance your form input-group in Bootstrap-4 by adding a stylish border and background-color through validation

I'm working on a form using Bootstrap 4 and I need to display validation errors. Specifically, I want the text field with an input-group for the password to have the same styling as the Username field when there is an error (i.e., red border and backg ...

What sets npm node-sass apart from npm sass?

Recently, I discovered that the recommended approach is to utilize @use rather than @import in sass. However, it appears that using npm sass instead of npm node-sass is necessary for this. Can you clarify the distinction between these two? Also, why do @ ...

Change the P element to a Span element while maintaining the existing content using the replaceWith

I'm having trouble replacing a p tag with a span while keeping the actual content intact. Can anyone assist me with this, please? $('.anim-letters p').replaceWith( "<span class='letters'>" + $('this').text() + "< ...

The function will not be triggered when the form is submitted

I've set up this form to send data to the server-side. It's built in HTML/CSS with AngularJS as well. I made sure that the button is placed inside the form, a common mistake that can cause issues. However, despite my efforts, the function "onAddE ...