1.25k likes | 1.44k Views
CSS Overview. CSS Quick Facts. CSS – Cascading Style Sheets Can be used in HTML, SVG, and any XML document . Is a style sheet language used for presentation. Intro. Three ways to get CSS into HTML. From easiest to best practice Inline Embedded External. Intro. Inline.
E N D
CSS Quick Facts • CSS – Cascading Style Sheets • Can be used in HTML, SVG, and any XML document. • Is a style sheet language used for presentation Intro
Three ways to get CSS into HTML From easiest to best practice • Inline • Embedded • External Intro
Inline Using the style attribute <p style=“color: red; font-family: arial;”> This is awesome content. </p> Intro
Embedded Placed inside of the <head> element <head> <style type=“text/css”> p { color: red; font-family: arial; } h1 { margin: 0em; padding: 0em; } </style> <head> Intro
External index.html <head> <link rel=“stylesheet” type=“text/css” href=“style.css”/> </head> style.css p { color: red; font-family: arial; } h1 { margin: 0em; padding: 0em; } Intro
Inline CSS is Bad! Avoid this because we want to • Separate presentation from structure • Make maintenance easier • Promote re-usability • Make our HTML more accessible Intro
Disadvantages of Embedded CSS • They affect only the pages they’re on • Increase page load times Intro
Why use external CSS? • See previous two slides Intro CSE 3345
External CSS Tip Don’t use @import for external css Not as network friendly and will not be cached by browser. **if you don’t know what @import is, don’t worry** Intro CSE 3345
CSS Selectors CSE 3345
How does CSS work? Three key features • Selector • Properties • Values Property Value Declaration Selector { color: red; font-family: arial; } p Intro CSE 3345
Selectors 5 ways to select • element - body • class - .page • id - #navigation • position in document - .page p, p:first-child, div > a, li + li • State – a:visited, input:focus Selectors CSE 3345
A Subtle Distinction Class selector Matches an element that has a class value which contains the attribute value. Id selector Matches an element that has a class value which is equal Selectors CSE 3345
Element Selector • To select an element, create a selector using the element’s name. <p>This text will be red</p> p { color: red} CSE 3345
Style all h2 elements <body> <h1>History</h1> <section> <article> <h2>Ancient</h2> <p>Knights and horses use to be best friends.</p> </article> <article> <h2>Colonial</h2> <p>In 1300, men walked around town holding a gun.</p> </article> </section> </body> CSE 3345
Class Selector • To select an element by class value, create a selector with the class value you want to select prepended with a period. <div class=“content”>Hello blue text!</div> .content { color: blue; } CSE 3345
Id Selector • To select an element by id, create a selector with the id value you want to select prepended with a pound/hash sign. <div id=“nav”>Hello green text!</div> #nav { color: green; } CSE 3345
Pseudo-Class Selector • Create a pseudo-class selector by appending a colon after an element, class, or id selector followed by the pseudo-class. CSE 3345
Pseudo-Class Selector <p> The last P before the note. </p> <div class="note"> <p id=“fred”>The selector applies to me, I’m purple!</p> <p>The selector doesn’t apply to me</p> </div> div p:first-child { color: purple; } div p#fred:first-child { color: purple; } CSE 3345
Specialized Selectors Selectors CSE 3345
30 selectors you should memorize • http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ Selectors CSE 3345
More Info • See the CSS spec for thorough details on selectors http://www.w3.org/TR/CSS2/selector.html Selectors CSE 3345
What will be selected? #navul.links a:hover <body> <div id=“nav”> <ul class=“links”> <li><a href=“#”>Selectors Rule</a></li> </ul> </div> </body> A possible HTML snippet Selectors CSE 3345
Practice Selectors Visit Selectoracle to learn about and practice with CSS selectors. Selectors CSE 3345
CSS Specificity/Point System CSE 3345
CSS Specificity/Point System <pstyle="color:red;">This is red</p> CSE 3345
CSS Specificity/Point System #nav{ color: red;} CSE 3345
CSS Specificity/Point System .warning{ color: yellow;} p:first-child{ padding: 10em;} CSE 3345
CSS Specificity/Point System a { margin-top: 5em;} CSE 3345
CSS Specificity/Point System • If the element has inline styling, that automatically wins 1,0,0,0 points • For each ID value, apply 0,1,0,0 points • For each class value (or pseudo-class or attribute selector), apply 0,0,1,0 points • For each element reference, apply 0,0,0,1 point CSE 3345
Points System (Bad Way!!!) • Is NOT base 10 system • (1, 1, 1, 1) -> (1 * (10^3)) + (1 * (10^2)) + (1 * (10^1)) + (1 * (10^0)) --> (1 * (1000)) + (1 * (100)) + (1 * (10)) + (1 * (1)) ---> (1,000)) + (100) + (10) + (1) ----> 1,111 -----> WRONG!!! CSE 3345
Points System • Add the number of selectors for each category and place them in the appropriate column. • Numbers don’t fill over • (0, 1, 13, 1) CSE 3345
How to determine a winner • Score your selectors in the order they are written • Starting from the top, go down the first column looking for the highest score. If you don’t find a winner or two columns are equal, move to the next column (repeat if necessary). • If you find two selectors with equal specificity, the selector listed last wins. CSE 3345
How to determine a winner (0, 1, 1, 10) (0, 0, 13, 5) (0, 0, 7, 3) (0, 0, 2, 1) (0, 0, 1, 0) (0, 1, 2, 2) CSE 3345
How to determine a winner (0, 1, 1, 10) (0, 0, 13, 5) (0, 0, 7, 3) (0, 0, 2, 1) (0, 0, 1, 0) (0, 1, 2, 2) – winner! CSE 3345
Lets Practice • div #nav li • ol.ing li • ol.ing:hover li CSE 3345
Point System Reminder! • 13 elements don’t override a class • 13 classes don’t override an id • 13 ids don’t override an inline style CSE 3345
Inheritance & Units CSE 3345
Inheritance • Some styles are inherited by the child from their parent • Font-family • Text-decoration • Text related styles Inheritance CSE 3345
Inheritance: ones on left will, ones on right will not Text related Layout related Inheritance CSE 3345
Inheritance You can force a child to inherit properties from its parent <div style=“border: 1px solid red;”> <p style=“border: inherit”> I like to inherit stuff. </p> </div> Inheritance CSE 3345
Units • Absolute: always compute the same value • Points (pt) • Inches (in) • Relative: computed with respect to something • Pixels (px) • Em • % Units CSE 3345
Absolute Units • Don’t use them unless your media type is print • DON’T USE THEM for building web pages! Units CSE 3345
Relative Units : Pixels • Pixels are relative because they are based on the pixel size of the display screen. • There are a lot of different pixel sizes so almost every screen will be different. Units CSE 3345
Relative Units : Em For fonts and containers • 1em is equal to the current font size of the element in question. Units CSE 3345
Relative Units : % Percent For fonts • 100% is equal to the current font size of the element in question For containers • 100% is equal to the size of the parent container Units CSE 3345
CSS Font Size Fiddle • http://jsfiddle.net/znpKn/ CSE 3345
Box Model CSE 3345
CSS Moment of Clarity • Every page element is a box. • Developers can control the size and position of these boxes. Box Model CSE 3345