Search code examples
cssbackground-imageradial-gradients

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?


Solution

  • 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;
    }