<div class="aspectratio">
  4 : 3
  <span id="width">width: ?</span>
  <span id="height">height: 300px</span>
</div>
@import url("https://fonts.googleapis.com/css2?family=Exo:wght@600&display=swap");

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

body {
  width: 100vw;
  min-height: 100vh;
  font-family: "Exo", Arial, sans-serif;
  background-color: #222;
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  gap: 10px;
}

.aspectratio {
  height: 300px;
  aspect-ratio: 4 / 3;

  background-color: #09f;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}

.aspectratio span {
  position: absolute;
  white-space: nowrap;
}

#width {
  left: 5px;
  right: 5px;
  bottom: calc(100% + 5px);
  text-align: center;
}

#height {
  top: 5px;
  bottom: 5px;
  writing-mode: vertical-lr;
  text-align: center;
  left: calc(100% + 5px);
}
const boxElement = document.querySelector(".aspectratio");

const widthElement = document.getElementById("width");

const widthVal = boxElement.getBoundingClientRect().width;

widthElement.textContent = `width: ${widthVal}px`;
View Compiled

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.