css architecture

27
CSS ARCHITECTURE Standards and Best Practices

Upload: pelletized

Post on 06-May-2015

6.094 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Css Architecture

CSS ARCHITECTURE

Standards and Best Practices

Page 2: Css Architecture

“CSS starts out being a pain for everyone. Don't worry. You're not stupid, CSS is.” - John Manoogian

Page 3: Css Architecture

Reasons for this

Common issues throughout the sites Knowledge sharing Advice on overcoming problems

Page 4: Css Architecture

Multiple CSS files create confusionPeterson’s homepage

Page 5: Css Architecture

Combine CSS files

One master CSS file for common elements

One project level CSS file for project specific styles

One CSS file for print specific

Other CSS filesOne CSS file for each cobrand One CSS file for IE specific styles

Page 6: Css Architecture

Inline Styles and Internal Stylesheets<ul> <li> <img src="/images/landing/yahoo_hotjobs.jpg" style="width: 150px; height: 25px; border: 0px;" alt="Yahoo Hotjobs" /> </li> <li> <img src="/images/landing/wsj_logo.jpg" style="width: 150px; height: 25px; border: 0px;" alt="Wall Street Journal" /> </li> <li> <img src="/images/landing/air_force_logo.jpg" style="width: 150px; height: 25px; border: 0px;" alt="U.S. Air Force" /> </li> </ul>

Page 7: Css Architecture

Inline Styles and Internal StylesheetsHTML<ul> <li> <img src="/images/landing/yahoo_hotjobs.jpg“ class=“logo” alt="Yahoo Hotjobs" /> </li> <li> <img src="/images/landing/wsj_logo.jpg" class=“logo” alt="Wall Street Journal" /> </li> <li> <img src="/images/landing/air_force_logo.jpg" class=“logo” alt="U.S. Air Force" /> </li> </ul>

CSSimg {border:0;}.logo {width:150px;Height:25px;}

Page 8: Css Architecture

HTML Structure<div id="header"> this is the header</div><div id=”navbar”> main navigation goes here</div><div id=”menu”> list of items</div><div id="main-content"> the main content will go here</div><div id="sidebar"> this is the sidebar</div><div id="footer"> this is the footer</div>

Page 9: Css Architecture

Naming Conventions

Be consistent name your elements what they are, not

what they do (semantic vs. presentational)

GOOD:.featured-link

BAD:.blue-link-left

Page 10: Css Architecture

Hacks* html .class {width:150px;}

Page 11: Css Architecture

Hacks* html .class {width:150px;}

THIS DOESN’T WORK!

Page 12: Css Architecture

Hacks<!--[if lte IE 7]> <style type="text/css"> .class {width:150px;} </style><![endif]-->

<!--[if IE]> <link rel="stylesheet" href=“ie.css" type="text/css“ /><![endif]-->

Page 13: Css Architecture

Hacks

Can I code it in a way that doesn’t require any specific targeting in the first place?

Can I instead use a conditional comment?

Page 14: Css Architecture

Tables aren’t the enemy There are many places where using tables is entirely appropriate

Page 15: Css Architecture

Tables aren’t the enemy

Page 16: Css Architecture

Tables have their place, but not for layout

Page 17: Css Architecture

Main Navigation HTML (yes, that’s all there is to it!)

<!-- main nav --> <ul id="mainnav"> <li><a id="home" href="/" class="mainnavCurrent">Home</a></li> <li><a id="prodserv" href="/ps/products-services-home.aspx">Products &amp; Services</a></li> <li><a id="partnerships" href="/partnerships/partnerships-home.aspx">Partnerships</a></li> <li><a id="company" href="/company/about.aspx">Our Company</a></li> <li><a id="contact" href="/contact.aspx">Contact Us</a></li> </ul> <!-- /main nav -->

Page 18: Css Architecture

Main Navigation CSSThe heavy lifting

Page 19: Css Architecture

Selectors

You don’t need to add a class to every elementBadHTML<div id=“main”> <p class=“paragraph”>Content</p> <p class=“paragraph”>Content 2</p></div>

CSS.paragraph { color:#F00;}

GoodHTML<div id=“main”> <p>Content</p> <p>Content 2</p></div>

CSS#main p { color:#F00;}

Page 20: Css Architecture

SelectorsHTML<ul> <li> <img src="/images/landing/yahoo_hotjobs.jpg“ class=“logo” alt="Yahoo Hotjobs" /> </li> <li> <img src="/images/landing/wsj_logo.jpg" class=“logo” alt="Wall Street Journal" /> </li> <li> <img src="/images/landing/air_force_logo.jpg" class=“logo” alt="U.S. Air Force" /> </li> </ul>

CSSimg {border:0;}.logo {width:150px;Height:25px;}

Page 21: Css Architecture

SelectorsHTML<ul class=“logo”> <li> <img src="/images/landing/yahoo_hotjobs.jpg“ alt="Yahoo Hotjobs" /> </li> <li> <img src="/images/landing/wsj_logo.jpg” alt="Wall Street Journal" /> </li> <li> <img src="/images/landing/air_force_logo.jpg” alt="U.S. Air Force" /> </li> </ul>

CSSimg {border:0;}

.logo li img {width:150px;Height:25px;}

Page 22: Css Architecture

Don’t Repeat Yourself

Combine styles where possible<h1>Head</h1><h2>sub head</h2>

CSSh1 {font-size:18px;color:blue;}

h2 {font-size:18px;color:blue;}

<h1>Head</h1><h2>sub head</h2>

CSSh1, h2 {font-size:18px;color:blue;}

Page 23: Css Architecture

Don’t Repeat Yourself

Use shorthand wherever possible.class {margin-top:10px;margin-right:0;margin-bottom:15px;margin-left:5px;color:#990000;}

.class-other {font-weight: bold;font-style: italic;font-variant: small-caps;font-size: 12px;line-height: 14px;font-family: verdana,sans-serif;}

.class {margin: 10px 0 15px 5px;color:#900;}

.class-other {font: bold italic small-caps 12px/14px verdana,sans-serif;}

Page 24: Css Architecture

Commenting

Color/Layout glossary Flag different sections

Page 25: Css Architecture

Validation

Not just for HTML, but for CSS too Ensures your code is error free and

interpreted in the right way for browsers Search engines

Page 26: Css Architecture

Floats

Wrapping content around an element

Horizontally, not vertically

To stop content from wrapping, you must clear the floatCSS.clear {clear:both;}

HTML<div class=“clear”></div>

Page 27: Css Architecture

DON’T PANIC