Lesson 3 HeyDayAssignment
Lesson 3 HeyDayAssignment
3. Set the body style rule as shown below (if yours isn't already showing that). You can copy/paste
in the code below to save typing:
body{
background-image: url(pix/misty.jpg);
}
4. Save the style sheet, and open or refresh the page in the browser. The page should look
something like the one below.
Remove ImageRepeat
Depending on the size of your browser window, you may be able to tell that the image is tiling or
repeating itself to fill the available space. But that wasn't our intent here. We just want the photo to
show once. During the lesson you saw setting the background-repeat property to no-repeat could fix
that. Let's see how it works.
Here are the Steps
[Type here]
body{
background: url(pix/misty.jpg); background-repeat: no-repeat;
}
3. Save the style sheet, and open or refresh the page in the browser.
Now when you look in the browser, the image no longer repeats. However, if the browser window is
larger than the image, the white page background also shows, as in the image below.
body {
background: url(pix/misty.jpg); background-repeat: no-repeat;
background-size: cover;
}
3. Save the style sheet, then open or refresh the page in your browser.
[Type here]
At this point, the image should be wide enough to cover the screen.
It might seem as though we're done, but there is a little potential problem you might not notice right
away, especially if the background image is small and the page is very tall.
2. The second is the background-position property. You can use this property to ensure that the
background image always stays centered both horizontally and vertically.
background-attachment: fixed;
background-size: center center;
When we add the following code, the reason why the code is there is because you only need it when
you're using a background image in the body{} style rule and want to ensure the image stays centered
and always fills the background.
Here are the Steps
2. Add the comment and two lines of code shown below to your style rule.
/* Use this only when you want one background image to fill the page
body */ background-attachment: fixed;
background-position: center center;
3. When your style rule looks like the one below, save your style sheet.
body {
[Type here]
background: url(pix/misty.jpg); background-repeat: no-repeat;
background-size: cover;
/* Use this only when you want one background image to fill the page
body */ background-attachment: fixed;
background-position: center center;
}
Now when you view the page in a browser, the background should be covered entirely with one
image, with the center of your background image in the center of the screen. Note that the sun is not
the center of that photo. So if it looks off-center to you, it's because you're assuming the sun is in the
center, but it's not. It's slightly left and above of the center in the photograph.
When the shape of the browser window is different from the shape of the image, some of the edges
will naturally be cut off. But the idea here isn't to show the image as a figure or photograph, but rather
just as a decorative thing to enhance the appearance of the page. The figure below shows
(approximately) how the image will look at different sizes and shapes of a browser window.
Though it won't be apparent when working with a page that has no content, using the fixed
attachment method, as we did above, prevents the image from scrolling when the user scrolls down
the page. Instead, the image stays right where it is and the content scrolls in front of it. That's a nice,
clean professional way to use a background photo. But again, you won't be able to try it out until your
page contains enough text and photos for a scroll bar to appear at the right of the page.
HTML and CSS are all about creativity. So please feel free to experiment and try out different
backgrounds until you find something you like, whether it is a color, gradient, or image.
[Type here]