SlideShare a Scribd company logo
1 of 78
http://schoolacademy.telerik.com Introduction to Cascading Style Sheets (CSS) Svetlin Nakov Telerik Corporation www.telerik.com
Table of Contents What is CSS? Styling with Cascading Stylesheets (CSS) Selectors and style definitions Linking HTML and CSS Fonts, Backgrounds, Borders The Box Model in W3C and IE Alignment, Z-Index, Margin, Padding Positioning and Floating Elements Visibility, Display, Overflow 2
CSS: A New Philosophy Separate content from presentation! 3 Content  (HTML document) Presentation (CSS Document) Title Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at pede ut purus malesuada dictum. Donec vitae neque non magna aliquam dictum. ,[object Object]
 accumsan accumsan. Morbi at
 arcu vel elit ultricies porta. Proin tortor purus, luctus non, aliquam nec, interdum vel, mi. Sed nec quam nec odio lacinia molestie. Praesent augue tortor, convallis eget, euismod nonummy, lacinia ut, risus.  Bold Italics Indent
The Resulting Page 4 Title Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Suspendisse at pede ut purus malesuada dictum. Donec vitae neque non magna aliquam dictum. ,[object Object]
 accumsan accumsan. Morbi at
 arcu vel elit ultricies porta. ProinTortor purus, luctus non, aliquam nec, interdum vel, mi. Sed nec quam nec odio lacinia molestie. Praesent augue tortor, convallis eget, euismod nonummy, lacinia ut, risus.
CSS Intro Styling with Cascading Stylesheets
CSS Introduction Cascading Style Sheets (CSS) Markup language, used to describe the presentation of document Defines sizes, fonts, colors, layout, etc. Improves content accessibility Improves flexibility Designed to separate presentation from content Because of CSS all HTML presentation tags are deprecated, e.g. <font>, <center>, <b> etc. 6
CSS Introduction (2) CSS can be applied to any XML document Not just to HTML / XHTML CSS can specify different styles for different rendering methods On-screen In print Etc. … even by voice or Braille-based reader 7
Why “Cascading”? Priority scheme determining which style rules apply to element Cascade priorities or weights are calculated and assigned to the rules Child elements in the HTML DOM tree inherit the styles from their parent Can override them Control via !important rule 8
Why “Cascading”? (2) 9
Style Sheets Syntax CSS has simple syntax, based on English words Contains a set of cascading rules Each rule consists of one or more selectors and declaration block Declaration block consists of one or more semicolon-terminated declarations in curly braces Declaration consists of property, a colon and value 10 h1,h2,h3,h4,h5,h6 { color: green }
Style Sheets Syntax (2) Selectors determine which element the rule applies to:  All elements of specific type Those that mach specific attribute Elements may be matched depending on how they are nested in the document (HTML) Examples: 11 h1 .title { color: green } #menu li { padding-top: 8px }
Style Sheets Syntax (3) Pseudo-classes define further behavior Appended to a selector  :hover, :first-letter, :visited, :active, :before, :after Not all browsers support them fully 12 a:link {text-decoration: none}a:visited {text-decoration: none}a:active {text-decoration: none}a:hover {text-decoration: underline; color: red} .title:before { content: "»" } .title:after { content: "«" }
Style Sheets Syntax (4) Three primary types of selectors: By tag: By element id: By element class name (only for HTML):  Selectors can be combined with commas: This will match <h1> tags, elements with class link and element with id top-link 13 h1 {font-face: Verdana} #element_id {color:#FF0000} .class_name {border: 1px solid red} h1, .link, #top-link {font-weight: bold}
Style Sheets Syntax (5) Match relative to element placement: This will match all <a> tags that are inside of <p> * – universal selector: This will match all child nodes of <p> tag + selector – used to match “the following” tag: 	This will match all elements with class name link that appear immediately after <img> tag 14 p a {text-decoration: underline} p * {color: black} img + .link {float:right}
Style Sheets Syntax (5) > selector – matches direct child nodes of element: 	This will match all elements with class error, direct children of <p> tag [] – match tag attributes by regular expression: This will match all <img> tags with alt attribute containing the word logo There are more rules to select attributes Not well supported in all browsers 15 p > .error {font-size: 8px} img[alt~=logo] {border: none}
Default Browser Styles Browsers have default CSS styles Used when there is no CSS information or any other style information in the document Silently inherited in all documents Caution: default styles differ in browsers E.g. Firefox default page background is white, while IE7 uses about 5% gray background 16
Linking HTML and CSS HTML (content) and CSS (presentation) can be linked in three ways: Inline: the CSS rules in the style attribute No selectors are needed Embedded: in the HTML in <style> tag External: CSS rules are in separate file  Usually a file with .css extension Linked via <linkrel="stylesheet"href=…>tag or @import directive in embedded CSS block 17
Linking HTML and CSS (2) Inline styles have highest priority Then are the embedded styles External styles are last Using external files is highly recommended Simplify the HTML document  Benefit from browser's cache Inline styles are about to be deprecated by the W3C 18
Inline Styles Inline CSS styles Individual element’s style defined using styleattribute Contains only declaration, no selectors: Override any other styles Apply to all descendant elements Used for styles that are not needed anywhere else in the Web site 19 <p style="font-size:20pt; color: #0000FF">
Inline Styles: Example 20 inline-styles.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>   <title>Inline Styles</title> </head> <body>   <p>Here is some text</p> <!--Separate multiple styles with a semicolon-->   <p style="font-size: 20pt">Here is some     more text</p>   <p style="font-size: 20pt;color:     #0000FF" >Even more text</p>  </body> </html>
Inline Styles: Example 21 inline-styles.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>   <title>Inline Styles</title> </head> <body>   <p>Here is some text</p> <!--Separate multiple styles with a semicolon-->   <p style="font-size: 20pt">Here is some     more text</p>   <p style="font-size: 20pt;color:     #0000FF" >Even more text</p>  </body> </html>
CSS Rules Precedence Inline CSS rules have precedence over the external CSS rules: 22 precedence.html <!DOCTYPE html …> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>CSS Rules Precedence - Example</title>     <style type="text/css"> span {color:red} </style>     <link type="text/css" rel="stylesheet" href="" /> </head> <body>     <span>Some text</span>     <span style="color:Blue">Some text</span> </body> </html>
CSS Rules Precedence Inline CSS rules have precedence over the external CSS rules: 23 precedence.html <!DOCTYPE html …> <html xmlns="http://www.w3.org/1999/xhtml" > <head>     <title>CSS Rules Precedence - Example</title>     <style type="text/css"> span {color:red} </style>     <link type="text/css" rel="stylesheet" href="" /> </head> <body>     <span>Some text</span>     <span style="color:Blue">Some text</span> </body> </html>
Embedded Styles Embedded in the HTML in the <style> tag: The <style> tag is placed in the <head> section of the document Styles apply to the whole document type attribute specifies the MIME type MIME is a describes the format of the content Other MIME types include text/html, image/gif, text/javascript … Used for document-specific styles 24 <style type="text/css">
Embedded Styles: Example 25 embedded-stylesheets.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>   <title>Style Sheets</title>   <style type="text/css">     em {background-color:#8000FF; color:white}     h1 {font-family:Arial, sans-serif}     p  {font-size:18pt}     .blue {color:blue}   </style> <head>
Embedded Styles: Example (2) 26 … <body>   <h1 class="blue">A Heading</h1>   <p>Here is some text. Here is some text. Here   is some text. Here is some text. Here is some   text.</p>         <h1>Another Heading</h1>           <p class="blue">Here is some more text.   Here is some more text.</p>   <p class="blue">Here is some <em>more</em>   text. Here is some more text.</p>   </body> </html>
… <body>   <h1 class="blue">A Heading</h1>   <p>Here is some text. Here is some text. Here   is some text. Here is some text. Here is some   text.</p>         <h1>Another Heading</h1>           <p class="blue">Here is some more text.   Here is some more text.</p>   <p class="blue">Here is some <em>more</em>   text. Here is some more text.</p>   </body> </html> Embedded Styles: Example (3) 27
External CSS Styles External linking Separate pages can all use shared style sheet Only modify a single file to change the styles across your entire Web site link tag (with rel attribute) Specifies a relationship between current document and another document link element can stay only in the HTML header 28 <link rel="stylesheet" type="text/css"   href="styles.css">
External CSS Styles (2) @import Another way to link external CSS files Example: Not all browsers recognize such rules You can specify this way browser-specific styles (IE6 ignores the @import) 29 <style type="text/css">   @import url(styles.css); </style>
External Styles: Example 30 styles.css /* CSS Document */ a 	  { text-decoration: none } a:hover { text-decoration: underline;           color: red;           background-color: #CCFFCC } li em   { color: red;            font-weight: bold } ul	  { margin-left: 2cm } ul ul	  { text-decoration: underline;            margin-left: .5cm }
External Styles: Example (2) 31 external-styles.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0    Transitional//EN"    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head>   <title>Importing style sheets</title>   <link type="text/css" rel="stylesheet"     href="styles.css"  /> </head> <body>   <h1>Shopping list for <em>Monday</em>:</h1>   <li>Milk</li>   …
External Styles: Example (3) 32   …   <li>Bread     <ul>       <li>White bread</li>       <li>Rye bread</li>       <li>Whole wheat bread</li>     </ul>   </li>   <li>Rice</li>   <li>Potatoes</li>   <li>Pizza <em>with mushrooms</em></li> </ul> <a href="http://food.com" title="grocery   store">Go to the Grocery store</a> </body> </html>
  …   <li>Bread     <ul>       <li>White bread</li>       <li>Rye bread</li>       <li>Whole wheat bread</li>     </ul>   </li>   <li>Rice</li>   <li>Potatoes</li>   <li>Pizza <em>with mushrooms</em></li> </ul> <a href="http://food.com" title="grocery   store">Go to the Grocery store</a> </body> </html> External Styles: Example (4) 33
Values in the CSS Rules Colors are specified in RGB format, in hex form:  Example: #A0A6AA Predefined color aliases exist: black, blue, etc. Numeric values are specified in: Pixels, e.g. 12px Points, e.g. 10pt Inches, centimeters, millimeters E.g. 1in, 1cm, 1mm Percentages, e.g. 50% Percentage is relative to the parent element 34
CSS Rules for Fonts color – specifies the color of the text font-size – size of font: xx-small, x-small, small, medium, large, x-large, xx-large, smaller, larger or numeric value font-family – comma separated font names Example: Verdana, Sans-Serif, etc.  The browser loads the first one that is available There should always be at least one serif font font-weight –normal, bold, bolder, lighter or number in range [100…900] 35
CSS Rules for Fonts (2) font-style – styles the font Values: normal, italic, oblique text-decoration – decorates the text Values: none, underline, line-trough, overline, blink text-align – defines the alignment of text or other content Values: left, right, center, justify 36
Short Font Rules font Shorthand rule for setting multiple font properties at the same time 	is equal to writing this: 37 font: italic normal bold 12px Verdana; font-style: italic; font-variant: normal; font-weight: bold; font-size:12px; font-family: Verdana;
Fonts Live Demo font-rules.html
Background CSS Rules background-color Solid color background background-image URL of image to be used as background, e.g.: background-repeat repeat-x, repeat-y, repeat, no-repeat background-attachment fixed / scroll 39 background-image:url("back.gif");
Background CSS Rules (2) background-position: specifies vertical and horizontal position of the background image Vertical position: top, center, bottom Horizontal position: left, center, right Both can be specified in percentage or other numerical values Examples: 40 background-position: top left; background-position: -5px 50%;
Background Short Rule background: shorthand rule for setting background properties at the same time: 	is equal to writing: Some browsers will not apply BOTH color and image for background if using shorthand rule 41 background: #FFF0C0 url("back.gif") no-repeat fixed top; background-color: #FFF0C0; background-image: url("back.gif"); background-repeat: no-repeat; background-attachment: fixed; background-position: top;
Background-image or <img>? Background images allow you to save many image tags from the HTML  Leads to less code More content-oriented approach All images that are not part of the page content should be moved to the CSS 42
Background Styles Live Demo background-rules.html
Border CSS Rules border-width: thin, medium, thick or numerical value (e.g. 10px) border-color: color alias or RGB value border-style: none, hidden, dotted, dashed, solid, double, groove, ridge, inset, outset Each property can be defined separately for left, top, bottom and right border-top-style, border-left-color, … 44
Border Short Rules border: shorthand rule for setting border properties at once: 	is equal to writing: Specify different borders for the sides via shorthand rules: border-top, border-left, border-right, border-bottom 45 border: 1px solid red; border-width:1px; border-color:red; border-style:solid;
Borders Live Demo border-rules.html
Width, Height CSS Rules width – defines numerical value for the width of element, e.g. 200px height – defines numerical value for the height of element, e.g. 100px Important: not all elements and browsers follow this value! Usually the height of an element is defined by its content Common mistake is to apply height to tables or table cells 47
Width / Height Live Demo size-rules.html
Margin and Padding margin and padding define the spacing around the element Numerical value, e.g. 10px or -5px Can be defined for each of the four sides separately margin-top, padding-left, … margin is the spacing outside of the border padding is the spacing between the border and the content 49
Margin and Padding: Short Rules margin: 5px; Sets all four sides to have margin of 5 px; margin: 10px 20px; Sets margins: top and bottom to 10px, left and right to 20px; margin: 1px 3px 5px 7px; Sets top, right, bottom, left margins to 1px, 3px, 5px and 7px respectively Same shorthand rules apply for padding 50
The Box Model 51
Gecko and W3C vs. IE Major difference between browsers when applying border, padding and width/height To avoid you need either “CSS hacks” or just don’t specify for the same element width/height and padding or border,  different than 0 52
Margins and Paddings Live Demo margins-paddings-rules.html
CSS Rules for Positioning position: defines the positioning of the element, depending on the parent elements  The value is one of: static (default) relative – relative position according to where the element would appear with static position absolute – position according the parent element fixed – fix element on screen, ignore page scrolling 54
CSS Rules for Positioning (2) Fixed and absolute positioned elements “float” over the rest of elements Moved to separate document layer Their position and size is ignored when calculating the size of parent element or position of surrounding elements Ordered by their z-index 55
Positioning Live Demo positioning-rules.html
CSS Rules for Positioning top, left, bottom, right: specifies offset of absolute/fixed/relative positioned element as numerical values vertical-align: sets the vertical-alignment of an element Usually used for table cells Values: baseline, sub, super, top, text-top, middle, bottom, text-bottom or numeric z-index: specifies the depth-order of element 57
Alignment and Z-Index Live Demo alignments-and-z-index-rules.html
Float CSS Rule float: the element “floats” above the elements similar to position:absolute and the align HTML property of <img> tag  element is taken into account when rendering the surrounding text and elements left: places the element on the left right: places the element on the right 59
Clear CSS Rule clear Sets the sides of the element where other floating elements are NOT allowed Possible values: left, right, both This rule can be applied only to “floating” elements 60
Floating Elements Live Demo float-rules.html
Opacity CSS Rule opacity: specifies the opacity of the element Floating point number from 0 to 1 Supported only by Safari browser  For Mozilla use –moz-opacity CSS rule For IE use filter:alpha(opacity=value) where value is from 0 to 100 Need to apply all three rules 62
Opacity Live Demo opacity-rule.html
Visibility CSS Rule visibility Determines whether the element is visible hidden: element is not rendered, but still takes place in the rendering (acts like opacity:0) visible: element is rendered normally 64
Visibility Live Demo visibility-rule.html
Display CSS Rule display: controls the display of the element and the way it is rendered and if breaks should be placed before and after the element inline: no breaks are placed before and after (<span> is inline element) block:  breaks are placed before AND after the element (<div> is block element) 66
Display CSS Rule (2) none: element is hidden and its dimensions are not used to calculate the surrounding elements rendering (differs from visibility:hidden!) There are some more possible values, but not all browsers support them Specific displays like table-cell and table-row 67
Display Live Demo display-rule.html
Overflow CSS Rule overflow: defines the behavior of element when content needs more space than you have specified by the size properties or for other reasons. Values:  visible (default) – element size is increased to make space for content or the content “overflows” out of the element scroll – show horizontal/vertical scroll bar in the element hidden – any content in the element that cannot be placed inside is hidden 69
Overflow Live Demo overflow-rule.html
Other CSS Rules cursor:  specifies the look of the mouse cursor when placed over the element  Values: crosshair, help, pointer, progress, move, hair, col-resize, row-resize, text, wait, copy, drop, and others white-space – controls the line breaking of text. Value is one of: nowrap – keeps the text on one line normal (default) – browser decides whether to brake the lines if needed 71
Benefits of using CSS More powerful formatting than using presentation tags Your pages load faster, because browsers cache the .css files Increased accessibility, because rules can be defined according given media Pages are easier to maintain and update 72
Maintenance Example 73 Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. one CSS file Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css. Title Some random text here.  You can’t read it anyway!  Harharhar!  Use Css.
What to Avoid Don’t use <font> or other presentation tags to change the look of your text <font color=...> <b>text that is bold <center> this text is centered Do not use complex CSS rules-sets because may slow down page rendering Move as much as possible to external files Avoid inline CSS 74

More Related Content

What's hot

Introduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSIntroduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSJussi Pohjolainen
 
HTML presentation for beginners
HTML presentation for beginnersHTML presentation for beginners
HTML presentation for beginnersjeroenvdmeer
 
IPW HTML course
IPW HTML courseIPW HTML course
IPW HTML courseVlad Posea
 
Lecture 2 introduction to html
Lecture 2  introduction to htmlLecture 2  introduction to html
Lecture 2 introduction to htmlpalhaftab
 
An Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptAn Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptFahim Abdullah
 
Html Intro2
Html Intro2Html Intro2
Html Intro2mlackner
 
DIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningDIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningRasan Samarasinghe
 
Web I - 02 - XHTML Introduction
Web I - 02 - XHTML IntroductionWeb I - 02 - XHTML Introduction
Web I - 02 - XHTML IntroductionRandy Connolly
 
HTML Web design english & sinhala mix note
HTML Web design english & sinhala mix noteHTML Web design english & sinhala mix note
HTML Web design english & sinhala mix noteMahinda Gamage
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTMLMayaLisa
 
Origins and evolution of HTML and XHTML
Origins and evolution of HTML and XHTMLOrigins and evolution of HTML and XHTML
Origins and evolution of HTML and XHTMLHowpk
 
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJSBasic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJSDeepak Upadhyay
 
HTML & CSS
HTML & CSSHTML & CSS
HTML & CSSjlinabary
 
Introduction To HTML
Introduction To HTMLIntroduction To HTML
Introduction To HTMLMehul Patel
 
Intro to HTML5
Intro to HTML5Intro to HTML5
Intro to HTML5Vlad Posea
 
Basic Html Knowledge for students
Basic Html Knowledge for studentsBasic Html Knowledge for students
Basic Html Knowledge for studentsvethics
 

What's hot (20)

Introduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSSIntroduction to XML, XHTML and CSS
Introduction to XML, XHTML and CSS
 
HTML presentation for beginners
HTML presentation for beginnersHTML presentation for beginners
HTML presentation for beginners
 
IPW HTML course
IPW HTML courseIPW HTML course
IPW HTML course
 
Lecture 2 introduction to html
Lecture 2  introduction to htmlLecture 2  introduction to html
Lecture 2 introduction to html
 
HTML
HTMLHTML
HTML
 
An Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java ScriptAn Overview of HTML, CSS & Java Script
An Overview of HTML, CSS & Java Script
 
Html Intro2
Html Intro2Html Intro2
Html Intro2
 
DIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web DesigningDIWE - Coding HTML for Basic Web Designing
DIWE - Coding HTML for Basic Web Designing
 
Web I - 02 - XHTML Introduction
Web I - 02 - XHTML IntroductionWeb I - 02 - XHTML Introduction
Web I - 02 - XHTML Introduction
 
HTML Web design english & sinhala mix note
HTML Web design english & sinhala mix noteHTML Web design english & sinhala mix note
HTML Web design english & sinhala mix note
 
Introduction to HTML
Introduction to HTMLIntroduction to HTML
Introduction to HTML
 
Origins and evolution of HTML and XHTML
Origins and evolution of HTML and XHTMLOrigins and evolution of HTML and XHTML
Origins and evolution of HTML and XHTML
 
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJSBasic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
Basic of HTML, CSS(StyleSheet), JavaScript(js), Bootstrap, JSON & AngularJS
 
Web Development using HTML & CSS
Web Development using HTML & CSSWeb Development using HTML & CSS
Web Development using HTML & CSS
 
HTML & CSS
HTML & CSSHTML & CSS
HTML & CSS
 
Tags in html
Tags in htmlTags in html
Tags in html
 
Introduction To HTML
Introduction To HTMLIntroduction To HTML
Introduction To HTML
 
Intro to HTML5
Intro to HTML5Intro to HTML5
Intro to HTML5
 
Basic Html Knowledge for students
Basic Html Knowledge for studentsBasic Html Knowledge for students
Basic Html Knowledge for students
 
HTML
HTMLHTML
HTML
 

Similar to CSS Intro Styling with Cascading Stylesheets

Similar to CSS Intro Styling with Cascading Stylesheets (20)

CSS Part I
CSS Part ICSS Part I
CSS Part I
 
CSS.pptx
CSS.pptxCSS.pptx
CSS.pptx
 
Week3 css
Week3 cssWeek3 css
Week3 css
 
Advanced Cascading Style Sheets
Advanced Cascading Style SheetsAdvanced Cascading Style Sheets
Advanced Cascading Style Sheets
 
Css
CssCss
Css
 
Chapter 4a cascade style sheet css
Chapter 4a cascade style sheet cssChapter 4a cascade style sheet css
Chapter 4a cascade style sheet css
 
Casc Style Sheets Ii
Casc Style Sheets IiCasc Style Sheets Ii
Casc Style Sheets Ii
 
CSS Overview
CSS OverviewCSS Overview
CSS Overview
 
XHTML and CSS
XHTML and CSS XHTML and CSS
XHTML and CSS
 
Web Designing
Web DesigningWeb Designing
Web Designing
 
Web technology Unit-II Part-C
Web technology Unit-II Part-CWeb technology Unit-II Part-C
Web technology Unit-II Part-C
 
Advanced Web Programming Chapter 8
Advanced Web Programming Chapter 8Advanced Web Programming Chapter 8
Advanced Web Programming Chapter 8
 
Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01Cascading Style Sheets - Part 01
Cascading Style Sheets - Part 01
 
Web Designing
Web DesigningWeb Designing
Web Designing
 
Css introduction
Css introductionCss introduction
Css introduction
 
ch04-css-basics_final.ppt
ch04-css-basics_final.pptch04-css-basics_final.ppt
ch04-css-basics_final.ppt
 
CSS
CSSCSS
CSS
 
Css 2010
Css 2010Css 2010
Css 2010
 
Css 2010
Css 2010Css 2010
Css 2010
 
Css
CssCss
Css
 

More from BG Java EE Course

More from BG Java EE Course (20)

Rich faces
Rich facesRich faces
Rich faces
 
JSP Custom Tags
JSP Custom TagsJSP Custom Tags
JSP Custom Tags
 
Java Server Faces (JSF) - advanced
Java Server Faces (JSF) - advancedJava Server Faces (JSF) - advanced
Java Server Faces (JSF) - advanced
 
Java Server Faces (JSF) - Basics
Java Server Faces (JSF) - BasicsJava Server Faces (JSF) - Basics
Java Server Faces (JSF) - Basics
 
JSTL
JSTLJSTL
JSTL
 
Unified Expression Language
Unified Expression LanguageUnified Expression Language
Unified Expression Language
 
Java Server Pages
Java Server PagesJava Server Pages
Java Server Pages
 
Web Applications and Deployment
Web Applications and DeploymentWeb Applications and Deployment
Web Applications and Deployment
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
HTML: Tables and Forms
HTML: Tables and FormsHTML: Tables and Forms
HTML: Tables and Forms
 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
 
Creating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSSCreating Web Sites with HTML and CSS
Creating Web Sites with HTML and CSS
 
Processing XML with Java
Processing XML with JavaProcessing XML with Java
Processing XML with Java
 
Introduction to XML
Introduction to XMLIntroduction to XML
Introduction to XML
 
Data Access with JDBC
Data Access with JDBCData Access with JDBC
Data Access with JDBC
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Introduction to-RDBMS-systems
Introduction to-RDBMS-systemsIntroduction to-RDBMS-systems
Introduction to-RDBMS-systems
 
Basic data-structures-v.1.1
Basic data-structures-v.1.1Basic data-structures-v.1.1
Basic data-structures-v.1.1
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
 
Strings v.1.1
Strings v.1.1Strings v.1.1
Strings v.1.1
 

Recently uploaded

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfTechSoup
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 

Recently uploaded (20)

Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdfInclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
Inclusivity Essentials_ Creating Accessible Websites for Nonprofits .pdf
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 

CSS Intro Styling with Cascading Stylesheets

  • 1. http://schoolacademy.telerik.com Introduction to Cascading Style Sheets (CSS) Svetlin Nakov Telerik Corporation www.telerik.com
  • 2. Table of Contents What is CSS? Styling with Cascading Stylesheets (CSS) Selectors and style definitions Linking HTML and CSS Fonts, Backgrounds, Borders The Box Model in W3C and IE Alignment, Z-Index, Margin, Padding Positioning and Floating Elements Visibility, Display, Overflow 2
  • 3.
  • 5. arcu vel elit ultricies porta. Proin tortor purus, luctus non, aliquam nec, interdum vel, mi. Sed nec quam nec odio lacinia molestie. Praesent augue tortor, convallis eget, euismod nonummy, lacinia ut, risus. Bold Italics Indent
  • 6.
  • 8. arcu vel elit ultricies porta. ProinTortor purus, luctus non, aliquam nec, interdum vel, mi. Sed nec quam nec odio lacinia molestie. Praesent augue tortor, convallis eget, euismod nonummy, lacinia ut, risus.
  • 9. CSS Intro Styling with Cascading Stylesheets
  • 10. CSS Introduction Cascading Style Sheets (CSS) Markup language, used to describe the presentation of document Defines sizes, fonts, colors, layout, etc. Improves content accessibility Improves flexibility Designed to separate presentation from content Because of CSS all HTML presentation tags are deprecated, e.g. <font>, <center>, <b> etc. 6
  • 11. CSS Introduction (2) CSS can be applied to any XML document Not just to HTML / XHTML CSS can specify different styles for different rendering methods On-screen In print Etc. … even by voice or Braille-based reader 7
  • 12. Why “Cascading”? Priority scheme determining which style rules apply to element Cascade priorities or weights are calculated and assigned to the rules Child elements in the HTML DOM tree inherit the styles from their parent Can override them Control via !important rule 8
  • 14. Style Sheets Syntax CSS has simple syntax, based on English words Contains a set of cascading rules Each rule consists of one or more selectors and declaration block Declaration block consists of one or more semicolon-terminated declarations in curly braces Declaration consists of property, a colon and value 10 h1,h2,h3,h4,h5,h6 { color: green }
  • 15. Style Sheets Syntax (2) Selectors determine which element the rule applies to: All elements of specific type Those that mach specific attribute Elements may be matched depending on how they are nested in the document (HTML) Examples: 11 h1 .title { color: green } #menu li { padding-top: 8px }
  • 16. Style Sheets Syntax (3) Pseudo-classes define further behavior Appended to a selector :hover, :first-letter, :visited, :active, :before, :after Not all browsers support them fully 12 a:link {text-decoration: none}a:visited {text-decoration: none}a:active {text-decoration: none}a:hover {text-decoration: underline; color: red} .title:before { content: "»" } .title:after { content: "«" }
  • 17. Style Sheets Syntax (4) Three primary types of selectors: By tag: By element id: By element class name (only for HTML): Selectors can be combined with commas: This will match <h1> tags, elements with class link and element with id top-link 13 h1 {font-face: Verdana} #element_id {color:#FF0000} .class_name {border: 1px solid red} h1, .link, #top-link {font-weight: bold}
  • 18. Style Sheets Syntax (5) Match relative to element placement: This will match all <a> tags that are inside of <p> * – universal selector: This will match all child nodes of <p> tag + selector – used to match “the following” tag: This will match all elements with class name link that appear immediately after <img> tag 14 p a {text-decoration: underline} p * {color: black} img + .link {float:right}
  • 19. Style Sheets Syntax (5) > selector – matches direct child nodes of element: This will match all elements with class error, direct children of <p> tag [] – match tag attributes by regular expression: This will match all <img> tags with alt attribute containing the word logo There are more rules to select attributes Not well supported in all browsers 15 p > .error {font-size: 8px} img[alt~=logo] {border: none}
  • 20. Default Browser Styles Browsers have default CSS styles Used when there is no CSS information or any other style information in the document Silently inherited in all documents Caution: default styles differ in browsers E.g. Firefox default page background is white, while IE7 uses about 5% gray background 16
  • 21. Linking HTML and CSS HTML (content) and CSS (presentation) can be linked in three ways: Inline: the CSS rules in the style attribute No selectors are needed Embedded: in the HTML in <style> tag External: CSS rules are in separate file Usually a file with .css extension Linked via <linkrel="stylesheet"href=…>tag or @import directive in embedded CSS block 17
  • 22. Linking HTML and CSS (2) Inline styles have highest priority Then are the embedded styles External styles are last Using external files is highly recommended Simplify the HTML document Benefit from browser's cache Inline styles are about to be deprecated by the W3C 18
  • 23. Inline Styles Inline CSS styles Individual element’s style defined using styleattribute Contains only declaration, no selectors: Override any other styles Apply to all descendant elements Used for styles that are not needed anywhere else in the Web site 19 <p style="font-size:20pt; color: #0000FF">
  • 24. Inline Styles: Example 20 inline-styles.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Inline Styles</title> </head> <body> <p>Here is some text</p> <!--Separate multiple styles with a semicolon--> <p style="font-size: 20pt">Here is some more text</p> <p style="font-size: 20pt;color: #0000FF" >Even more text</p> </body> </html>
  • 25. Inline Styles: Example 21 inline-styles.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Inline Styles</title> </head> <body> <p>Here is some text</p> <!--Separate multiple styles with a semicolon--> <p style="font-size: 20pt">Here is some more text</p> <p style="font-size: 20pt;color: #0000FF" >Even more text</p> </body> </html>
  • 26. CSS Rules Precedence Inline CSS rules have precedence over the external CSS rules: 22 precedence.html <!DOCTYPE html …> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>CSS Rules Precedence - Example</title> <style type="text/css"> span {color:red} </style> <link type="text/css" rel="stylesheet" href="" /> </head> <body> <span>Some text</span> <span style="color:Blue">Some text</span> </body> </html>
  • 27. CSS Rules Precedence Inline CSS rules have precedence over the external CSS rules: 23 precedence.html <!DOCTYPE html …> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>CSS Rules Precedence - Example</title> <style type="text/css"> span {color:red} </style> <link type="text/css" rel="stylesheet" href="" /> </head> <body> <span>Some text</span> <span style="color:Blue">Some text</span> </body> </html>
  • 28. Embedded Styles Embedded in the HTML in the <style> tag: The <style> tag is placed in the <head> section of the document Styles apply to the whole document type attribute specifies the MIME type MIME is a describes the format of the content Other MIME types include text/html, image/gif, text/javascript … Used for document-specific styles 24 <style type="text/css">
  • 29. Embedded Styles: Example 25 embedded-stylesheets.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Style Sheets</title> <style type="text/css"> em {background-color:#8000FF; color:white} h1 {font-family:Arial, sans-serif} p {font-size:18pt} .blue {color:blue} </style> <head>
  • 30. Embedded Styles: Example (2) 26 … <body> <h1 class="blue">A Heading</h1> <p>Here is some text. Here is some text. Here is some text. Here is some text. Here is some text.</p> <h1>Another Heading</h1> <p class="blue">Here is some more text. Here is some more text.</p> <p class="blue">Here is some <em>more</em> text. Here is some more text.</p> </body> </html>
  • 31. … <body> <h1 class="blue">A Heading</h1> <p>Here is some text. Here is some text. Here is some text. Here is some text. Here is some text.</p> <h1>Another Heading</h1> <p class="blue">Here is some more text. Here is some more text.</p> <p class="blue">Here is some <em>more</em> text. Here is some more text.</p> </body> </html> Embedded Styles: Example (3) 27
  • 32. External CSS Styles External linking Separate pages can all use shared style sheet Only modify a single file to change the styles across your entire Web site link tag (with rel attribute) Specifies a relationship between current document and another document link element can stay only in the HTML header 28 <link rel="stylesheet" type="text/css" href="styles.css">
  • 33. External CSS Styles (2) @import Another way to link external CSS files Example: Not all browsers recognize such rules You can specify this way browser-specific styles (IE6 ignores the @import) 29 <style type="text/css"> @import url(styles.css); </style>
  • 34. External Styles: Example 30 styles.css /* CSS Document */ a { text-decoration: none } a:hover { text-decoration: underline; color: red; background-color: #CCFFCC } li em { color: red; font-weight: bold } ul { margin-left: 2cm } ul ul { text-decoration: underline; margin-left: .5cm }
  • 35. External Styles: Example (2) 31 external-styles.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Importing style sheets</title> <link type="text/css" rel="stylesheet" href="styles.css" /> </head> <body> <h1>Shopping list for <em>Monday</em>:</h1> <li>Milk</li> …
  • 36. External Styles: Example (3) 32 … <li>Bread <ul> <li>White bread</li> <li>Rye bread</li> <li>Whole wheat bread</li> </ul> </li> <li>Rice</li> <li>Potatoes</li> <li>Pizza <em>with mushrooms</em></li> </ul> <a href="http://food.com" title="grocery store">Go to the Grocery store</a> </body> </html>
  • 37. … <li>Bread <ul> <li>White bread</li> <li>Rye bread</li> <li>Whole wheat bread</li> </ul> </li> <li>Rice</li> <li>Potatoes</li> <li>Pizza <em>with mushrooms</em></li> </ul> <a href="http://food.com" title="grocery store">Go to the Grocery store</a> </body> </html> External Styles: Example (4) 33
  • 38. Values in the CSS Rules Colors are specified in RGB format, in hex form: Example: #A0A6AA Predefined color aliases exist: black, blue, etc. Numeric values are specified in: Pixels, e.g. 12px Points, e.g. 10pt Inches, centimeters, millimeters E.g. 1in, 1cm, 1mm Percentages, e.g. 50% Percentage is relative to the parent element 34
  • 39. CSS Rules for Fonts color – specifies the color of the text font-size – size of font: xx-small, x-small, small, medium, large, x-large, xx-large, smaller, larger or numeric value font-family – comma separated font names Example: Verdana, Sans-Serif, etc. The browser loads the first one that is available There should always be at least one serif font font-weight –normal, bold, bolder, lighter or number in range [100…900] 35
  • 40. CSS Rules for Fonts (2) font-style – styles the font Values: normal, italic, oblique text-decoration – decorates the text Values: none, underline, line-trough, overline, blink text-align – defines the alignment of text or other content Values: left, right, center, justify 36
  • 41. Short Font Rules font Shorthand rule for setting multiple font properties at the same time is equal to writing this: 37 font: italic normal bold 12px Verdana; font-style: italic; font-variant: normal; font-weight: bold; font-size:12px; font-family: Verdana;
  • 42. Fonts Live Demo font-rules.html
  • 43. Background CSS Rules background-color Solid color background background-image URL of image to be used as background, e.g.: background-repeat repeat-x, repeat-y, repeat, no-repeat background-attachment fixed / scroll 39 background-image:url("back.gif");
  • 44. Background CSS Rules (2) background-position: specifies vertical and horizontal position of the background image Vertical position: top, center, bottom Horizontal position: left, center, right Both can be specified in percentage or other numerical values Examples: 40 background-position: top left; background-position: -5px 50%;
  • 45. Background Short Rule background: shorthand rule for setting background properties at the same time: is equal to writing: Some browsers will not apply BOTH color and image for background if using shorthand rule 41 background: #FFF0C0 url("back.gif") no-repeat fixed top; background-color: #FFF0C0; background-image: url("back.gif"); background-repeat: no-repeat; background-attachment: fixed; background-position: top;
  • 46. Background-image or <img>? Background images allow you to save many image tags from the HTML Leads to less code More content-oriented approach All images that are not part of the page content should be moved to the CSS 42
  • 47. Background Styles Live Demo background-rules.html
  • 48. Border CSS Rules border-width: thin, medium, thick or numerical value (e.g. 10px) border-color: color alias or RGB value border-style: none, hidden, dotted, dashed, solid, double, groove, ridge, inset, outset Each property can be defined separately for left, top, bottom and right border-top-style, border-left-color, … 44
  • 49. Border Short Rules border: shorthand rule for setting border properties at once: is equal to writing: Specify different borders for the sides via shorthand rules: border-top, border-left, border-right, border-bottom 45 border: 1px solid red; border-width:1px; border-color:red; border-style:solid;
  • 50. Borders Live Demo border-rules.html
  • 51. Width, Height CSS Rules width – defines numerical value for the width of element, e.g. 200px height – defines numerical value for the height of element, e.g. 100px Important: not all elements and browsers follow this value! Usually the height of an element is defined by its content Common mistake is to apply height to tables or table cells 47
  • 52. Width / Height Live Demo size-rules.html
  • 53. Margin and Padding margin and padding define the spacing around the element Numerical value, e.g. 10px or -5px Can be defined for each of the four sides separately margin-top, padding-left, … margin is the spacing outside of the border padding is the spacing between the border and the content 49
  • 54. Margin and Padding: Short Rules margin: 5px; Sets all four sides to have margin of 5 px; margin: 10px 20px; Sets margins: top and bottom to 10px, left and right to 20px; margin: 1px 3px 5px 7px; Sets top, right, bottom, left margins to 1px, 3px, 5px and 7px respectively Same shorthand rules apply for padding 50
  • 56. Gecko and W3C vs. IE Major difference between browsers when applying border, padding and width/height To avoid you need either “CSS hacks” or just don’t specify for the same element width/height and padding or border, different than 0 52
  • 57. Margins and Paddings Live Demo margins-paddings-rules.html
  • 58. CSS Rules for Positioning position: defines the positioning of the element, depending on the parent elements The value is one of: static (default) relative – relative position according to where the element would appear with static position absolute – position according the parent element fixed – fix element on screen, ignore page scrolling 54
  • 59. CSS Rules for Positioning (2) Fixed and absolute positioned elements “float” over the rest of elements Moved to separate document layer Their position and size is ignored when calculating the size of parent element or position of surrounding elements Ordered by their z-index 55
  • 60. Positioning Live Demo positioning-rules.html
  • 61. CSS Rules for Positioning top, left, bottom, right: specifies offset of absolute/fixed/relative positioned element as numerical values vertical-align: sets the vertical-alignment of an element Usually used for table cells Values: baseline, sub, super, top, text-top, middle, bottom, text-bottom or numeric z-index: specifies the depth-order of element 57
  • 62. Alignment and Z-Index Live Demo alignments-and-z-index-rules.html
  • 63. Float CSS Rule float: the element “floats” above the elements similar to position:absolute and the align HTML property of <img> tag element is taken into account when rendering the surrounding text and elements left: places the element on the left right: places the element on the right 59
  • 64. Clear CSS Rule clear Sets the sides of the element where other floating elements are NOT allowed Possible values: left, right, both This rule can be applied only to “floating” elements 60
  • 65. Floating Elements Live Demo float-rules.html
  • 66. Opacity CSS Rule opacity: specifies the opacity of the element Floating point number from 0 to 1 Supported only by Safari browser For Mozilla use –moz-opacity CSS rule For IE use filter:alpha(opacity=value) where value is from 0 to 100 Need to apply all three rules 62
  • 67. Opacity Live Demo opacity-rule.html
  • 68. Visibility CSS Rule visibility Determines whether the element is visible hidden: element is not rendered, but still takes place in the rendering (acts like opacity:0) visible: element is rendered normally 64
  • 69. Visibility Live Demo visibility-rule.html
  • 70. Display CSS Rule display: controls the display of the element and the way it is rendered and if breaks should be placed before and after the element inline: no breaks are placed before and after (<span> is inline element) block: breaks are placed before AND after the element (<div> is block element) 66
  • 71. Display CSS Rule (2) none: element is hidden and its dimensions are not used to calculate the surrounding elements rendering (differs from visibility:hidden!) There are some more possible values, but not all browsers support them Specific displays like table-cell and table-row 67
  • 72. Display Live Demo display-rule.html
  • 73. Overflow CSS Rule overflow: defines the behavior of element when content needs more space than you have specified by the size properties or for other reasons. Values: visible (default) – element size is increased to make space for content or the content “overflows” out of the element scroll – show horizontal/vertical scroll bar in the element hidden – any content in the element that cannot be placed inside is hidden 69
  • 74. Overflow Live Demo overflow-rule.html
  • 75. Other CSS Rules cursor: specifies the look of the mouse cursor when placed over the element Values: crosshair, help, pointer, progress, move, hair, col-resize, row-resize, text, wait, copy, drop, and others white-space – controls the line breaking of text. Value is one of: nowrap – keeps the text on one line normal (default) – browser decides whether to brake the lines if needed 71
  • 76. Benefits of using CSS More powerful formatting than using presentation tags Your pages load faster, because browsers cache the .css files Increased accessibility, because rules can be defined according given media Pages are easier to maintain and update 72
  • 77. Maintenance Example 73 Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. one CSS file Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css. Title Some random text here. You can’t read it anyway! Harharhar! Use Css.
  • 78. What to Avoid Don’t use <font> or other presentation tags to change the look of your text <font color=...> <b>text that is bold <center> this text is centered Do not use complex CSS rules-sets because may slow down page rendering Move as much as possible to external files Avoid inline CSS 74
  • 79. CSS Development Tools Visual Studio – CSS Editor 75
  • 80. CSS Development Tools (2) Firebug – add-on to Firefox used to examine and adjust CSS and HTML 76
  • 81. CSS Development Tools (3) IE Developer Toolbar – add-on to IE used to examine CSS and HTML (press [F12]) 77
  • 82. CSS ? ? ? ? ? Questions? ? ? ? ? ? http://schoolacademy.telerik.com