Search code examples
htmltwitter-bootstrapcssweb-frontend

CSS Scaling Specified Pixels to Dynamic


body {
  margin: 0;
  background-color: #2196F3;
}

.outer {
  height: 100vh;
}

.inner {
  width: 500px;
  height: 1000px;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
  background-color: #FF5722;
}
<div class="outer">
  <div class="inner"></div>
</div>

How fit inner div with specific pixels to the screen height or width with CSS keeping ratio the same?


Solution

  • Like this?:

    body {
      margin: 0;
      background-color: #2196F3;
    }
    
    .outer {
      height: 100vh;
      width: 100%;
    }
    
    .inner {
      width: calc(100% - 40px);
      height: calc(100% - 40px);
      background-color: #FF5722;
      margin-top: 20px;
      margin-left: 20px;
    }
    <div class="outer">
      <div class="inner"></div>
    </div>