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.

Screenshot of a weather app at a mobile width
The app at a narrow width.

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.

Screenshot of a weather app with wide gaps between items
The app at a point where we feel we should tweak the design.

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.

Screenshot of a a page of text on a mobile device
The text as read on a mobile device.
Screenshot of a a page of text on a desktop browser
The text as read on a desktop browser with a breakpoint added to constrain the line length.

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.

Screenshot of DevTools with our weather app open and a width of 822 pixels selected.
DevTools showing the weather app as it looks at a wider viewport size.
Screenshot of DevTools with our weather app open and a width of 436 pixels selected.
DevTools showing the weather app as it looks at a narrower viewport size.

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.

Last updated:  Improve article

Comments

Popular posts from this blog

What Build Tools Do JavaScript Developers Use?

unit3.online registration form tricks: input field box align right and placeholder text right, but input text left.

how to use :focus

Numbers in JavaScript introduction

WordPress Theme Development From Scratch - 3. Enqueuing CSS and JS to W...

review: Add Form Labels task3