Efficient: Maintainable, Modular
Efficient: Maintainable, Modular
maintainable, modular
Efficient
Not good::
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text <title>Page title</title> </head> <body> <p style="color: red"> ... </p> </body> </html>
Not good::
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text <title>Page title</title> <style type="text/css" media="screen"> p { color: red; } </style> </head> <body> ... </body> </html>
Good::
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text <title>Page title</title> <link rel="stylesheet" href="name.css" type="text/css" media="screen" /> < /head> <body> ... </body> </html>
Not good::
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text <title>Page title</title> <style type="text/css" media="screen"> @import url("styles.css");</style> </style> </head> <body> ... </body> </html>
Good::
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text <title>Page title</title> <link rel="stylesheet" href="name.css" type="text/css" media="screen" /> </head> <body> ... </body> </html>
Use inheritance
Inefficient::
p { font-family: arial, helvetica, sans-serif; } #container { font-family: arial, helvetica, sans-serif; } #navigation { font-family: arial, helvetica, sans-serif; } #content { font-family: arial, helvetica, sans-serif; } #sidebar { font-family: arial, helvetica, sans-serif; } h1 { font-family: georgia, times, serif; }
Efficient::
Inefficient::
h1 h2 h3 h4
{ { { {
} } } }
Efficient::
Inefficient::
Efficient::
Inefficient::
body { font-size: 85%; font-family: arial, helvetica, sans-serif; background-image: url(image.gif); background-repeat: no-repeat; background-position: 0 100%; margin-top: 1em; margin-right: 1em; margin-bottom: 0; margin-left: 1em; padding-top: 10px; padding-right: 10px; padding-bottom: 10px; padding-left: 10px; border-style: solid; border-width: 1px; border-color: red; color: #222222;
Efficient::
body { font: 85% arial, helvetica, sans-serif; background: url(image.gif) no-repeat 0 100%; margin: 1em 1em 0; padding: 10px; border: 1px solid red; color: #222; }
Avoid !important
Desperate::
Understanding specificity::
So, how can you make your CSS easier to understand for people who maintain your website?
/* --------------------------------Site: Site name Author: Name Updated: Date and time Updated by: Name --------------------------------*/
/* --------------------------------COLORS Body background: #def455 Container background: #fff Main Text: #333 Links: #00600f Visited links: #098761 Hover links: #aaf433 H1, H2, H3: #960 H4, H5, H6: #000 --------------------------------*/
Not good::
Better::
{ ... } h1 { ... } h1 img { ... } form { ... } a#skip { ... } { ... } ul { ... } ul li { ... } ul li a { ... } ul li a:hover { ... }
#navigation #navigation #navigation #navigation #navigation #content #content #content #content #content
/* --------------------------------header styles --------------------------------*/ #header #header #header #header { ... } h1 { ... } h1 img { ... } form { ... }
OK, how can you manage your CSS across an entire site?
Modular CSS
header.css
content.css
Why separate CSS files? Its easier to find rules. You can also serve specific CSS files to pages.
HTML files
Why add a bridging file? You can add or remove files as needed without changing the HTML.
HTML files
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text <title>Page title</title> <link rel="stylesheet" href="bridging.css" type="text/css media="screen, projection"> </head> <body> ... </body> </html>
Why double media type? NN4 will not see bridging file and will therefore not crash!
How do @imports work? This will imports all rules from one file into the other. @import cannot be read by older browsers.
Recap?
HTML files
header
home
header
Section 1
header
Section 2
Hack-free CSS
One of our biggest challenges is dealing with buggy browsers such as IE.
The problem is that IE has improved its CSS support. Hacks may come back to bite!
We ask that you please update your pages to not use these CSS hacks. If you want to target IE or bypass IE, you can use conditional comments.
header
home
IE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="en"> <head> <meta http-equiv="content-type" content="text <title>Page title</title> <link href="css/import1.css" rel="stylesheet" <!--[if IE 5]><link rel="stylesheet" href="ie5.css" type="text/css" media="screen"><![endif]--> </head> <body> ... </body> </html>
Only the relevant version of IE will sees this new style sheet. All other browsers will ignore it completely.
header
home
IE
header
home
IE
header
home
IE
For example, while most browsers add padding to the width of a container, IE5 will not. This these cases, IE5 will display a narrower container.
1. No hacks Specific CSS rules are simply rewritten in new style sheet.
2. Separate file Work-around CSS kept out of main CSS and can be turned off easily at a later date.
Questions?