Solved–LAB 4– (chapter 04) Introduction to –CSS Solution

$35.00 $24.00

What You Will Learn • How to use CSS to style HTML documents • How to use CSS selectors • The CSS box model QUICK TOUR OF CSS PREPARING DIRECTORIES 1 If you haven’t done so already, create a folder in your personal drive for all the labs for this book. 2 From the main…

You’ll get a: . zip file solution

 

 
Categorys:

Description

5/5 – (2 votes)

What You Will Learn
• How to use CSS to style HTML documents
• How to use CSS selectors
• The CSS box model
QUICK TOUR OF CSS

PREPARING DIRECTORIES
1
If you haven’t done so already, create a folder in your personal drive for all the labs for this book.
2
From the main labs folder (either downloaded from the textbook’s web site using the code provided with the textbook or in a common location provided by your instructor), copy the folder titled lab04 to your course folder created in step one.
CSS is a W3C standard for describing the appearance of HTML elements. Another common way to describe CSS’s function is to say that CSS is used to define the presentation of HTML documents. With CSS, we can assign font properties, colors, sizes, borders, background images, and even the position of elements. CSS is a language in that it has its own syntax rules. CSS can be added directly to any HTML element (via the style attribute), within theelement, or, most commonly, in a separate text file that contains only CSS.

Exercise 4.1 — ADDING STYLES
1
Open, examine, and test lab04-exercise01.html in browser.
2
Add the following internal style definition and test.

Share Your Travels

Remember: these labs use the convention of red bolded text to indicate content to change/enter.
3
Modify the following and test.

Share Your Travels

Exercise 4.2 — EMBEDDED STYLE SHEETS

1 Add the following embedded style to theelement from the previous exercise.

2 Test. Why didn’t it seem to work?
It didn’t work because of cascade specificity rules. The internal style created in last exercise overrides the embedded style we just created. To fix it, we will have to remove the internal styles.
3 Remove the internal styles created in last exercise. Test.
The

element should now have blue text against a yellow background.
4 Add the following style rule and test.
h1, h2, h3 {
font-family: “Trebuchet MS”, “Lucida Grande”, Tahoma, sans-serif;
}
This is a grouped selector which selects all three headings.
5 Add the following style rule after the one created in previous step and test.
h1, h2, h3 {
font-family: “Trebuchet MS”, “Lucida Grande”, Tahoma, sans-serif;
}
h1 {
font-family: Georgia, Cambria, “Times New Roman”, serif;
}
Notice that the new style rule for h1 overrides the earlier one due to the cascade principle of location (i.e., when rules have the same specificity, then the latest are given more weight).
6 Change the previous style rule to the following. Before you test it, ask yourself whether this will affect the font-family of the headings.
h1, h2, h3 {
font-family: “Trebuchet MS”, “Lucida Grande”, Tahoma, sans-serif;
}
body {
font-family: Georgia, Cambria, “Times New Roman”, serif;
}

Figure 4.1 – Exercise 4.2 Complete

Exercise 4.3 —EXTERNAL STYLE SHEETS

1 Create a new text document with the following content:
header, footer {
color: white;
background-color: #213643;
}
nav {
background-color: #728B96;
}
h1, h2, h3 {
font-family: “Trebuchet MS”, “Lucida Grande”, Tahoma, sans-serif;
}
body {
font-family: Georgia, Cambria, “Times New Roman”, serif;
}
Notice that this document contains no HTML. Also notice that the first style rule uses a grouped selector, meaning that both the header and footer elements will receive the same style.
2 Save your file as lab04-exercise03.css.
3 Open lab04-exercise03.html (which has the same content as the last exercise).

4 Add the following to theelement.5 Save and test lab04-exercise03.html in browser.6 View the lab04-exercise03.css file (yes the css file) in the browser.At some point everyone will mistakenly load a css file into the browser rather than the HTML file. What will happen will vary depending upon the browser and one’s computer setup. For the author of this lab, if I open up the CSS file in Chrome or FireFox, the CSS text file is displayed; in Internet Explorer, it starts my default CSS editor, which just happens to be Adobe Dreamweaver.CSS SELECTORSWhen defining CSS rules you will need to first need to use a selector to tell the browser which elements will be affected by the styles. CSS selectors allow you to select individual or multiple HTML elements, elements that belong together in some way, or elements that are positioned in specific ways in the document hierarchy. The previous exercises used HTML selectors. The next exercises make use of other selectors.Exercise 4.4 — ELEMENT, CLASS, AND ID SELECTORS

1 Open lab04-exercise04.html.
2 Add the following to the markup.

Reviews

3 Open lab04-exercise04.css, add the following style, and test.
#reviews {
border-bottom: solid 3pt #213643;
color: #ed8030;
}
4 Change the previous selector to the following and test.
h3#reviews
In this example the selector in step 3 and 4 are functionally equivalent. However, the one in step 4 is more specific, so in some situations will be preferable.5 Switch to lab04-exercise04.html and add the following.

Reviews

By Ricardo on

Easy on the HDR buddy.


By Susan on

I love Central Park.


6 Switch to lab04-exercise04.css and add the following style.
.first {
color: #728B96;
font-style: italic;
}
7 Test.
Remember that whenever the lab says “test” it means view the relevant HTML file in a browser.
Attribute Selectors
An attribute selector provides a way to select HTML elements either by the presence of an element attribute or by the value of an attribute. This can be a very powerful technique, but because of uneven support by some of the browsers, not all web authors have used them.

Exercise 4.5 — ATTRIBUTE SELECTORS

1 Open lab04-exercise05.html and view in browser.
2 Open lab04-exercise05.css, add the following style, and test.
[title] {
cursor: help;
text-decoration: none;
padding-bottom: 3px;
border-bottom: 2px dotted blue;
}
This will select every element that contains a title attribute. Examine the HTML to see which elements contain a title attribute.

3 Modify the attribute to the following and test.
[title=”Calgary”] {
This selects only the one element whose title attribute is exactly Calgary.
4 Modify the attribute (add the asterisk) to the following and test.
[title*=”Calgary”] {
This selects all elements that contain the text Calgary within in.
5 Modify the attribute (add the caret) to the following and test.
[title^=”Calgary”] {
This selects all elements that begin with the text Calgary.

Pseudo-Class Selectors
The next exercise illustrates the use of pseudo-class selectors, which do not apply to an HTML element, but targets either a particular state or, in CSS3, a variety of family relationships. The most common use of this type of selectors is for targeting link states. By default, the browser displays link text blue and visited text links purple. Exercise 4.6 illustrates the use of pseudo-class selectors to style not only the visited and unvisited link colors, but also the hover color, which is the color of the link when the mouse is over the link. Do be aware that this state does not occur on touch screen devices.
Exercise 4.6 — PSEUDO SELECTORS AND LISTS

1 Open lab04-exercise06.html.
2 Switch to lab04-exercise06.css and add the following style.
a:link {
font-weight: bold;
color: #47B3D7;
}
a:visited {
color: #BB78FF;
}
3 Test in browser.
4 Add the following style to lab04-exercise06.css and test.
a:hover {
background-color: #FFFF99;
}
To test this style, you must hover your mouse over a link.

5 Add the following style to lab04-exercise06.css and test.
nav ul li {
list-style: none;
}
This removes the bullet from the navigation

  • elements.
    6 Add the following and test.
    nav ul li {
    list-style: none;
    display: inline;
    }
    By changing the
  • elements from their default display: block value, the list items are no longer block elements (thus existing on their own lines) but are now inline elements.
    7 Add the following and test.
    nav ul li {
    list-style: none;
    display: inline;
    margin: 1em;
    }
    This adds space to the left and right of each list item. Why doesn’t it also add space to the top and bottom as well? The answer is that top and bottom margins are ignored for inline elements.
    8 Add the following and test.
    nav {
    padding: 0.25em;
    }
    This adds padding space within the