Creating a CSS grid layout comprising of 40x40 boxes: A step-by-step guide

I'm trying to create a 40 x 40 grid of boxes with outlines that can be fully displayed on the screen without requiring scrolling. However, my current method causes the grid to exceed the screen size and forces me to scroll.

If the screen size changes, I want the individual box sizes to adjust accordingly, and I want the grid to completely fill the div it's contained in.

/* container styling */
.grid-container {
  position: absolute;
  margin: 10px;
}

/* individual box styling */
.node {
  width: 40px;
  height: 40px;
  outline: 1px solid #0f0e0e;
  display: inline-block;
}
<div class="grid">
  <div>
    <div class="node"></div>
    // .... repeated 40 times
    <div class="node"></div>
  </div>
  // ....repeated 40 times
  <div>
    <div class="node"></div>
    // .... repeated 40 times
    <div class="node"></div>
  </div>
</div>

It's essentially a 30 x 30 grid, and I'd prefer to achieve this using only CSS.

Answer №1

In this example, we have a grid of size 20 x 20 implemented using the flex display for both columns and rows. The main element sets a specific height and acts as an external container that limits the grid's dimensions, while allowing it to take up all available horizontal and vertical space.

The CSS code snippet below demonstrates how the grid structure is created with the specified heights and flex properties:

main {
  height: 150px;
}

.grid {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.row {
  flex: 1;
  display: flex;
}

.node {
  outline: 1px solid #0f0e0e;
  flex: 1;
}

The corresponding HTML markup is structured to create the grid layout by repeating the 'node' elements within 'row' elements inside the 'grid', ultimately contained within the 'main' element.

Answer №2

Are you in need of a similar solution?

.layout {
   padding: 12px; 
}

/* defining the individual element */
.section {
    display: block;
}

.element{
  margin: 0px;
  display: inline-block;
  height: 60px;
  width: 60px;
  border: 2px solid black;
}
<div class="layout">
   <div class="section" >
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
   </div>
   <div class="section" >
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
   </div>
   <div class="section" >
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></div>
      <div class="element"></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

Error: Unable to execute fields.map function while generating a dynamic sitemap using next-sitemap module

I'm in the process of creating a dynamic sitemap for my website and here's the code I'm using: import { GetServerSideProps } from 'next'; import { getServerSideSitemap, ISitemapField } from 'next-sitemap'; import { makeSl ...

I am looking to adjust/modulate my x-axis labels in c3 js

(I'm specifically using c3.js, but I also added d3.js tags) I have been working on creating a graph that displays data for each month based on user clicks. However, I am facing an issue where the x-axis labels show 0-X instead of 1-X. For instance, ...

issue with the alteration of table row colors

I am facing an issue with my database and a table called status. In this table, a status of 0 signifies unread, while a status of 1 represents read. The goal is to have rows with a status of 0 appear lime green and rows with a status of 1 show up as white. ...

What is the best way to assign a numeric variable to the value attribute of an HTML form input?

Is there a way to assign a numeric value to the value attribute of an HTML form input element using a variable? I understand that the value attribute automatically gets converted to a string. How can I make sure the variable is read before being converted? ...

Is iterating through object with a hasOwnProperty validation necessary?

Is there any benefit to using hasOwnProperty in a loop when an object will always have properties? Take this scenario: const fruits = { banana: 15, kiwi: 10, pineapple: 6, } for (let key in fruits) { if (fruits.hasOwnProperty(key)) { ...

What is a unique method for creating a wireframe that replicates the structure of a square grid without using interconnected nodes

Currently, I am in the process of designing the wire frame styles for human body OBJs and my goal is to achieve a wire frame similar to the image below. In the following lines, you will find the code snippets that illustrate how I create the wire frame alo ...

Content is cleared from the leaflet popup upon first closure

In my application, I've been working extensively with Leaflet and have a good grasp on the "leaflet way" of doing things. Since I have a single static layout for all my markers, I decided to create a simple Mustache template to insert variable data a ...

Convert all page links to post requests instead

Currently, I am working on a JavaScript project where my objective is to transform all links on the page into forms. This will enable the requests to be sent using the POST method rather than the GET method. The code I have implemented so far is as follow ...

Ways to filter out specific fields when returning query results using Mongoose

I was wondering about the code snippet below: Post.create(req.body) .then(post => res.status(201).json(post)) .catch(err => res.status(500).json(err)) While this code works perfectly, I am curious about excluding a specific field, such as the __v fi ...

Enhance Fullcalendar by incorporating an HTML tag alongside the "titleformat" option

I am utilizing the jQuery Fullcalendar agenda feature. My main objective is to link an action with each date that appears in the calendar and enable the opening of a separate window for each date. Therefore, I am looking to include a button or a link next ...

Struggling to implement the UI router into my Angular Framework

I've been working on a framework that is supposed to be router agnostic. While I've managed to make it work with ngRoute, I just can't seem to get it functioning with UI Router. Here's a snippet of the main app module: (function () { ...

HTML and JavaScript: Struggling to include multiple parameters in onclick event even after escaping quotes

I am struggling to correctly append an HTML div with multiple parameters in the onclick function. Despite using escaped quotes, the rendered HTML is not displaying as intended. Here is the original HTML code: $("#city-list").append("<div class='u ...

In older versions of Internet Explorer like IE6 and IE7, the jQuery ui Accordion may not function properly, but it works

I have implemented two accordions on my webpage, each with customized CSS in a separate file to prevent any conflicts. However, the accordions are not displaying correctly and instead show all content at once as if the accordion styling is missing. Both ac ...

The issue with the functionality of position absolute in CSS when used with JavaScript

<html> <head> <style> #wrapper{ position: absolute; top : 250px; width: 500px; height: 500px; border: 1px solid red; } .tagging{ position: absolute; border: 1px solid black; width : 20px; height: 30px; } & ...

Merge JavaScript function mouseup

I've been trying to combine JS functions, but it's not working as expected. Can anyone provide some suggestions for my code? $(document).ready(function(){ $(".searchs").keyup(function() { var searchbox = $(this).val(); var dataString = ...

AngularJS tree grid component with customizable cell templates

I have been utilizing the tree-grid component in AngularJS from this link: Here is an example of it on Plunker: http://plnkr.co/edit/CQwY0sNh3jcLLc0vMP5D?p=preview In comparison to ng-grid, I am unable to define cellTemplate, but I do require the abilit ...

Troubleshooting issue with v8 callback failure when interacting with JavaScript objects stored on the heap

Creating an instance of a JavaScript object is easy, for example; myObj1 = new myObject("Object1"); Next step is to call a node.js c++ addon which triggers a callback to the Javascript object... myAddon = require('myAddon'); myAddon.greet(myOb ...

How can I indicate to the browser that the HTML page should be accessed through SSL?

I've set up a certificate on my website. Now, I only want one specific page to be accessed through SSL. Do I need to add any markup on the page itself, or is there another setting elsewhere that I should use? ...

Trouble with integrating jQuery Mobile data-role page into a PHP project

Why am I encountering issues when trying to link to the data role page <div data-role="page" id="sessionrecordsuccess"> in the main.php file? Instead of directing me to the specific section, it keeps redirecting to the top of main.php. I have another ...

Using Jsoup: Converting a String with HTML into an XHTML file

Looking for a way to achieve this using Jsoup without involving files? I've come across examples that use byte arrays and file outputs, but all I need is a valid XHTML document to work with itext PdfWriter and XMLWorker. Any suggestions? ...