I recently included the option to add a large cover image, like the one above, to my posts. The source image is cropped, and below specific maximum dimensions it’s displayed at a predetermined aspect ratio. This post describes the implementation.
Known support: Chrome, Firefox, Safari, Opera, IE 9+
The way that the cover image scales, and changes aspect ratio, is illustrated in the following diagram.
The cover image component must:
- render at a fixed aspect ratio, unless specific maximum dimensions are exceeded;
- support different aspect ratios;
- support different background images;
- display the image to either fill, or be contained within the component;
- center the image.
The aspect ratio of an empty, block-level element can be controlled by setting a percentage value for its
padding-top. Given a declaration of
padding-bottom:50% (and no explicit
height), the rendered height of the element will be 50% of its width.
Changing that padding value will change the aspect ratio. For example, padding of
25% results in an aspect ratio of
4:1, padding of
33.333% results in an aspect ratio of
The problem with using this aspect ratio hack is that if the element has a
max-height declared, it will not be respected. To get around this, the hack can be applied to a pseudo-element instead.
Now the main element can take a
max-height. It should also clip the pseudo-element overflow.
This aspect ratio pattern is provided by the FlexEmbed component for SUITCSS. That component is primarily for responsive video embeds, but it’s flexible enough to be useful whenever you need an element rendered at a predetermined aspect ratio. It comes with modifiers for
4:3 aspect ratios. The cover image component can extend the FlexEmbed component.
<div class="CoverImage FlexEmbed FlexEmbed--2by1"></div>
The cover image is applied as a background image that is sized to cover the entire area of the element. This makes sure the image is clipped to fit the aspect ratio of the element.
If you want different cover images for different instances of the component, they can be applied via the
<div class="..." style="background-image: url(cover.jpg)"></div>
The image can be fully centered by using background positioning and block centering. This makes sure that the image is centered in the element, and that the element is centered within its parent (when it reaches the
margin: 0 auto;
If you depend on the FlexEmbed module, the amount of additional code required is minimal. (See the demo for all the code, including the FlexEmbed dependency.)
margin: 0 auto;
<div class="CoverImage FlexEmbed FlexEmbed--3by1"
You can add further customizations, such as setting the accompanying background color, or providing a means to switch between the
contain keywords for