Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase height as width decreases(Reverse Vw for height)

Tags:

html

css

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!

like image 385
Ahmad Azizov Avatar asked Mar 25 '16 02:03

Ahmad Azizov


People also ask

How is VH calculated?

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.

How is VH calculated in CSS?

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.

How do you measure the width of a viewport?

You can use the window. innerHeight property to get the viewport height, and the window. innerWidth to get its width.

What is Vmin CSS?

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.


1 Answers

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

like image 171
Le____ Avatar answered Sep 21 '22 17:09

Le____