Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS vw and vh but relative to the parent instead of viewport

Tags:

html

css

I'm trying to create a fixed aspect ratio box that resizes to not overflow its parent.

The classic padding-bottom trick is only able to define height in terms of parent width, and as testable here allows the element to grow taller than the parent as width increases. Not good.

Using vh and vw however, we can accomplish what we want with the restriction that the parent is the dimensions of the viewport. Testable here.

<style>
  div {
    max-width: 90vw;
    max-height: 90vh;
    height: 50.625vw; /* height defined in terms of parent width (90*9/16) */
    width: 160vh; /* width defined in terms of parent height, which is missing from the padding-bottom trick (90*16/9) */
    background: linear-gradient(to right, gray, black, gray);
  }
</style>

<div></div>

Is there a way to have a vh and vw equivalent that references the parent instead of the viewport? Or is there a complimentary trick to the padding-bottom trick which fixes its problems? Where is the css ratio property?

Also, images have some sort of intrinsic ratio, but I'm unsure how to take advantage of this.

like image 657
Tet Avatar asked Aug 29 '16 01:08

Tet


People also ask

What is the difference between VW and VH in CSS?

To reiterate, VH stands for “viewport height”, which is the viewable screen's height. 100VH would represent 100% of the viewport's height, or the full height of the screen. And of course, VW stands for “viewport width”, which is the viewable screen's width.

How do you use VH and VW CSS?

To use vh and vw values, just type “Nvh” or “Nvw” (where “N” represents the percentage of the viewport you'd like to cover) into any width or height field. So to cover 100% of the viewport, you'd set 100% for the width and 100vh for the height. To cover half of the viewport height, you'd set a height of 50vh.

What does 100vw mean in CSS?

The difference between using width: 100vw instead of width: 100% is that while 100% will make the element fit all the space available, the viewport width has a specific measure, in this case the width of the available screen, including the document margin.

What is 2vw?

For example, if a view-port is 1600px wide, and you specify something as being 2vw, that will be the equivalent of 2% of the view-port width, or 32px.


1 Answers

You can use something similar to what I did in Maintain aspect ratio with a fixed height, which takes advantage the intrinsic aspect ratio of <canvas> elements.

But here we need nested containers with two canvas.

#resize {
  resize: both;
  overflow: auto;
  width: 100px;
  height: 140px;
  padding: 20px;
  border: 1px solid black;
}
.ratio {
  position: relative;
  display: inline-block;
  vertical-align: middle;
}
.ratio.vertical, .ratio.vertical > canvas {
  height: 100%;
  max-width: 100%;
}
.ratio.horizontal, .ratio.horizontal > canvas {
  width: 100%;
  max-height: 100%;
}
.ratio > div {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
}
#contents {
  background: linear-gradient(to right, gray, black, gray);
}
<div id="resize">
  <div class="ratio vertical">
    <canvas width="16" height="9"></canvas>
    <div>
      <div class="ratio horizontal">
        <canvas width="16" height="9"></canvas>
        <div id="contents">
          Hello
        </div>
      </div>
    </div>
  </div>
</div>
like image 199
Oriol Avatar answered Sep 23 '22 09:09

Oriol