July 26, 20179 yr 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 July 26, 20179 yr by DonkeyWorx
July 26, 20179 yr I find it works really well, there are alternatives using position absolute, but I would use VH if you're building for IE9+.
July 26, 20179 yr http://caniuse.com tells you browser compatibility for just about anything. use that if you are unsure about any CSS properties.
July 26, 20179 yr 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.
July 27, 20179 yr 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/.
July 27, 20179 yr 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.
July 27, 20179 yr 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.
July 28, 20179 yr 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.
July 28, 20179 yr 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.
July 28, 20179 yr 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?
July 28, 20179 yr 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.
July 31, 20179 yr 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.
July 31, 20179 yr 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.
August 1, 20179 yr 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.
August 1, 20179 yr 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.
August 2, 20179 yr Author Think I may have sorted out the issues. Final test version is here; http://www.donkeyworx.com/products/puzzles/spot-the-difference-interactive-new.shtml Please let me know if you spot anything untoward. Thanks again for the pointers.
Create an account or sign in to comment