Lab 06 Background Images & Videos
Goal
- Continue to utilize HTML tags and CSS properties from previous labs
- Learn CSS property
background-image
,background-repeat
,background-position
, andbackground-size
- Expand knowledge of CSS Pseudo-classes
- Embed a video in HTML
Learn
Background Image CSS
background-image:url(images/image.jpg);
background-image
Instead of using an <img>
element in the HTML an alternative is using the CSS property background-image. Keep in mind that background images are not semantic HTML tags, therefore screen readers will not announce the presence of background images. Background images should only be used for decoration and not critical information.
Above is an example of the CSS declaration for a single background image.
Hint: You will need url or file path to tell the browser the source of the image. If you place the image within an images folder remember to indicate that in your path.
background-image:url(images/image1.jpg), url(images/image2.jpg);
Background-image you can have more than one image in the declaration. To do this you will separate the images with commas like above. They will be displayed in the order they are in the CSS declaration.
background-image:url(images/image.jpg);
background-repeat: repeat;
background-repeat
Defines how background images are repeated. A background image can be repeated along the horizontal (x) and vertical (y) axes, or not repeated at all.
Values for background-repeat:
- repeat-x - The image is repeated only horizontally.
- repeat-y - The image is repeated only horizontally.
- repeat - The image is repeated as much as needed to cover the whole background image painting area. The last image will be clipped if it doesn't fit. (default).
- no-repeat - The image is shown once.
- space - The image is repeated as much as possible without clipping. The first and last images are pinned to either side of the element, and whitespace is distributed evenly between the images. The background-position property is ignored unless only one image can be displayed without clipping.
- round - As the allowed space increases in size, the repeated images will stretch (leaving no gaps) until there is room (space left >= half of the image width) for another one to be added. When the next image is added, all of the current ones compress to allow room. Example: An image with an original width of 260px, repeated three times, might stretch until each repetition is 300px wide, and then another image will be added. They will then compress to 225px.
When defining the background-repeat for multiple images use commas to separate the values.
background-image:url(images/image.jpg);
background-repeat: repeat;
background-position: top;
background-position
Defines the position of the background image within the element.
Background-position: has a variety of values that can be used to define the image's position. By default all images start in the top left corner of the HTML element they are assigned to.
Values for background-position:
- keywords: top | bottom | left | right | center
- percentages
- edge offsets: for example "bottom 10px right 20px"
When defining the background-position for multiple images use commas to separate the values.
background-image:url(images/image.jpg);
background-repeat: repeat;
background-position: top;
background-size:cover;
background-size
Values for background-size:
- cover - Scales the image as large as possible without stretching the image. If the proportions of the image differ from the element, it is cropped either vertically or horizontally so that no empty space remains.
- contain - Scales the image as large as possible without cropping or stretching the image.
- percentages
- em, rem
- pixel
When using one value with background-size the value is the width size and the height is scaled accordingly, if there are two values the first is the width and the second is height.
When defining the background-size for multiple images use commas to separate the values.
CSS Pseudo-Classes
button{
background-color:blue;
}
button:hover{
background-color:red;
}
A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, :hover can be used to change a button's color when the user's pointer hovers over it. In the CSS example above the button would be blue, except when the user hovers their cursor over the button, then it would turn red.
There are many different types of pseudo classes. Here is a list of all possible pseudo classes. Below are some of the most basic pseudo classes, we will be adding more to our repertoire in future labs.
Location Pseudo Classes
These pseudo-classes relate to links, and to targeted elements within the current document.
:visited
- Matches links that have been visited. This is useful to visual show the user that they have already clicked on a link.
:link
- Matches links that have not yet been visited. This is useful to visual show the user they have not clicked on the link.
User Action Pseudo Classes
These pseudo-classes require some interaction by the user in order for them to apply, such as holding a mouse pointer over an element.
:hover
- Matches when a user designates an item with a pointing device, for example holding the mouse pointer over it.
:active
- Matches when an item is being activated by the user, for example clicked on.
Embedding a Video
Embedding a video iframe from Youtube is very easy. Follow these instructions to accomplish it.
Locate the video you would like to embed.
Click on the share icon below the video.
In the share options click on embed
Copy the iframe code.
Paste the iframe into your HTML document were you want the video to appear.
Now you will be able to see the video in the browser
Practice
Experiment Try setting the background-repeat to no repeat, background-size to contain, and background-position to center in the demo below.
HTML
CSS
Preview
Pick a topic you would like to make three different web pages for. (Must be a different topic than lab 3)
Open GitHub Desktop (you may need to download the program again if the computers get wiped.)
Couple of things to check in GitHub Desktop.
- Check to see if it says Pull Origin. If it does, click Pull Origin. Why? A pull occurs if you or a collaborator made a change to the repository and pushed the changes. ALWAYS MAKE SURE TO PULL BEFORE CODING!
- If the computers were wiped you made need to reclone your repository to the computer.
With your cs-318 repository selected, Click open in Visual Studio code.
Create a new folder called lab6
Create a new file called index.html plus two others you will need to name in the lab6 folder
Setup your HTML files
Create your stylesheet
Link your stylesheet to the HTML
Required HTML Elements
header
nav
main
img
a
p
h1
embedded video
Optional HTML Elements
- h2-h6
- div
- footer
- article
- section
- ol
- ul
Required CSS Properties
background-image
background-size
background-position
background-repeat
Optional CSS Properties
- background-color
- color
- padding
- margin
- width
- border
- font-family
- font-weight
- font-size
- text-align
- text-decoration
Pseudo Classes
You are required to use at least one pseudo class in your CSS
:link
:visited
:hover
:active
Once you are satisfied with your work, go through the final checklist.
Final Steps
Validate your HTML.
- Navigate to W3C Validator
- You will get an error about the frameborder being obsolete. This is okay, ignore this error.
- Select the validate by file upload tab at the top of the page
Lint your CSS at Jigsaw.w3.org
Now look at GitHub Desktop. You should see all of your files listed out in the left column.
Now you are going to commit your changes.
With all of the files checked. Fill out the Summary field in the bottom left and then click Commit to main.
Committing is staging the files, so they can be pushed to the repository hosted at GitHub.
Now you want to push your changes to the Repository. Now if you access the repository from another computer Lab6 will be there. After you push in the top right of the nav it should say "Fetch Origin" instead of "Push Origin"
Your are Done with Lab6
Are you someone who needs to double check that it worked? If you want to make sure the push was successful click on the "View on GitHub" button from GitHub Desktop. It will open up your repository in the browser. From there you should be able to see all of your files.
...What if I need to make a change after pushing?
So let's say you have already pushed but noticed an error. No worries, this is easy to fix.
In visual studio code make any changes you need to. Once you are done making corrections. Save.
Go back to GitHub Desktop. You should see your changes to the documents listed there.
Fill out the Summary field again. I suggest saying what you fixed. For example: "Fixed paragraph error on Sea-Jellies"
Click "Commit to main"
Click "Push Origin" and you are done.