This document provides an introduction to CSS (Cascading Style Sheets) and how to use CSS to style HTML elements and lay out web pages. It covers basic HTML tags, linking an external CSS stylesheet, using selectors and properties to style elements, adding divs to structure content sections, and other CSS techniques like typography, images, and white space. Examples are given throughout and the reader is prompted to practice these skills by adding CSS to their own HTML code.
2. What is CSS? C ascading S tyle S heets Adds style to HTML elements Separates design from content HTML (content) CSS (design)
3. 2-minute HTML H yper Te xt M ark-up L anguage Marks up plain text for web browsers Before After
4. Some HTML tags Tag Function Usage Output <p> Paragraphs <p>Sample text…</p> <p>Next paragraph.</p> Sample text… Next paragraph. <h1> Heading <h1>A headline</h1> A headline <strong> Bold <strong>Be bold</strong> Be bold <em> Italics <em>For emphasis</em> For emphasis <a> Hyperlink <a href=“link.html”>Click here</a> Click here <img /> Image <img src=“photo.jpg” alt=“Photo description” />
5. Your turn Mark up the text in your index.html file Turn “CSS for online journalism” into a heading Add paragraph tags to each paragraph Italicize the last line Turn the e-mail address into a link (use the following tag: <a href= " mailto:jsmith@student.umass.edu " > ) Remember to close each tag you open
6. Styling your HTML Create a CSS file and link to it index.html style.css <head> <link rel="stylesheet" type="text/css" href="style.css" /> </head>
7. CSS syntax Selector: the HTML tag to be styled Property: the attribute you want to change Value: the style of the attribute selector property value p { font-family: Courier; font-size: 11px; }
8. Your turn Change the color of the <a> tag by adding a definition to the style sheet Selector Property Values a color brown, red, orange, yellow, green, blue, purple, black, gray
9. What are divs? Div ide a page into sections Used to design the layout of a page header content sidebar
10. Divvying up the page First, create a page-container div <body> <div id="page-container"> ... </div> </body> HTML CSS #page-container { width: 1024px; }
11. Divvying up the page Then, wrap the content in its own div <body> <div id="page-container"> <div id="content"> <h1>...</h1> <p>...</p> </div> </div> </body> HTML CSS #content { float: left; width: 700px; }
12. Divvying up the page Now add a sidebar <body> <div id="page-container"> <div id="content"> ... </div> <div id="sidebar"> </div> HTML CSS #sidebar { float: right; width: 324px; background: brown; }
13. Divvying up the page And a header <body> <div id="page-container"> <div id="header"> <h1>My News Site</h1> </div> <div id="content"> ... </div> HTML CSS #header { background: gray; height: 60px; } #header h1 { color: white; }
14. Typography Property Description Values font-family A prioritized list of font families family-name, generic-family font-size Sets the size of the font 8px, 10px, 12px, 14px, etc. font-style Sets the style of the font normal, italic, oblique font-weight Sets the weight of the font normal, bold, bolder, lighter, 100, 200, 300, 400, 500, 600, 700, 800, 900 font-variant Displays text in a small-caps font or a normal font normal, small-caps text-transform Displays text in all caps, no caps, or first letter capitalized none, capitalize, uppercase, lowercase text-align Aligns left, right, or center left, right, center color Sets the color of the font red, blue, black, etc. or Hex code
15. Your turn Change the header’s h1 element to all uppercase Change the p element to size 12px Arial
16. What are classes? Let you style the same element in different ways Defined in the style sheet with a . instead of # <p class="byline">By John Smith • February 20, 2009</p> <p>...</p> HTML CSS p.byline { color: gray; }
17. Images Add an image and style it <div id="content"> <h1>...</h1> <p>...</p> <div class="img-right"> <img src="images/photo.jpg" alt="Photo description" /> </div> <p>...</p> </div> HTML CSS img { border: 1px solid gray; padding: 1px; } .img-right { float: right; margin-left: 10px; }
18. Image captions Add a caption class and style it <div id="content"> <div class="img-right"> <img src="images/photo.jpg" alt="Photo description" /> <p class="caption">This is a caption.</p> </div> <p>...</p> </div> HTML CSS p.caption { font-size: 10px; }
19. White space Breathing room for content enhances readability Three ways to add white space: Property Description Sample Values line-height Sets the distance between lines. 1.4em, 14pt, 140% padding Defines the space between the element border and the element content (interior). 10px, 10% margin Defines the space around the elements (exterior). 10px, 10%
20. Your turn Increase the spacing between lines Add padding to the left of the content div to move it away from the edge Add padding to the top and left of the header text to move it away from the edge Add a margin to the left of the image to create space between image and text
21. Styling on the fly In-line styles override external style sheets <div id="content"> <p style="float:left; width:150px; color:gray;">Style me!</p> </div> HTML
22. Your turn Style a pull-quote on the fly using the following properties: Float to the left 10px right margin, 10px padding 150px width Text aligned in the center 16px italicized Georgia font Gray font color 1px solid gray border top and bottom