I would like to do the next trick in my css file so that (height = width) without setting pixels. I want to do this so whatever the resolution of the browser is, to have same values on these two dimensions.
#test{
height: 100%;
width: (same as height);
}
I prefer to do it with css and not javascript.
Thank you in advance.
The length, width and height are the dimensions of a geometrical figure that depict how long, wide and high a figure is. While length is the longest side of a figure, width is the shorter side and height is the vertical dimension of the figure.
Top header with 100% width and 50px height. Left navigation bar with 200px width and dynamic height to fill the screen. A container on the right of the nav bar and under the header with dynamic width and height to fill the screen.
In the CSS for the <div>, add a percentage value for padding-bottom and set the position to relative, this will maintain the aspect ratio of the container. The value of the padding determines the aspect ratio. ie 56.25% = 16:9.
This is my way
aspect-ratio: 1 / 1;
height: 100%;
Example:
.my-container {
width: 100%;
height: 150px;
background: black;
padding: 20px
}
.my-element {
background: #fff;
aspect-ratio: 1 / 1;
height: 100%;
}
<div class="my-container">
<div class="my-element">Height = Width</div>
</div>
The only CSS way of doing this at the moment (AFAIK) is using viewport relates values (vh / vw )
Support is not great at the moment: http://caniuse.com/viewport-units but here is a quick demo
JSFiddle
CSS
.box {
background-color: #00f;
width: 50vw;
height:50vw;
}
The box is responsive but will always remain square.
Pure % values will not work as height:100%
does not equal width:100%
as they refer to different things being the relevant dimensions of the parent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With