What is the best way to implement grid-gap in the display:grid property?

I've added grid-gap to both classes, but the gap area is showing up in a different color than white. Additionally, the text is aligned at the top of each cell. How can I fix this?

1)

Even though I applied grid-gap to both classes, the gap area is visible, but it's not displaying as white; instead, it appears as [#96ceb4]. What steps should I take to resolve this?

2)

Despite using text-align:center, the text is positioned at the top of each cell. I would like the text positioning to be centered within every cell. How can I achieve this?

body {
  padding: 50px
}

.container div {
  text-align: center;
  color: #fff;
  font-size: 1.5rem;
}

.container div:nth-child(1) {
  background: #96ceb4;
}

.container div:nth-child(2) {
  background: #ff6f69;
}

.container div:nth-child(3) {
  background: #88d8b0;
}

.container div:nth-child(4) {
  background: #ffcc5c;
}

.container div:nth-child(5) {
  background: #96ceb4;
}

.container div:nth-child(6) {
  background: #ff6f69;
}

.one {
  display: grid;
  grid-template-columns: 100px auto 100px;
  grid-template-rows: 60px 100px;
  grid-gap: 10px;
}

.two {
  display: grid;
  grid-template-columns: 100px auto;
  grid-template-rows: 60px 100px;
  grid-gap: 10px;
}
<div class="container">
  <div class="one">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
  </div>

  <br>

  <div class="two">
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
    <div>6</div>
  </div>
</div>

Answer №1

To modify the background color of the empty space to white, insert this CSS code:

.container {
background-color: white;
}

If you want to vertically center the text in each cell, use the following CSS rule:

.container div {
display: flex;
align-items: center;
justify-content: center;
}

Updated snippet:

.container {
background-color: white;
}

 body{padding:50px}
.container div {text-align: center; color:#fff;font-size: 1.5rem;}
.container div:nth-child(1) {background: #96ceb4;}
.container div:nth-child(2) {background: #ff6f69;}
.container div:nth-child(3) {background: #88d8b0;}
.container div:nth-child(4) {background: #ffcc5c;}
.container div:nth-child(5) {background: #96ceb4;}
.container div:nth-child(6) {background: #ff6f69;}

.one {
  display: grid;
  grid-template-columns: 100px auto 100px;
  grid-template-rows: 60px 100px;
  grid-gap:10px;
}


.two {
  display: grid;
  grid-template-columns: 100px auto;
  grid-template-rows: 60px 100px;
  grid-gap:10px;
}

.container div {
display: flex;
align-items: center;
justify-content: center;
}
To change the background color of the gap area to white, include the following CSS rule:

 <div class="container">
    <div class="one">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>5</div>
      <div>6</div>
    </div>

  <br>

    <div class="two">
      <div>1</div>
      <div>2</div>
      <div>3</div>
      <div>4</div>
      <div>5</div>
      <div>6</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

Run a function upon clicking a button in JavaScript

Could someone please help me figure out what I am doing incorrectly in the code below? I am attempting to trigger a function when a button is clicked. The HTML: <button id="btn1">Press me!</button> <input type="button" id="btn2" value="Pr ...

Swapping out for <br />

I am currently working on a feature that allows users to enter text into a textarea and send it as the body of an email. The challenge I am facing is automatically adding new lines when users press enter in the textarea. The mailing functionality works, b ...

Utilizing the Bootstrap grid system to dynamically adjust column width based on specific conditions

When working with HTML and bootstrap (3.7) CSS, I often come across this issue: <div class="row"> <div class="col-xs-6">Stretch ME!</div> <div class="col-xs-6">Conditional</div> </div> Sometimes the "Conditional" d ...

Implementing Google Apps Script code on my webpages

I am currently in the process of developing a web application using HTML, CSS, and JavaScript with Google Spreadsheet serving as my database. To turn this web app into a fully functional application, I am considering utilizing PhoneGap. After successfully ...

Exemplary CSS Text Alignment When Vertical Align is Exceptional

I'm currently working on a menu where some text needs to be aligned vertically with the property vertical-align:super. However, I am facing an issue where this particular item with "super" text is not aligning properly with the other items. Below is ...

Creating dynamic CSS backgrounds that scroll horizontally

Styling with CSS #container { min-width: 800px; max-width: 1000px; height: 100%; margin: 0 auto; text-align: left; } #left-section { height: 100%; position: relative; float: left; ...

What is the correct way to integrate a HTML/CSS/JS theme into a Vue project effectively?

As a newcomer, I recently acquired a bootstrap theme that comes with HTML, CSS, and JavaScript files. My goal now is to integrate this theme into Vue in order to make it fully functional. The challenge I am facing is how to successfully incorporate the the ...

Is there a way to automatically convert English numbers to Persian/Arabic as soon as a user types them into the input tag?

Currently, I am working with Vuejs and trying to hide the user's characters in an input tag and replace them with my own characters (specifically numbers). I have attempted using @onchange, Watch, as well as getters and setters within computed proper ...

Is there a way to utilize local resources in case CDNs are unable to load properly?

Encountering an issue with Bootstrap and jQuery loading from CDN, sometimes failing to import from the CDN source. For instance: <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYii ...

How can JavaScript be integrated with Django static URLs?

I have a Django application where I store static images on Digital Ocean Spaces. Displaying these static images in my template is simple:<img>{% static 'images/my_image.png' %}</img> When I inspect the HTML page after loading, I see ...

Gradually blend the section of the picture with an image banner after 20 seconds

I have a background image that rotates every 20 seconds, fading in and out. The images consist of a logo at the top-left corner and a person image at the right side from top to bottom. However, I want only the person image to fade in and out while keeping ...

Conceal the div once it reaches the top of the webpage

Is there a way to hide an "h1" element when its parent div reaches the top of the page and then show it again when it moves down using both JQuery and CSS? I attempted to utilize a scroll listener in JQuery to toggle a class, but this resulted in the "h1" ...

Guidance on incorporating static files with Spring MVC and Thymeleaf

I'm seeking guidance on how to properly incorporate static files such as CSS and images in my Spring MVC application using Thymeleaf. Despite researching extensively on this topic, I have not found a solution that works for me. Based on the recommenda ...

Insert the <span> element within the <a> element with the help of Jquery

I am attempting to insert a span tag inside .block-leftnav ul li .parent a, however, instead of adding the tag after only the .parent a, jQuery is adding the span tag after every href tag on my page. Below is the code I am using to achieve my desired outc ...

Creating a Sticky Header and Footer with Responsive Design: Ensuring Content Div Expansion between Them

Greetings. I am working on creating a simple responsive HTML layout as described below: HTML: <div id="header"></div> <div id="content"></div> <div id="footer"></div> CSS: #header{ wi ...

ScrollReveal.js won't function as intended

https://i.stack.imgur.com/SOQEk.png Looking to utilize the popular ScrollReveal.js created by Julian Lloyd (ScrollReveal) Despite following instructions from various sources, the script is not producing the intended effect. Steps taken to make it work: ...

The JavaScript require() function appears to be malfunctioning

Why am I encountering an error when trying to import a valid node_module? <script > var Twit = require('twit'); </script> Error: Uncaught ReferenceError: require is not defined I am puzzled as to why the require() function wor ...

Lost: pointer for speech bubble

Here is the HTML code I have written: <!DOCTYPE html> <html> <head> <style type="text/css"> #box { width: 200px; height: 250px; border-radius: 15px; background-color: pink; ...

Alignment problem with email signatures on MAC devices

Recently, I designed a straightforward email signature and successfully copied and pasted it into the appropriate section. However, while it works perfectly in Outlook, there is an issue with MAC Mail (Version 7.3) where the image extends beyond the table. ...

Using jQuery to modify the CSS of a div located outside of an iframe

Currently, I am developing a straightforward script. Firstly, here is all of the code that I have: <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script type="text/javascript"> function SuperWebF1() { $("#outerd ...