i have been trying to increase the height of a div as the width decreases. This allows for all the text to fit in better and not flow out. Is it possible to achieve this without js?
width: 100%;
height: 29vw;
Vw decreases the height as the width decreases.(i need the exact opposite behaviour. Width goes down-height goes up) I have tried using a negative vw to reverse the effect with no luck.
Thank you!
Viewport Height (vh). This unit is based on the height of the viewport. A value of 1vh is equal to 1% of the viewport height.
In CSS, we also have length units based on the viewport size. A vh unit is 1% of the layout viewport's height. Similarly, the vw unit is 1% of the layout viewport's width.
You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width.
vmin is the minimum between the viewport's height or width in percentage depending on which is smaller. vmax is the maximum between the viewport's height or width in percentage depending on which is bigger.
The main way to do it is using CSS media queries (wich is very popular). If you're looking for an alternative to do it without media queries, here it is:
vw
gets bigger on small screens while calc(Xpx - Xvw)
gets smaller on small screens.
Example:
div {
width: 40vw;
height: calc(400px - 20vw);
background: tomato;
}
jsfiddle DEMO
Alternatively, you could use the other 3 units related to the viewport's size: vh
vmin
and vmax
.
vw - Relative to 1% of the width of the viewport*;
vh - Relative to 1% of the height of the viewport*;
vmin - Relative to 1% of viewport's* smaller dimension;
vmax - Relative to 1% of viewport's* larger dimension;*viewport = the browser window size.
Source: w3schools
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