'How to deal with size of HTML element not adjusted while transforming/scaling (with CSS) to create "zoom/scroll panel"?

I would like to create a "zoom/scroll" panel, i.e., a div with fixed size with another div inside, the content of which I can scale/zoom and scroll.

In order to do so, I have used CSS's transform function "scale". The issue is that the div I would like to scale does not really change its size. The visible content is smaller (in case I am scaling down) but the div around it (with the scroll bars) still behaves as if the scaled-down div had not changed its size. It just gets "surrounded" by a transparent border/area.

Eventually, I solved this problem by "translating" the content to the top left and by embedding it in another div that has the size I would expect after the "scaling". However, this does seem like I unsatisfactory to me. Is there a better solution?

Here is the initial scenario (HTML + CSS only) before scaling the content (in "div3"):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <style>
      body {
        height: 100vh;
        width: 100vw;
        padding: 0px;
        margin: 0px;
        background-color: gray;
      }
      .div1 {
        height: 100%;
        width: 100%;
        padding: 0px;
        margin: 0px;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
      }
      .div2 {
        height: 400px;
        width: 400px;
        padding: 0px;
        margin: 0px;
        flex-shrink: 0;
        overflow: auto;
        background-color: red;
      }
      .div3 {
        position: relative;
        top: 0px;
        left: 0px;
        height: 600px;
        width: 600px;
        padding: 0px;
        margin: 0px;
        background-color: mediumblue;
        font-size: 36px;
        font-family: Arial, Helvetica, sans-serif;
        color: white;
        font-weight: bold;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="div1">
      <div class="div2">
        <div class="div3">
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Tempora vel,
          atque voluptate, doloribus, illum asperiores omnis ea mollitia eveniet
          aut sit dignissimos praesentium cumque error. Commodi amet at eaque
          eos?
        </div>
      </div>
    </div>
  </body>
</html>

Here is the scenario after the scaling with the effect that I do not want to have:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <style>
      body {
        height: 100vh;
        width: 100vw;
        padding: 0px;
        margin: 0px;
        background-color: gray;
      }
      .div1 {
        height: 100%;
        width: 100%;
        padding: 0px;
        margin: 0px;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
      }
      .div2 {
        height: 400px;
        width: 400px;
        padding: 0px;
        margin: 0px;
        flex-shrink: 0;
        overflow: auto;
        background-color: red;
      }
      .div3 {
        position: relative;
        top: 0px;
        left: 0px;
        height: 600px;
        width: 600px;
        padding: 0px;
        margin: 0px;
        background-color: mediumblue;
        font-size: 36px;
        font-family: Arial, Helvetica, sans-serif;
        color: white;
        font-weight: bold;
        transform: scale(0.5, 0.5);
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="div1">
      <div class="div2">
        <div class="div3">
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Tempora vel,
          atque voluptate, doloribus, illum asperiores omnis ea mollitia eveniet
          aut sit dignissimos praesentium cumque error. Commodi amet at eaque
          eos?
        </div>
      </div>
    </div>
  </body>
</html>

And here is the workaround that I have found ("embedding" div is "div3", and content is in "div4"):

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <style>
      body {
        height: 100vh;
        width: 100vw;
        padding: 0px;
        margin: 0px;
        background-color: gray;
      }
      .div1 {
        height: 100%;
        width: 100%;
        padding: 0px;
        margin: 0px;
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
      }
      .div2 {
        height: 400px;
        width: 400px;
        padding: 0px;
        margin: 0px;
        flex-shrink: 0;
        overflow: auto;
        background-color: red;
      }
      .div3 {
        position: relative;
        top: 0px;
        left: 0px;
        height: 300px;
        width: 300px;
        padding: 0px;
        margin: 0px;
        overflow: hidden;
      }
      .div4 {
        position: relative;
        top: 0px;
        left: 0px;
        height: 600px;
        width: 600px;
        padding: 0px;
        margin: 0px;
        background-color: mediumblue;
        font-size: 36px;
        font-family: Arial, Helvetica, sans-serif;
        color: white;
        font-weight: bold;
        transform: translateX(-150px) translateY(-150px) scale(0.5, 0.5);
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="div1">
      <div class="div2">
        <div class="div3">
          <div class="div4">
            Lorem ipsum dolor sit, amet consectetur adipisicing elit. Tempora
            vel, atque voluptate, doloribus, illum asperiores omnis ea mollitia
            eveniet aut sit dignissimos praesentium cumque error. Commodi amet
            at eaque eos?
          </div>
        </div>
      </div>
    </div>
  </body>
</html>


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source