CSS Best Practices By Peter Funk 1. Web development since 1996 Senior Front-end web developer at Ancestry.com Proficient at CSS, HTML, and native JavaScript.

Slides:



Advertisements
Similar presentations
Cascading Style Sheets
Advertisements

Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 6 Key Concepts 1 Copyright © Terry Felke-Morris.
Copyright © Terry Felke-Morris WEB DEVELOPMENT & DESIGN FOUNDATIONS WITH HTML5 Chapter 6 Key Concepts 1 Copyright © Terry Felke-Morris.
Advance CSS (Menu and Layout) Miftahul Huda. CSS Navigation MENU It's truly remarkable what can be achieved through CSS, especially with navigation menus.
Class four: the box model and positioning. is an HTML tag which allows you to create an element out of inline text. It doesn’t mean anything! try it:
Tutorial 4: Creating page layout with css
Stylin’ with CSS. 2 Topics What is CSS? Why CSS? CSS Examples.
CS4370/6370 Web Development Cascading Style Sheets (CSS)
Basics of Web Design Chapter 6 More CSS Basics Key Concepts 1.
Web Design & Development Cascading Style Sheets (CSS)
Beginning Web Site Creation: Dreamweaver CS4. XHTMLCSS  Describes the structure  Content  Collection of styles  Formatting body { background-color:
Cascading Style Sheet (CSS) Instructor: Dr. Fang (Daisy) Tang
4.01 Cascading Style Sheets
CSS (Cascading Style Sheets): How the web is styled Create Rules that specify how the content of an HTML Element should appear. CSS controls how your web.
Cascading Style Sheets CS3505. What are CSS? Method for adding style attributes consistently to HML tags Cascading because styles are applied in order.
Cascading Style Sheets Billy Toy Cascading Style Sheets Syntax review How to Implement style sheets Background properties Text properties.
CSS Dvijesh Bhatt.
HTML - Quiz #2 Attendance CODE:
LING 408/508: Programming for Linguists
Cascading style sheets (CSS)
Tutorial #3 Cascading Style Sheets. Review: Last Class Image sizing Pathnames Project Default Path Relative Path Absolute Path Blackboard Homework Submission.
Web Development & Design Foundations with XHTML
Doman’s Sections Information in this presentation includes text and images from
MCS 270 Cascading Style Sheets (CSS) Gustavus Adolphus College.
CSS: Cascading Style Sheets. 2 What are Style Sheets A style sheet is a mechanism that allows to specify how HTML (/XHTML/XML) pages should look The style.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
FTS: The Perils and Promise of Computing Cascading Style Sheets (CSS) Gustavus Adolphus College Fall 2012.
Class three: CSS review, backgrounds, font formatting, the box model.
Tutorial #3 Cascading Style Sheets. Tutorial #2 Review - Anchors Links to Site DMACC Internal Links Go to Top Mail To me Local.
Cascading Style Sheet Bayu Priyambadha, S.Kom. Definition Cascading Style Sheets (CSS) form the presentation layer of the user interface. –Structure (HTML)
 Word doc for you to download › Link at the beginning of class › 10 Questions › Answers to be added inline  Post to Sakai when completed › No resubmits.
Introduction to CSS. What is CSS?  Cascading Style Sheets  Used for styling HTML  Also important in javascript and jquery for selectors  External.
CHAPTER 3 CSS & CSS3 อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา 1.
MySQL and PHP Review CSS. Cascading Style Sheet (CSS) Style sheets are files or forms that describe the layout and appearance of a document. Cascading.
 Remember that HTML is just text  Need to point to pictures  Use the img tag  alt: › screen reader › REQUIRED for this class and to validate.
COMP213 – Web Interface Design
Ch 10 HTML and CSS Web Standards Solutions A Web Standardistas’ Approach.
Lesson 03 // Cascading Style Sheets. CSS Stands for Cascading Style Sheets. We’ll be using a combination of Html and CSS to create websites. CSS is a.
CSS WORKSHOP Design Principles for Web Standards.
LING 408/508: Programming for Linguists Lecture 10 September 30 th.
REEM ALMOTIRI Information Technology Department Majmaah University.
Microsoft Expression Web-Illustrated Unit F: Enhancing a Design with CSS.
Copyright © Osmosys O S M O S Y SO S M O S Y S D e p l o y i n g E x p e r i e n c e & E x p e r t i s e™ CSS Training.
Lecture 2: Cascading Style Sheets (CSS) Instructor: Dr. M. Anwar Hossain.
CSS Hadas Kahsay. Overview  What is CSS  Basic syntax of CSS Rules  How to link CSS style to html documents  Browsers and CSS  Advantages of CSS.
 HTML, CSS, JavaScript – enormously successful  SASS has all the tools we need! INTRO.
Web Technologies Beginning Cascading Style Sheets (CSS) 1Copyright © Texas Education Agency, All rights reserved.
Web Development & Design Foundations with XHTML Chapter 6 Key Concepts.
CONTROLLING Page layout
Department of Computer Science, Florida State University CGS 3066: Web Programming and Design Spring
CSS: Cascading Style Sheets Part II. Style Syntax.
CIS67 Foundations for Creating Web Pages Professor Al Fichera Rev. September 22, 2010—All HTML code brought to XHTML standards. Reference for CIS127 and.
OV Copyright © 2008 Element K Content LLC. All rights reserved. Working with Web Pages  An Introduction to Cascading Style Sheets  Format a Web.
Review First – on your own... Go to Day 5 Grab the contents of position review file Place in DW Add styles to recreate – get something close to
Cascading Style Sheet (CSS) SAMPLE IT133 Pengembangan Web.
Understanding CSS Cascading Style Sheets control the presentation of content in HTML pages Style Sheets separate formatting from the content –Styles defined.
Font-Family, Color and Background. Basics  CSS rule: Selector (multiple separated by, ) Declaration (multiple separated by ; ) Example: p { color: red;
PERSPECTIVES: TUTORIAL 4 CREATING PAGE LAYOUTS WITH CSS.
Chapter 7.  Change body and link styles  Create a drop-down menu  Change color and font styles in the menu  Create a pop-up effect using CSS  Utilize.
CS 120: Introduction to Web Programming Lecture 10: Extra Features and Website Design Part 1 Tarik Booker CS 120 California State University, Los Angeles.
Blended HTML and CSS Fundamentals 3 rd EDITION Tutorial 3 Introducing Cascading Style Sheets.
1 CS428 Web Engineering Lecture 07 Font, Text & Background (CSS - II)
HTML & CSS Contents: the different ways we can use to apply CSS to The HTML documents CSS Colors and Background CSS Fonts CSS Text CSS box model: padding,
Unit 3 - Review. Topics 1. Tag, Attribute, Value 2. CSS Rule Syntax + link 3. Categories of Selectors 4. Inline vs Block Tags 5. CSS Layout 6. CSS Box.
CSS Introductions. Objectives To take control of the appearance of a Web site by creating style sheets. To use a style sheet to give all the pages of.
CONFIGURING COLOR AND TEXT WITH CSS Chapter 3. Cascading Style Sheets (CSS) Used to configure text, color, and page layout. Launched in 1996 Developed.
CSS.
4.01 Cascading Style Sheets
CSS Borders and Margins.
4.01 Cascading Style Sheets
Presentation transcript:

CSS Best Practices By Peter Funk 1

Web development since 1996 Senior Front-end web developer at Ancestry.com Proficient at CSS, HTML, and native JavaScript Developed and maintain CSS3.me 2

3

Current CSS Files are: Disorganized Unreadable Large in size Contain invalid code Virtually unmaintainable 4 "Any developer who claims he never writes bad code is either lying, ignorant or living in a fantasy world." - Davy Brion

Organization / Readability Naming / Declaration Shorthand Avoidances Tips / Tricks 5

Organization / Readability Organize styles DOM Order Grouped Order /* Header */.header { property:value; }.header.menu { property:value; } /* Content */.content { property:value; }.content.widget { property:value; } /* Footer */.footer { property:value; }.footer.links { property:value; } /* Containers */.header { property:value; }.content { property:value; }.footer { property:value; } /* Navigation */.header.menu { property:value; }.footer.links { property:value; } /* Widgets */.content.widget { property:value; } 6

Organization / Readability Organize properties.button { width: 227px; height: 35px; line-height: 35px; background-color: #3A78AC; border: 1px solid #333; border-radius: 18px; text-decoration: none; box-shadow: 0 2px 2px rgba(0, 0, 0,.3); color: #fff; text-shadow: 0 -1px rgba(0, 0, 0,.5); top: 9px; left: 9px; display: block; position: absolute; font-size: 15px; font-weight: 700; line-height: 15px; text-transform: uppercase; } 7

Organization / Readability Organize properties Alphabetical order.button { background-color: #3A78AC; border: 1px solid #333; border-radius: 18px; box-shadow: 0 2px 2px rgba(0, 0, 0,.3); color: #fff; display: block; font-size: 15px; font-weight: 700; height: 35px; left: 9px; line-height: 35px; position: absolute; text-align: center; text-decoration: none; text-shadow: 0 -1px rgba(0, 0, 0,.5); text-transform: uppercase; top: 9px; width: 227px; } 8

Organization / Readability Order vender properties Alphabetical order.widgetHeaderBackground { background-color: #3A78AC; background-image: -moz-linear-gradient(top, #62A0D4, #4785B9 55%, #2D6B9F 55%, #19578A); background-image: -ms-linear-gradient(top, #62A0D4, #4785B9 55%, #2D6B9F 55%, #19578A); background-image: -o-linear-gradient(top, #62A0D4, #4785B9 55%, #2D6B9F 55%, #19578A); background-image: -webkit-linear-gradient(top, #62A0D4, #4785B9 55%, #2D6B9F 55%, #19578A); background-image: linear-gradient(top, #62A0D4, #4785B9 55%, #2D6B9F 55%, #19578A); filter: progid: DXImageTransform.Microsoft.gradient(startColorstr = '#62A0D4', endColorstr = '#19578A'); -ms-filter: "progid: DXImageTransform.Microsoft.gradient(startColorstr = '#62A0D4', endColorstr = '#19578A')"; } 9

Organization / Readability Make styles readable Single-line styles.content { float:left; padding:10px; width:590px; }.widget { color:red; height:40px; margin-top:30px; } 10 Multi-line styles.content { float: left; padding: 10px; width: 590px; }.widget { color: red; height: 40px; margin-top: 30px; }

Organization / Readability Use whitespace Single-line styles.content█{█float:left;█padding:10px;█width:590px;█}.widget█{█color:red;█height:40px;█margin-top:30px;█} 11 Multi-line styles.content█{ float:█left; padding:█10px; width:█590px; }.widget█{ color:█red; height:█40px; margin-top:█30px; }

Organization / Readability Organize styles Organize properties Order vender properties Make styles readable Use whitespace 12

Naming / Declaration Use semantic naming BAD:.sB {…}.button3 {…}.topLeftButton {…}.greenButton {…} GOOD:.searchButton {…} 13

Naming / Declaration Use a naming convention BAD:.sEaRcHbUtToN {…}.searchbutton {…} GOOD:.searchButton {…}.search-button {…}.search_button {…} 14 Camel Casing, Hyphens, or Underscores

Naming / Declaration Use necessary selectors BAD: form.form {…} div.first, ul.first, li.first {…} div.contentDiv {…}.firstItemStyle_and_titleWithImageStyle {…} form#searchForm.formClass {…} html body div.myWidget form.myForm input#myInput {…}.theOnlyElementToEverUseThisClass {…} #sideBarLink, #sideBarLink2, #sideBarLink3 {…} 15

Naming / Declaration Use a wrapping selector for components #myWidget.header {…} #myWidget.header p {…} #myWidget.content {…} #myWidget.content p {…} #myWidget form {…} #myWidget input {…} #myWidget.searchButton {…} #myWidget.content a {…} #myWidget.footer {…} #myWidget.footer a {…} 16

Naming / Declaration Use semantic naming Use a naming convention Use necessary selectors Use a wrapping selector for components 17

Shorthand Use shorthand notation when available margin: 1px 1px 1px 1px;=margin: 1px; margin: 1px 2px 1px 2px;=margin: 1px 2px; margin: 1px 2px 3px 2px;=margin: 1px 2px 3px; 18 Background Border Font with Line-Height List Margin Outline Padding CSS3 properties (most)

Shorthand Use shorthand if all properties declared font-family: Arial, Helvetica, serif; font-size: 13px; font-weight: 700; line-height: 120%; = font:700 13px/120% Arial, Helvetica, serif; 19 BAD: background: url(someImg.jpg); color: #fff; GOOD: background: #000 url(someImg.jpg); color: #fff; Know property defaults

Shorthand Remove units on zero values padding: 0px; = padding: 0em; = padding: 0; 20 box-shadow: 05px 8.0px = box-shadow: 5px 8px; Remove leading/trailing zeros

Shorthand Use when available Use if all properties declared Know property defaults Remove units on zero values Remove leading/trailing zeros 21

Avoidances Avoid the use of !important Avoid browser hacks Avoid the * selector Avoid too many selectors Avoid inline styles Avoid multiple element selectors 22

Avoidances Avoid inappropriate properties Avoid empty rules Avoid duplicate properties Avoid too many web fonts Avoid complex attribute selectors 23

Tips Use comments Know the box model CSS3 only for presentational purposes Understand Specificity Use a CSS formatting tool Use a CSS compressor 24

Tips Show upgrade links for old browsers Declare background images once Learn about all CSS properties and values Know how to use z-index Use word-wrap: break-word Use text-overflow: ellipsis 25

Organize / Make Readable Name / Declare Well Use Shorthand Avoid bad code Know / Use properties 26

CSS3.me 27