20. - height: 50vw has better support than height: calc(50vw)
- When using vw for height you need to add a width of 100vw, because of the
Gmail Apps
- You can also use a basic padding-bottom: 50% or 50vw
Awesome to be here and thanks for having me!
I’m Wilbert Heinen from the Netherlands and I’m an Email Developer. I’m developing emails since 2009 at e-Village.
Our company have created their own ESP software called Clang and I’m working on the full service department where we support our customers by creating email lifecycle campaigns, emails and drag & drop emailtemplates for them in Clang.
When creating a drag & drop emailtemplate for a client, I bumped into a challenge:
Clients can upload a desktop image and a mobile image. The desktop image is just a plain image and the mobile image is a background image on the div surrounding the image that will show on mobile through media queries. The desktop image will be hidden on mobile. The images always have a fixed width, but the height is variable.
The mobile version of the template doesn’t have a fixed width, but is based on the viewport width. This isn’t a problem for the width because this is just plain simple width 100%, but the height needs to be relative. Because a simple percentage height won’t work I need to think of another solution.
From my experience with the vw unit I know this unit is powerful. VW unit often works for me where other units fail, so I thought I tried to use it by using it in the height property.
With vw/vh, we can size elements to be relative to the size of the viewport.
At first I did something I thought was clever: height: calc(50 * 1vw)
The vw/vh units are interesting in that 1 unit reflects 1/100th the width of the viewport. To make an element the full width of the viewport, for example, you'd set it to width:100vw.
So basically 1vw is 1/100 or 1% of the viewport width.
So this could be simpler by writing: height: calc(50vw)
And this worked great in the native mail apps of Android 4.4, my Samsung Galaxy S4 and the iOS native mail app in iOS8 and above. Also the Gmail and Inbox app played nicely.
But unfortunately not in iOS7 and the native mail app on Android 4.4
I want to support those as well so I left out the calc and just wrote: height: 50vw
It’s now working in the native mail app on Android 4.4 and iOS7
Looking closer to the images I noticed that in the Gmail App the background images that make use of vw get a little bit vertically stretched. Possibly because the viewport is larger the amount of width that’s available for the image.
This can be solved easily by adding a width of 100vw to the background image. I use the vw unit again because width: 100% has no effect.
Voila! Now we have a fullwidth background image with a relative height on mobile when media queries are supported.
A couple of weeks ago I stumbled into something that’s called the padding hack:
http://www.goldenapplewebdesign.com/responsive-aspect-ratios-with-pure-css/
“CSS gives us an easy way to give an element a “responsive” width declaration relative to the browser window size or other containing parent element by simply using a percentage. What we don’t have is a way to simply declare a responsive height relative to width, maintaining an aspect ratio.”
“We find that when declaring percentages instead of fixed values for padding, the percentage is calculated based on the WIDTH of the element in question, even if we are declaring a vertical value such as padding-top or padding-bottom.”
I can use padding-bottom with a precentage for the relative height and works everywhere as well
But I can also use vw in the padding-bottom and this also works everywhere, but you have to use width: 100vw to make sure images will not be vertically stretched.
Voila! Now we have a fullwidth background image with a relative height on mobile when media queries are supported.