Cascading Style Sheet (CSS) Tutorial PDF

Document Details

InstructiveNephrite4861

Uploaded by InstructiveNephrite4861

Universiti Utara Malaysia

Tags

cascading style sheets CSS web development programming

Summary

This document provides a tutorial on Cascading Style Sheets (CSS). It covers topics such as the concept of CSS, its use in web page design, and different ways to implement CSS within HTML documents. The tutorial clarifies multiple aspects of this essential web development skill.

Full Transcript

Cascading Style Sheet Client Side Script: Cascading Style Sheet (CSS)  Objectives  To explain and introduce the concept and use of Client Side Script 2 Introduction to CSS  Cascading: Multiple styles can overlap in order to specify a range of style from...

Cascading Style Sheet Client Side Script: Cascading Style Sheet (CSS)  Objectives  To explain and introduce the concept and use of Client Side Script 2 Introduction to CSS  Cascading: Multiple styles can overlap in order to specify a range of style from a whole web site down to a unique element. Which style gets applied pertains to the rules of CSS cascading logic.  Style: CSS deals specifically with the presentation domain of designing a web page (colour, font, layout, etc).  Sheet: Normally, CSS is a file separate from the HTML file – linked to the HTML file through its (exceptions apply). Why CSS ?  Allows for much richer document appearances than HTML.  Reduce workload by centralizing commands for visual appearance instead of scattered throughout the HTML doc.  Use same style on multiple pages.  Reduce page download size. Use HTML for content; CSS for Presentation. Reference CSS from HTML siti.css hello.html. 1. External Style Sheet An external style sheet can be written in any text editor. The file should NOT contain any html tags. The style sheet should be saved with a.css extension. An example of a style sheet file is shown below: my_style.css hr { color:green; } body { background-image: url("images/back40.gif"); }. 1. External siti.html How to Insert CSS into HTML 2. Internal Style Sheet An internal style sheet should be used when a single document has a unique style. Define internal styles in the head section of an HTML page, by using the tag, like this: hr { color:green; } p { margin-left: 20px; } body { background-image: url("images/tree.png"); } CSS 2. Internal Style Sheet How to Insert CSS into HTML 3. Inline Style Sheet  An inline style loses many of the advantages of style sheets by mixing content with presentation.  Use this method sparingly!  To use inline styles you use the style attribute in the relevant tag.  The style attribute can contain any CSS property.  The example shows how to change the color and the left margin of a paragraph: This is a paragraph. How to Insert CSS into HTML Multiple Style Sheet (External + Internal + Inline)  If some properties have been set for the same selector in different style sheets, the values will be inherited from the more specific style sheet.  For example, an external style sheet has these properties for the h3 selector: h3 { color:red; text-align:left; font-size:8pt; }  And an internal style sheet has these properties for the h3 selector: h3 {text-align:right; font-size:20pt; }  If the page with the internal style sheet also links to the external style sheet the properties for h3 will be: color: red; text-align:right; font-size:20pt; How to Insert CSS into HTML Multiple Style Sheet (External + Internal + Inline)  The color is inherited from the external style sheet and the text-alignment and the font-size is replaced by the internal style sheet. How to Insert CSS into HTML Multiple Style Sheet (External + Internal + Inline)  Styles can be specified:  inside an HTML element  inside the head section of an HTML page  in an external CSS file  Cascading order  What style will be used when there is more than one style specified for an HTML element?  All the styles will "cascade" into a new "virtual" style sheet by the following rules, where number four has the highest priority: a. Browser default b. External style sheet c. Internal style sheet (in the head section) d. Inline style (inside an HTML element)  An inline style (inside an HTML element) has the highest priority, which means that it will override a style defined inside the tag, or in an external style sheet, or in a browser (a default value). CSS Selectors  Select elements to apply a declared style.  Selector types:–  Element Selectors: selects all elements of a specific type (, , , etc.)  Class Selectors: selects all elements that belong to a given class.  ID Selectors: selects a single element that’s been given a unique id.  Pseudo Selectors: combines a selector with a user activated state (:hover, :link, :visited) CSS Selectors  Element Selector:–  Finds all HTML elements that have the specified element type.  Example: h1 { color: blue; }  Finds all elements of type and makes the text color blue. CSS Selectors  Class Selector:–  Finds all elements of a given class – based on theattribute’s class value.  Syntax:.classname (Remember the dot means class selector)  Example:.legs { font-weight: bold; background: pink;}  Finds all elements whose class = “legs” and makes their font bold and their backgrounds pink. CSS Selectors  Id Selector:–  Finds a single element that’s been given aunique id – based on the attribute’s id value.  Syntax: #idname (Remember the hash-sign means id selector)  Example: #snout { border: solid red;}  Finds a single element whose id = “snout” and gives it a solid red border. CSS Selectors  Pseudo Selector:–  Apply styles to a user activated state of an element.  If used, must be declared in a specific order in the style sheet.  General Purpose Pseudo-Selector: :hover Element with mouse over  Specific to hyperlinks (and/or buttons) a:active A link or button that is currently being clicked on a:link A link that has NOT been visited yet. a:visited. A link that HAS been visited. CSS  https://www.w3schools.com/css/css_intro.asp  https://www.freecodecamp.org/news/css-example-css3/ NEXT LAB NEXT LAB NEXT LAB NEXT LAB NEXT LAB Q& A QUESTION ANSWER

Use Quizgecko on...
Browser
Browser