Web Programming Lecture 4 PDF
Document Details

Uploaded by ReformedConnemara2249
Benha University
Dr. Mohamed Taha
Tags
Summary
This lecture provides a comprehensive introduction to the fundamentals of web programming, focusing on the application of Cascading Style Sheets (CSS). It explores different types of CSS, including inline, embedded, and external styles, and demonstrates their use in modifying the visual presentation of web pages, focusing on color and text manipulation through CSS properties and selectors.
Full Transcript
Web Programming By Dr. Mohamed Taha Lecturer, Computer Science Department, Faculty of Computers & Artificial Intelligence, Benha University Lecture No.1 4 Text Book Chapter 3 Configuring Color and Text with CS...
Web Programming By Dr. Mohamed Taha Lecturer, Computer Science Department, Faculty of Computers & Artificial Intelligence, Benha University Lecture No.1 4 Text Book Chapter 3 Configuring Color and Text with CSS Ninth Edition Learning Objectives (1 of 2) –3.1 Describe the evolution of style sheets from print media to the Web –3.2 List advantages of using Cascading Style Sheets –3.3 Use color on web pages –3.4 Create style sheets that configure common color and text properties –3.5 Apply inline styles Learning Objectives (2 of 2) –3.6 Use embedded style sheets –3.7 Use external style sheets –3.8 Configure element, class, id, and contextual selectors –3.9 Utilize the “cascade” in CSS –3.10 Validate CSS Overview of Cascading Style Sheets (CSS) See what is possible with CSS: Visit http://www.csszengarden.com/ Style Sheets used for years in Desktop Publishing apply typographical styles and spacing to printed media CSS provides the functionality of style sheets (and much more) for web developers a flexible, cross-platform, standards-based language developed by the W3C. CSS Advantages Greater typography and page layout control Style is separate from structure Styles can be stored in a separate document and associated with the web page Potentially smaller documents Easier site maintenance Types of Cascading Style Sheets Inline Styles Embedded Styles External Styles Imported Styles Cascading Style Sheets (1 of 2) Inline Styles – body section – HTML style attribute – apply only to the specific element Embedded Styles – head section – HTML style element – apply to the entire web page document Cascading Style Sheets (2 of 2) External Styles – Separate text file with.css file extension – Associate with a HTML link element in the head section of a web page Imported Styles – Similar to External Styles – We’ll concentrate on the other three types of styles. CSS Syntax Style sheets are composed of “Rules” that describe the styling to be applied. Each Rule contains a Selector and a Declaration CSS Syntax Sample Configure a web page to display blue text and yellow background. This could also be written using hexadecimal color values as shown below. Common Formatting CSS Properties See Table 3.1 (see slide 13,14,15 and 16) Common CSS Properties, including: o background-color o color o font-family o font-size o font-style o font-weight o line-height o margin o text-align o text-decoration o width Table 3.1 CSS Properties (1 of 4) Property Description Values background- Background color of Any valid color color an element color Foreground (text) Any valid color color of an element font-family Name of a font or font Any valid font or a font family such as serif, sanserif, fantasy, family monospace, or cursive font-size Size of the font Varies; a numeric value with pt (standard font point sizes) or px (pixels) units or the unit em (which corresponds to the width of the uppercase M of the current font); a numeric percentage; and the text values xx-small, x-small, small, medium, large, x-large, and xx-large font-style Style of the font normal, italic, or oblique Table 3.1 CSS Properties (2 of 4) Property Description Values font-weight The “boldness” or Varies; the text values normal, bold, bolder, and lighter and the weight of the font numeric values 100, 200, 300, 400, 500, 600, 700, 800, and 900 letter- The space between A numeric value (px or em) or normal (default) spacing characters line-height The spacing allowed It is most common to use a percentage for this value; for example, for the line of text a value of 200% would correspond to double-spacing. Margin Shorthand notation to A numeric value (px or em); for example, body {margin: 10px} will configure the margin set the page margins in the document to 10 pixels. When surrounding an eliminating the margin, do not use the px or em unit—for element example, body {margin:0} Table 3.1 CSS Properties (3 of 4) Property Description Values margin-left Configures the space in the left margin A numeric value (px or em), auto, or 0 of the element margin-right Configures the space in the right A numeric value (px or em), auto, or 0 margin of the element text-align The alignment of text center, justify, left, or right text-decoration Determines whether text is The value “none” will cause a hyperlink not to underlined; this style is most often be underlined in a browser that normally applied to hyperlinks processes in this manner text-indent Configures the indentation of the first Numeric value (px or em) or percentage line of text Table 3.1 CSS Properties (4 of 4) Property Description Values text-shadow Configures a drop shadow on the text Two to four numerical values (px or em) to displayed within an element. This indicate horizontal offset, vertical offset, blur CSS3 property is not supported in all radius (optional), and spread distance (optional), browsers. and a valid color value. text-transform Configures the capitalization of text none (default), capitalize, uppercase, or lowercase white-space Configures the display of whitespace normal (default), nowrap, pre, pre-line, pre- wrap width The width of the content of an A numeric value (px or em), numeric percentage, element or auto (default) word-spacing The space between words A numeric value (px or em) or normal (default) Using Color on Web Pages Computer monitors display color as intensities of red, green, and blue light RGB Color The values of red, green, and blue vary from 0 to 255. Hexadecimal numbers (base 16) represent these color values. Hexadecimal Color Values # indicates a hexadecimal value Hex value pairs range from 00 to FF Three hex value pairs describe an RGB color Web Color Palette A collection of 216 colors Display the most similar on the Mac and PC platforms Hex values: 00, 33, 66, 99, C C, FF Color Chart http://webdevfoundations.net/color Making Color Choices How to choose a color scheme? Monochromatic − http://meyerweb.com/eric/tools/color-blend Choose from a photograph or other image − http://www.colr.org Begin with a favorite color − Use one of the sites below to choose other colors ▪ http://colorsontheweb.com/colorwizard.asp ▪ https://color.adobe.com/create/color-wheel ▪ http://paletton.com Support Web Accessiblity Verify Sufficient Contrast When you choose colors for text and background, sufficient contrast is needed so that the text is easy to read. Use one of the following online tools to verify contrast: http://webaim.org/resources/contrastchecker http://snook.ca/technical/colour_contrast/colour.html http://juicystudio.com/services/luminositycontrastratio.php Configuring Color with Inline CSS (1 of 2) Inline CSS o Configured in the body of the web page o Use the style attribute of an HTML tag o Apply only to the specific element The Style Attribute o Value: one or more style declaration property and value pairs o Example: configure red color text in an Configuring Color with Inline CSS (2 of 2) Example 2: configure the red text in the heading configure a gray background in the heading Separate style rule declarations with; CSS Embedded (Internal) Styles Configured in the head section of a web page. Use the HTML Apply to the entire web page document Style declarations are contained between the opening and closing Example: Configure a web page with white text on a black background CSS Embedded Styles The body selector sets the global style rules for the entire page. These global rules are overridden for elements by the h1 and h2 style rules. Checkpoint 3.1 1. List three reasons to use CSS on a web page. 2. When designing a page that uses colors other than the default colors for text and background, explain why it is a good reason to configure style rules for both text color and background color. 3. Describe one advantage to using embedded styles instead of inline styles. Configuring Text with CSS CSS properties for configuring text: o font-weight − Configures the boldness of text o font-style − Configures text to an italic style o font-size − Configures the size of the text o font-family − Configures the font typeface of the text The Font-Size Property Accessibility Recommendation: Use em or percentage font sizes – these can be easily enlarged in all browsers by users em = ephemeral unit px = Pixels Pt = Point The Font-Family Property Font Family Category Font Typeface serif Times New Roman, Georgia, Times sans-Serif Arial, Verdana, Geneva monospace Courier new, Lucida Console cursive Brush Script M7 , Comic sans MS fantasy Jokeman, Curlz MT Not everyone has the same fonts installed in their computer Configure a list of fonts and include a generic family name Embedded Styles Example More CSS Text Properties (1 of 2) o line-height – Configures the height of the line of text (use the value 200% to appear double-spaced) o text-align – Configures alignment of text within a block display element o text-indent – Configures the indentation of the first line of text o text-decoration – Modifies the appearance of text with an underline, overline, or line- through More CSS Text Properties (2 of 2) o text-transform – Configures the capitalization of text o letter-spacing – Configures space between text characters o word-spacing – Configures space between words o text-shadow – Configures a drop shadow on text CSS Selectors CSS style rules can be configured for an: CSS Selectors HTML element descendant class selector id selector selector selector CSS Selectors CSS style rules can be configured for an: CSS Selectors HTML element descendant class selector id selector selector selector Using CSS with “HTML element selector” CSS Selectors CSS style rules can be configured for an: CSS Selectors HTML element descendant class selector id selector selector selector Using CSS with “Class” class Selector o Apply a CSS rule to a certain “class” of elements on a web page o Does not associate the style to a specific HTML element o Configure with.classname o code CSS to create a class called “new” with red italic text. Apply the class: CSS Selectors CSS style rules can be configured for an: CSS Selectors HTML element descendant class selector id selector selector selector Using CSS with “Id” id Selector o Apply a CSS rule to One element on a web page. Configure with #idname o Code CSS to create an id called “new” with red, large, italic text. Apply the id: CSS Selectors CSS style rules can be configured for an: Applications Types HTML element descendant class selector id selector selector selector CSS Descendant Selector Specify an element within the context of its container (parent) element. AKA contextual selector The example configures a green text color only for p tags located within an element assigned to the id named content Advantage of contextual selectors: Reduces the number of classes and ids you need to apply in the HTML Span Element Purpose: configure a specially formatted area displayed in-line with other elements, such as within a paragraph. There is no additional empty space above or below a span – it is inline display. Span Element Example HTML: External Style Sheets (1 of 2) CSS style rules are contained in a text file separate from the HTML documents. The External Style Sheet text file: extension ".css" contains only style rules does not contain any HTML tags External Style Sheets (2 of 2) Multiple web pages can associate with the same external style sheet file. site.css Link Element A self-contained tag Placed in the head section Purpose: associates the external style sheet file with the web page. Example: Using an External Style Sheet External Style Sheet color.css To associate the external style sheet called color.css, the H T M L code placed in the head section is: Checkpoint 3.2 1. Describe a reason to use embedded styles. Explain where embedded styles are placed on a web page. 2. Describe a reason to use external styles. Explain where external styles are placed and how web pages indicate they are using external styles. 3. Write the code to configure a web page to use an external style sheet called “mystyles.css”. Centering Page Content with CSS The “Cascade” W3C CSS Validation http://jigsaw.w3.org/css-validator/ Summary This chapter introduced you to Cascading Style Sheet Rules associated with color and text on web pages. You configured inline styles, embedded styles, and external styles. You applied CSS style rues to HTML, class, id, and descendent selectors. You are able to submit your CSS to the (W3C) CSS Validation test. Questions 53 54