Search code examples
htmlcssuser-interfacewebfrontend

How can I create a gradient on a rectangular div with repeating lines at a 45 degree angle in opposite directions?


I want to create a gradient with lines moving in both directions, but at a 45 degree angle. with html and css. this is what I managed to produce How can I do the same lines, but in an opposite direction.

This is the code I used below to produce the image above.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

<style>
  .repeating-linear {
  background: repeating-linear-gradient(
    -45deg,
    black 4px,
    transparent,
    transparent 10px
  );

}
</style>

</head>
<body>
    <div class="repeating-linear" style="width: 200px;height: 50px; border: 1px solid red;">

    </div>
</body>
</html>

Solution

  • So you want cross lines, you can stack gradients as proposed below:

    <div class='repeating-linear' />
    
    .repeating-linear{
      width: 200px;
      height: 50px; 
      border: 1px solid red;
      background: 
        repeating-linear-gradient(
          -45deg,
          black 4px,
          transparent,
          transparent 10px
        ),
        repeating-linear-gradient(
          45deg,
          black 4px,
          transparent,
          transparent 10px
        );
    }
    

    jsfiddle sample: https://jsfiddle.net/4rL7n8xo/

    result: enter image description here