Forecast Cards

Let's examine another case study. This time, suppose you're trying to make the following weather forecast cards, which have been mocked up by a graphic designer at your company:

You are given this HTML to start with, whose default rendering is far from your goal:

html
css
preview

The cards currently fill the page. A reasonable first step is make each one smaller. You start by setting the width:

html
css
preview

The sizes are improved, but the cards are still block elements. You want them to appear on the same line, so you switch them to inline-block:

html
css
preview

If the cards don't appear on the same line, try zooming out in your browser until they do.

Your browser may have a large default margin for figure elements. You want to provide a uniform amount of spacing across browsers, so you explicitly set this margin:

html
css
preview

The font used in the mockup didn't have serifs, so you change the font family on for figures. This property is inherited by all children:

html
css
preview

Next you apply a background color to each card:

html
css
preview

Whoops. The images are no longer visible because the colors are the same. You happen to know that these images have a transparent background, so you can fix this by adding a background color to the image elements themselves. If you use img as your selector, you will set the background color of all images across the entire page, which will be bad when you start to add other content. You'd like to narrow your target to just the images inside these cards, which you can achieve by using a direct descendent selector:

html
css
preview

The selector figure > img targets each image element that is an immediate child of a figure element.

The content runs right up to the edge of the cards. You fix this by adding some padding:

html
css
preview

The foreground content of the images runs right up to the edge of their boxes, so you decide to add some padding to these too:

html
css
preview

The images should fill the complete width of the cards, whatever size the cards happen to be, so you set their width to 100%:

html
css
preview

The images overflowed their parent. That's because the width doesn't currently take into account the padding you added. Adjusting box-sizing helps:

html
css
preview

You're getting closer. When you zoom out to make them all appear on the same line, you see that the boxes are all aligned at the bottom. You want them aligned at the top. To place an inline element at the top of its line, you use the vertical-align property:

html
css
preview

The images have different heights. You decide to make these uniform by setting their heights to a fixed value:

html
css
preview

For pixel images, setting both the width and height leads to distortion. But these are vector images. They don't get distorted.

The headings have too much space around them, and they need to be centered. Perhaps margin can fix both of these problems?

html
css
preview

The excess spacing is gone, but the centering didn't work. You remember that the auto margin trick only works for a block element whose width is not auto. You don't really know what the width should be because the headings may vary in size, so you dismiss auto as a solution.

You discover the poorly named text-align property, which tells a parent element that its inline children, text or otherwise, should be centered:

html
css
preview

Finally, you add a little margin above and below the images and round the corners of the cards:

html
css
preview

You and the graphic designer give each other high fives.