Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Anyone using 'vh' to set div height

Featured Replies

I'm using 'vh' to ensure our puzzle games fit in the current browser viewport. Anyone else using this or got horror stories, or can suggest alternatives?

 

TIA

 

Paul

Edited by DonkeyWorx

I find it works really well, there are alternatives using position absolute, but I would use VH if you're building for IE9+.

  • Author

Thanks both, I did check on caniuse but just because browsers support it doesn't necessarily mean it's worth using :). And it's giving me a lot of issues with container divs and images therein and the code is beginning to look horrible.

 

But it sounds like I should persevere.

Thanks both, I did check on caniuse but just because browsers support it doesn't necessarily mean it's worth using :). And it's giving me a lot of issues with container divs and images therein and the code is beginning to look horrible.

 

But it sounds like I should persevere.

 

This is an alternative if VH isn't working https://jsfiddle.net/b46srmns/.

  • Author

Thanks - I wish it was that simple :( although I'm probably trying to be too clever for my own good - it's just such an elegant solution I don't want to give it up :)

 

I've got it working for simple stuff but putting it into a responsive grid design is causing problems. I have to maintain aspect ratio for both bitmap and svg images in sub-divs (one of which uses position: absolute) so it's getting complicated. When the page resizes to a narrow mobile format as the image for the puzzle becomes too wide for the height set from vh. I'm thinking now I'll have to use different styling and prioritise vw for the mobile viewport.

Thanks - I wish it was that simple :( although I'm probably trying to be too clever for my own good - it's just such an elegant solution I don't want to give it up :)

 

I've got it working for simple stuff but putting it into a responsive grid design is causing problems. I have to maintain aspect ratio for both bitmap and svg images in sub-divs (one of which uses position: absolute) so it's getting complicated. When the page resizes to a narrow mobile format as the image for the puzzle becomes too wide for the height set from vh. I'm thinking now I'll have to use different styling and prioritise vw for the mobile viewport.

 

Feel free to replicate the issue on something like jsfiddle.net and post it here.

  • Author

 

Feel free to replicate the issue on something like jsfiddle.net and post it here.

 

Can't fiddle it because the div's are working as they should - it's the content which is overflowing. I've probably left in some basic error from trying lots of different fixes. I'm going to unpick and rebuild (again) to see where the problem occurs. Beginning to think I should have just used javascript but I thought this would be elegant.

Can't fiddle it because the div's are working as they should - it's the content which is overflowing. I've probably left in some basic error from trying lots of different fixes. I'm going to unpick and rebuild (again) to see where the problem occurs. Beginning to think I should have just used javascript but I thought this would be elegant.

 

¯\_(ツ)_/¯

 

It's highly likely you won't need to use JS to adjust your layout. CSS is considerably more powerful than it was even a couple years ago, less and less hacks are required.

  • Author

OK, I think I've distilled the problem to this. Here's the html

	<div id="puzzGame" style="height:85vh;">
		<img src="game.png" style="width:auto; height:inherit">
	</div>	 

which works perfectly as long as the aspect ratio of the viewport matches or is wider than the game.png (which it isn't on most mobile screens). To preserve the aspect ratio of game.png I cannot have width: inherit or 100% (or vice versa).

 

The only solution I can see is to manually calculate a width from the height which preserves the aspect ratio of the game.png into the container div to that of the image but I'm not sure that will work or refresh without javascript.

 

Am I missing something?

OK, I think I've distilled the problem to this. Here's the html

	<div id="puzzGame" style="height:85vh;">
		<img src="game.png" style="width:auto; height:inherit">
	</div>	 

which works perfectly as long as the aspect ratio of the viewport matches or is wider than the game.png (which it isn't on most mobile screens). To preserve the aspect ratio of game.png I cannot have width: inherit or 100% (or vice versa).

 

The only solution I can see is to manually calculate a width from the height which preserves the aspect ratio of the game.png into the container div to that of the image but I'm not sure that will work or refresh without javascript.

 

Am I missing something?

 

Use a background image with background-size: cover; on.

  • Author

 

Use a background image with background-size: cover; on.

 

Thanks Jack, that would work if I could get the current size of the background image but I don't think that's possible.

 

Thanks Jack, that would work if I could get the current size of the background image but I don't think that's possible.

 

I'd be interested to see what you're trying to achieve, even if it's a wireframe or something basic.

  • Author

Sorry, didn't mean to drag you into my learning process. Here's the current working page.

 

http://www.donkeyworx.com/products/puzzles/spot-the-difference-fh.shtml

 

Works great for desktop but if you check the size of the divs on any mobile device the content of puzzGame overflows the div because the width must preserve aspect ratio which is why I'm thinking I might need javascript to calculate best fit. However, at the moment I'm looking at media queries for aspect ratio in the hope that having a different style-sheet for landscape and portrait might be enough.

 

I'm using an SVG overlay to act as a UI so I need to access the IDs for the elements in the SVG. Ultimately the bitmap may go in the SVG but I don't think it will help with the current problem.

 

I want the puzzle to fill the viewport as best it can but for the whole puzzle to be visible and I thought vh might be a solution. I'd convinced myself there would be a combination of width styles which would constrain the nested divs but I think it's going to need two style settings for different aspect ratios.

 

I want it to be as flexible as possible because other puzzle layouts may not have the same aspect ratio and I don't want to have to make too many changes for the puzzle to be embedded in a standard div in a grid layout so customers can use as both full page or embedded in a page with other content.

 

An HTML5 canvas is probably much simpler solution except I know nothing about working with the canvas so it looked like a steeper learning curve than this.

  • Author

May have solved this by using SVG to maintain the aspect ratio whilst scaling to fit and using media queries to use vh only where it is relevant.

 

Jack, your pointer on background image got me thinking about using the image aspect ratio so thanks for that. I will post the finalised version once I've done my full checks and tidied it up in case anyone is interested.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.