+ All Categories
Home > Documents > Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Date post: 16-Jan-2016
Category:
Upload: curtis-marshall
View: 213 times
Download: 0 times
Share this document with a friend
Popular Tags:
57
Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual
Transcript
Page 1: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Cascading Style Sheets

Part 1

Presentation by Audrey AltmanExercises from CSS: The Missing Manual

Page 2: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

CSS is awesome because…

• It’s easy to write (once you get the hang of it)• You can apply one CSS stylesheet to all your

pages• It’s easy to edit• It loads fast• It frees you to make you HTML more search

engine-friendly (more on that later)• It makes your HTML look sooooo good– Examples: http://www.csszengarden.com/

Page 3: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Today’s agenda

1. How to make CSS-friendly HTML2. CSS syntax3. Cascades4. Formatting words and letters

Page 4: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

HOW TO MAKE CSS-FRIENDLY HTMLLesson 1

Page 5: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

In the dark days before CSS, I had to use workarounds to make my HTML look decent…

…<table> tags for layout

…<h2> instead of <h1> to avoid giant headings

…special tags like <font> <b> <i> <br />

…doing my design and layout in Photoshop, then splicing it back together on my HTML page

Page 6: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Now that I have CSS, my HTML reflects the logical structure of my web page!

• Only use <table> for actual data tables

• Use <div> and </div> to subdivide page into logical elements

• Use <span> and </span> for inline divisions

• Use <h1> only once per page; use <h2> for subheadings; use <h3> for sub-subheadings, etc.

• Use <strong> instead of <b>

• Use <em> instead of <i>

• Use <br /> sparingly

• Make sure every opening tag has a closing tag

This makes my HTML cleaner and search engine-friendly.Oh joy!

Page 7: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Remember that HTML follows the Document Object Model (DOM)…

Page 8: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Document Object Model (DOM)

• Everything in an HTML document is divided into objects – Objects are expressed with tags, ie. <body>– Objects are organized in a tree structure

Page 9: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

DOM Tree Structure<html> <head> <title>HTML and CSS </title> </head> <body> <h1> HTML and CSS </h1> <p> I’m a lean, mean HTML machine! </p> <p> My CSS will make my HTML <em> so beautiful! </em></p> </body></html>

html

head

title

body

h1 p p

em

Page 10: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Document Object Model (DOM)

• Everything in an HTML document is divided into objects – Objects are expressed with tags, ie. <body>– Objects are organized in a tree structure

• Objects have attributes• Objects can be accessed using methods

• CSS helps you to assign design attributes to objects

Page 11: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

CSS SYNTAXLesson 2

Page 12: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

With CSS, we will say two basic things:

• “Hey computer, I’m CSS!”– Internal stylesheet– External stylesheet– Inline style

• “Hey computer, make that look like this!”– Declaration block

• Stylesheets are comprised of declaration blocks. Easy peasy lemon squeezy!

Page 13: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Internal Stylesheet (good for styling one page)

• Stylesheet goes in the <head> section

<html> <head> <title> The Reddest Radish </title> <style type=“text/css”> h1 { color:red; font-weight: bold; } …

</style> </head>

Page 14: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

External Stylesheet(good for styling multiple pages)

• Stylesheet goes in a separate CSS file that is referenced in the <head> section

<html> <head> <title> The Reddest Radish </title> <link rel=“stylesheet” type=“text/css” href=“mystylesheet.css” /> </head>

h1 { color: red; font-weight: bold;}

mystylesheet.css

Page 15: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Inline Style(for special occasions)

• Style is given to a single object in the HTML body using the style attribute

<html> <head> <title> The Reddest Radish </title> </head>

<body> <h1 style=“color: red; font-weight:bold;”> The Reddest Radish </h1>

Page 16: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

CSS Declaration Block

Browsers ignore spaces and tabs within the declaration block.

Page 17: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Tag selector

• Apply style to every instance of an HTML tag on a webpage

<body> <h1> The Reddest Radish </h1> <p> Once upon a time… </p> <p> The rabbit was hungry… </p> <p> Copyright 2012 </p></body>

body { background-color: blue;}h1 { color: red; font-weight: bold;}p { font-family: Arial;}

Page 18: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Class selector• Apply style to every HTML object with a given class• More than one HTML object can have the same class name• In a CSS declaration block, class names start with a period (.)

– The period must be followed by a letter– Class names are case-sensitive, and can only consist of letters, numbers,

hyphens, and underscores.

<body> <h1> The Reddest Radish </h1> <p class= “special”> Once upon a time… </p> <p class=“special”> The rabbit was hungry… </p> <p> Copyright 2012 </p></body>

.special { margin-left: 40px;}

Page 19: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

ID selector• Apply style to a single HTML object• Only one HTML object can have a given ID• In a CSS declaration block, ID names start with a pound sign (#)

<body> <h1> The Reddest Radish </h1> <p> Once upon a time… </p> <p> The rabbit was hungry… </p> <p id=“copyright”> Copyright 2012 </p></body>

#copyright { text-align: center;}

Page 20: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Inheritance

• Some CSS properties applied to one tag are passed on to nested tags

• Some properties are not inherited:– borders, padding, and margin, – text decoration – background images– layout properties such as height, width, and float

Page 21: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

body

h1

strong

div

h2

strong

p

strong

ul

li

li

li

HTML family tree

Page 22: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

body

h1

strong

div

h2

strong

p

strong

ul

li

li

li

div {background-color: red;}

Page 23: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Descendent selectors

Apply style to selectors within selectors

p strong { background-color: red; } //apply style to all strong’s within first headings (h1) red

ul li a {background-color: red ;} //apply style to all links (a) within list items (li) within unordered lists (ul)

p.intro {color: red;}//apply style to all paragraphs (p) with an “intro” class attribute

p .intro {color: red;}//apply style to all tags with an “intro” class attribute within paragraphs (p)

.intro strong {color: red;}//apply style to all links (a) within tags with an “intro” class attribute

Page 24: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

body

h1

strong

div

p

strong

p

strong

ul

li

li

li

strong {background-color: red;}

Page 25: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

body

h1

strong

div

p

strong

p

strong

ul

li

li

li

p strong {background-color: red;}

Page 26: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

body

h1

strong

div class = “intro”

p class = “intro”

span

p

span

span

.intro {background-color: red;}

Page 27: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

.intro span {background-color: red;}

body

h1

strong

div class = “intro”

p class = “intro”

span

p

span

span

Page 28: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

div .intro span

body

h1

strong

div class = “intro”

p class = “intro”

span

p

span

span

Page 29: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

div.intro span

body

h1

strong

div class = “intro”

p class = “intro”

span

p

span

span

Page 30: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Group and universal selectors

• Group selector– You can apply style to a

group of selectors (separated by commas)

• Universal selector (*)– You can apply a style to

every object on the page at once.

h1, h2, p, .special, #copyright { font-family: Arial;}

* { color: blue;}

Page 31: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Psuedo-Classes

• a:link selects any link a user hasn’t visited yet.• a:visited selects any link a user has clicked before.• a:hover lets you change the look of a link as the

mouse passes over it.• a:active lets you determine how a link looks as a

user clicks. It covers the brief nanosecond when someone’s pressing the mouse button, before releasing it.

Page 32: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Properties

• List of CSS properties:– http://www.w3schools.com/cssref/default.asp – Click on any property to learn about its possible

attributes

Page 33: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

CASCADESLesson 3

Page 34: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

When styles collide…

• Sometimes elements are affected by more than one style command– Conflicting inheritances: different styles inherited

from several generations of ancestors– Conflicting specificity: different tag, class, id, and/or

inline styles applied to a single element• The cascade governs which style gets precedence

when there’s a conflict

Page 35: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Inheritance: who wins?

• Nearest ancestor beats other ancestors• Directly applied style beats all ancestors

<body> <h1> <strong> BOOGIE! </strong> </h1></body>

body { color: blue; font-size: medium;}h1{ color: green; font-size: large;}strong { color: yellow;}

What size will the word “BOOGIE!” be?

What color will the word “BOOGIE!” be?

Page 36: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Specificity: who wins?You can use a combination of tag, class, and ID selectors, along with inline style. If your commands are contradictory…

Tag selectors are overridden class selectors, ID selectors, and inline style

Class selectors override tag selectors, but are overridden by ID selectors and inline style.

ID selectors override both tag and class selectors, but are overridden by inline style.

Inline style overrides tag, class and ID selectors.

1 point10 points

100 points

1000 points

Page 37: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

What color will the paragraph be?

<p class=“special” id=“copyright”> Copyright 2012 </p>

p { color: black;}.special { color: blue;}#copyright { color: green;}

Page 38: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Now what color will the paragraph be?

<p class=“special” id=“copyright” style=“background-color:red”> Copyright 2012 </p>

p { color: black;}.special { color: blue;}#copyright { color: green;}

Page 39: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Tiebreaker: the last style wins

When two elements with the same specificity are applied to an element, the last style wins.

<p class=“intro”> I want a bagel. <span class=“shout”> NOW! </span></p>

p .shout {color: blue;}

.intro span {color: red;}

What color will the word “NOW!” be?

Page 40: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

The ultimate win

• Insert !important after any property to shield it from specificity-based overrides.

a { color: teal !important; }

Page 41: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Start with a clean slate

Browsers apply their own styles to tags. To avoid cross-browser inconsistencies, start with a CSS reset like this one:

html, body, h1, h2, h3, h4, h5, h6, p, ol, ul, li, pre, code, address, variable, form, fieldset, blockquote { padding: 0; margin: 0; font-size: 100%; font-weight: normal;}ol { margin-left: 1.4em; list-style: decimal;}ul { margin-left: 1.4em; list-style: square;}img { border: 0;}

Page 42: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

FORMATTING WORDS AND LETTERSLesson 4

Page 43: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Open css_practice_1.html

• Note how the programmer divided the page into logical sections with <div> tags, and labeled these sections with id’s.

Page 44: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Code: set up stylesheets

• Link to external stylesheets– In the head, under the title, write:<link href="reset.css" rel="stylesheet" type="text/css" /><link href="layout.css" rel="stylesheet" type="text/css" />

• Create internal stylesheet– After the link to the external stylesheets, write:<style type=“text/css”>

</style>

• If each stylesheet assigns a different color to the h1 tag, which style will win?

Page 45: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Open css_practice_1.html in your web browser.

Page 46: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Colors, font-size, font-family• colors

– can be words, hexidecimals, or RGB• color: red;• color: #FF0000;• color: rgb(255, 0, 0);

• font-size – Keywords:

• xx-small, x-small, small, medium, large, x-large, xx-large• font-size: small;

– Pixels: height of letter• font-size: 36px;

– Percentages: percentage of base font size determined by browser• font-size: 150%;

– ems: same as percentage, but 1.0 = 100%• font-size: 1.5em;

• font-family – Not all browsers support all fonts, so it’s good to have some back-ups. List your first

choice first, followed by your second, etc.• font-family: Tahoma, "Lucida Grande", Arial, sans-serif;

Page 47: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Code: body

• In the internal style sheet you just created:

Note how the body’s descendants inherit these properties.

body {color: #102536;font-family: Tahoma, "Lucida Grande", Arial, sans-

serif;font-size: 62.5%;

}

Page 48: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Margins to adjust space inbetween elements (more on this next week)

Page 49: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Code: headersh1 {

font-size: 2.4em;color:#14556B;

}h2 {

font-size: 1.5em;color: #993;margin-bottom: 5px;

}

Note how these directly applied styles override the inherited styles from the body.

Page 50: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Formatting words

• To italicize, add a font-style property– font-style: italic;– font-style: normal;

• To make text bold, add a font-weight property– font-weight: bold;– font-weight: normal;

• Letter and word spacing adjusts space between letters and words– use pixels, ems, or percentages– positive or negative numbers– word-spacing: 2em;– letter-spacing: -1px;

Page 51: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Formatting paragraphs

• Line height adjusts space between lines of text – use pixels, ems, or percentages– line-height: 1.5em;

• Indent the first line of a paragraph– text-indent: 25px;– text-indent: 5em;

• Align text to the left, right, or center:– text-align: left;– text-align: center;

Page 52: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Code: paragraphs

p {font-size: 1.2em;margin-top: 5px;margin-bottom: 0;text-indent: 2em;line-height: 150%;margin-top: 0;margin-bottom: 5px;

}

How can we make the byline paragraph look different?

Page 53: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Code: byline

.byline {color: #73afb7;font-size: 1.5em;margin-bottom: 10px;font-weight: bold;letter-spacing: 1px;font-variant: small-caps;text-indent: 0;

}

Add a class attribute to the first paragraph in the content div:<p class="byline">By Jean Graine de Pomme </p>

Then add the following declaration blocks to your internal stylesheet:

Page 54: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

lists• List style type changes the look of the bullet for unordered

lists, and of the number or letter for ordered lists– list-style-type: circle;– list-style-type: none;

• List style image allows you to use an image file in place of a bullet or number – list-style-image: url(images/bullet.gif);

Page 55: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

We have two unordered lists (ul) on our page. We want to give them different styles.What should we use as selectors in our declaration blocks?

Page 56: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Code: lists#mainNav ul {

font-size: 1.2em;list-style-type: none;margin-left: 0;padding-left: 0;text-transform: uppercase;text-align:center;

}

#mainNav li {margin-bottom: 10px;

}

#content ul {font-size: 1.2em;list-style-type:

square;}

#content li {margin-bottom:

5px;}

Page 57: Cascading Style Sheets Part 1 Presentation by Audrey Altman Exercises from CSS: The Missing Manual.

Next week’s agenda

1. Layout2. Navigation3. Images4. Tables5. Integrating CSS and JavaScript


Recommended