how to code a weather forecast web
Let's work through the example we saw at the beginning: the weather forecast. The first step is to make the forecast look good on a small screen.
Next, resize the browser until there is too much white space between the elements, and the forecast simply doesn't look as good. The decision is somewhat subjective, but above 600px
is certainly too wide.
To insert a breakpoint at 600px
, create two media queries at the end of your CSS for the component, one to use when the browser is 600px
and below, and one for when it is wider than 600px
.
@media (max-width: 600px) {
}
@media (min-width: 601px) {
}
Finally, refactor the CSS. Inside the media query for a max-width
of 600px
, add the CSS which is only for small screens. Inside the media query for a min-width
of 601px
add CSS for larger screens.
Pick minor breakpoints when necessary
In addition to choosing major breakpoints when layout changes significantly, it is also helpful to adjust for minor changes. For example, between major breakpoints it may be helpful to adjust the margins or padding on an element, or increase the font size to make it feel more natural in the layout.
Let's start by optimizing the small screen layout. In this case, let's boost the font when the viewport width is greater than 360px
. Second, when there is enough space, we can separate the high and low temperatures so that they're on the same line instead of on top of each other. And let's also make the weather icons a bit larger.
@media (min-width: 360px) {
body {
font-size: 1.0em;
}
}
@media (min-width: 500px) {
.seven-day-fc .temp-low,
.seven-day-fc .temp-high {
display: inline-block;
width: 45%;
}
.seven-day-fc .seven-day-temp {
margin-left: 5%;
}
.seven-day-fc .icon {
width: 64px;
height: 64px;
}
}
Similarly, for the large screens it's best to limit to maximum width of the forecast panel so it doesn't consume the whole screen width.
@media (min-width: 700px) {
.weather-forecast {
width: 700px;
}
}
Optimize text for reading
Classic readability theory suggests that an ideal column should contain 70 to 80 characters per line (about 8 to 10 words in English). Thus, each time the width of a text block grows past about 10 words, consider adding a breakpoint.
Let's take a deeper look at the above blog post example. On smaller screens, the Roboto font at 1em
works perfectly giving 10 words per line, but larger screens require a breakpoint. In this case, if the browser width is greater than 575px
, the ideal content width is 550px
.
@media (min-width: 575px) {
article {
width: 550px;
margin-left: auto;
margin-right: auto;
}
}
Avoid simply hiding content
Be careful when choosing what content to hide or show depending on screen size. Don't simply hide content just because you can't fit it on the screen. Screen size is not a definitive indication of what a user may want. For example, eliminating the pollen count from the weather forecast could be a serious issue for spring-time allergy sufferers who need the information to determine if they can go outside or not.
View media query breakpoints in Chrome DevTools
Once you've got your media query breakpoints set up, you'll want to see how your site looks with them. You could resize your browser window to trigger the breakpoints, but Chrome DevTools has a built-in feature that makes it easy to see how a page looks under different breakpoints.
To view your page under different breakpoints:
Open DevTools and then turn on Device Mode. This opens in responsive mode by default.
To see your media queries, open the Device Mode menu and select Show media queries to display your breakpoints as colored bars above your page.
Click on one of the bars to view your page while that media query is active. Right-click on a bar to jump to the media query's definition.
Comments
Post a Comment