Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a dotted grid with CSS

I want the whole body to have a dotted grid

body {
  background-image: radial-gradient(black 1px, transparent 0);
  background-size: 40px 40px;
}

The problem is that I want to shift it so that a dot is placed at the point (0, 0), i.e. the top left corner of the body. So the whole structure should be shifted at -20px, -20px. Or probably there is a better solution?

like image 695
Eugene Barsky Avatar asked Mar 26 '19 18:03

Eugene Barsky


People also ask

How do I create a grid pattern in CSS?

To get started you have to define a container element as a grid with display: grid , set the column and row sizes with grid-template-columns and grid-template-rows , and then place its child elements into the grid with grid-column and grid-row . Similarly to flexbox, the source order of the grid items doesn't matter.


2 Answers

Update

The following solution works, but only for a fixed background-size. See @Temani Afif's answer for a better solution.

Answer

You can use the background-position CSS property to do exactly what you were thinking.

body {
  background: white;
  background-image: radial-gradient(black 1px, transparent 0);
  background-size: 40px 40px;
  background-position: -19px -19px;
}
like image 138
Tim Klein Avatar answered Oct 24 '22 09:10

Tim Klein


Here is another way in addition to changing the background-position that may work whataver the size is:

body {
  background-image: radial-gradient(circle at 1px 1px, black 1px, transparent 0);
  background-size: 40px 40px;
}

body {
  background-image: radial-gradient(circle at 1px 1px, black 1px, transparent 0);
  background-size: 50px 30px;
}

Basically the idea is to change the position of the dots inside the area defined by background-size to the top/left instead of shifting all the background

like image 37
Temani Afif Avatar answered Oct 24 '22 09:10

Temani Afif