skip to Main Content

When rotating a element inside a div, or fullscreen, if you know the dimensions you can make it wide or tall enough to hide it by making the inner element bounds. With responsive web design, this seem to be a issue when height/width is too far off each other where the diagonal becomes too long.

Here is a example of an rotating background pattern that works in some aspects, but once it goes too tall or too wide it breaks and green background color bleeds: Codepen

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: green;
}

.pattern {
  width: 200%;
  height: 200%;
  position: fixed;
  top: -50%;
  right: -50%;
  background-image: conic-gradient( white 0deg 45deg, black 45deg 90deg, white 90deg 135deg, black 135deg 180deg, white 180deg 225deg, black 225deg 270deg, white 270deg 315deg, black 315deg 360deg);
  animation: rotate 4s linear infinite;
}

@keyframes rotate {
  0% {
    transform: rotate(0);
  }
  0% {
    transform: rotate(1turn);
  }
}
<div class="pattern"></div>

3

Answers


  1. See this solution, max takes the highest value of 100vh or 100vw, adds it to a variable and adds it to the width and height, I made some other changes to make it work as expected:

    * {
        box-sizing: border-box;
        margin: 0;
        padding: 0;
     }
    body {
      width: 100vw;
      height: 100vh;
      overflow: hidden;
      background: green;
    }
    .pattern {
      --size: calc(max(100vw, 100vh) * 1.5);
      width: var(--size);
      height: var(--size);
      position: fixed;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      background-image: conic-gradient(
        white 0deg 45deg,
        black 45deg 90deg,
        white 90deg 135deg,
        black 135deg 180deg,
        white 180deg 225deg,
        black 225deg 270deg,
        white 270deg 315deg,
        black 315deg 360deg
      );
      animation: rotate 4s linear infinite;
    }
    @keyframes rotate {
      0% {
        transform: translate(-50%, -50%) rotate(0);
      }
      0% {
        transform: translate(-50%, -50%) rotate(360deg);
      }
    }
    <div class="pattern"></div>
    Login or Signup to reply.
  2. This stays responsive under normal conditions – if device isn’t narrow and super long. I have added @media for narrower width, otherwise I couldn’t get it to work better. I’m curious if there is a better solution to it!

    * {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
    }
    
    body {
      width: 100vw;
      height: 100vh;
      overflow: hidden;
      background: green;
      display: flex;
      justify-content: center;
      align-items: center;
    }
    
    .pattern {
      width: 200vw;
      min-width: 200vh;
      height: 200vw;
      min-heigth: 200vh;
      position: fixed;
     
      background-image: conic-gradient(
        white 0deg 45deg,
        black 45deg 90deg,
        white 90deg 135deg,
        black 135deg 180deg,
        white 180deg 225deg,
        black 225deg 270deg,
        white 270deg 315deg,
        black 315deg 360deg
      );
      background-size: cover;
      animation: rotate 4s linear infinite;
    }
    
    @media screen and (max-width: 640px){
      .pattern {
        width: 400vh;
        min-width: 400vh;
        height: 400vw;
        min-height: 400vw;
      }
    }
    
    @keyframes rotate {
      0% {
        transform: rotate(0);
      }
      0% {
        transform: rotate(1turn);
      }
    }
    <div class="pattern"></div>

    Hopefully it helps!

    Login or Signup to reply.
  3. Your code can be simplified like below. All you need a big negative value with inset

    body {
      background: green;
    }
    
    .pattern {
      position: fixed;
      inset: -200%;
      background: repeating-conic-gradient(white 0 12.5%, black 0 25%);
      animation: rotate 4s linear infinite;
    }
    
    @keyframes rotate {
      0% {
        transform: rotate(1turn);
      }
    }
    <div class="pattern"></div>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search