|
CSS (Cascading Style Sheets)
| Topics this page:
|
CSSPlay lists experiments with CSS |
Transcending CSS: The Fine Art of Web Design (Voices That Matter) (New Riders, Nov 15, 2006) by Andy Clarke and Molly E. Holzschlag has a lot of "wasted" space, extraneous photos, and chatty text. But all that makes sense because in this book CSS is presented in the context of excellent web design. Like a conversation with an eccentric artist, there are enough gems of wisdom among the cryptic text and images that makes the exchange interesting, such as its comments on CSS3 support by Firebox 2 and IE7.
One would think that a web page about CSS would be better looking. This is like 1998.
Yeah, yeah. I agree with you. This WAS put together in 1998. This at the moment is an example of what NOT to do.
Then again, this is used as a playground. So this will get better ... eventually.
I sure hope so, for your sake ;)
The list in the above is coded without using extra mark-up to define spaces.
#listsep { list-style-type: none;} #listsep li { background-image: url(checkbox.gif); background-repeat: no-repeat; padding-left: 18px; background-position: 0px 2px; }
In priority of usefulness:
The Gallery List is a site listing 49 gallery sites, offering to submit your site to them for $1 each.
Most expert CSS coders make use of a template. Using templates is a good idea because one can spend months messing around.
You can often start with a free template and then trade up to a professionally designed template later.
BTW, his use of <met http-equiv="x-ua-compatible is flagged as an error by the W3C validator even though it works. So put it in .htaccess if you're on Apache.
Even using a template still means you have to learn CSS and graphics manipulation tools.
Start with proven (tested) cross-browser compatible CSS in reusable templates that can be used in CSS frameworks such as:
Among Promising CSS Frameworks and others:
WARNING: Most designers are using the reset.css in html5 boilerplate rather than blueprint's.
YAML ($160 from Dirk Jesse in Germany) has an unassuming name ("Yet Another Multicolumn Layout") belies its powerful foundation of structured flexibility with thorough documentation.
The trouble with CSS frameworks: They use generic names (such as span-6) which do not have semantic web meaning (such as main-content or logo-line). With YUI CSS, source is bloated by classes not needed.
CSS Frameworks + CSS Reset: Design From Scratch reviews CSS templates and their pros and cons.
There are several basic industry-wide standards for templates:
QUESTION TO YOU: Is there a set of templates that presents the same look and feel across a constellation of applications, such as blogs and wiki's for Joomla
Specific examples include:
Can HTML & CSS be used to create a printed book?
Docbook converts material from SGML/XML into HTML and press-ready PDF.
This css file uses the "boom!" microformat of class names for creating a book-quality pre-presss PDF file using the
It includes the WebArch document from www.yeslogic.com
CSS does not do the auto-hypenating needed for finer line alignments on paper documents. The author, H�kon Wium Lie (CTO at Opera) noted that he used perl script to add soft hyphens entities (­) in the right places.
Was his 2006 PhD thesis website created using the techniques he describes?
The pages have no corner trims and no bleed; the black text separates into CMYK leaving the black at only 90%. We would not be happy to go to press with this file, which highlights the pitfalls of using unsuitable tools for the job.
Most publishers want LaTeX formatted text.
An alternative is to create a XML file with xhtml markup tags and a XSL-FO (the formatting part of XSL) to make the pdf.
h1, h2, h3, h4, h5, h6 { page-break-after: avoid; }
Most of all, Photoshop enables graphics to be sliced so that interet browsers can download several files at once rather than one big graphic file. Such manipulates take several hours to learn and several days to master.
Adobe Photoshop is rather expensive. However, several less expensive programs can open and edit PSD files:
I also analyzed CSS and Javascript libraries from Yahoo, Google, SmartClient, and others.
CSS declarations can be placed within a separate file usually have an file extension of .css. External css files have the advantage of being cached on client machines. On subsequent retrievals, caching means faster access because the HTTP protocol has a "304" return code which means that the file does not need to be downloaded again. Thus, Visitors see faster page loads.
The css file does not need to be retrieved by any other page that references that css file.
A css file is specified with a LINK tag within the <HEAD> section:
<link rel="stylesheet" type="text/css" href="./css/printstyle.css" media="print"> <link rel="stylesheet" type="text/css" href="./css/pdastyle.css" media="handheld">These are needed because IE5 incorrectly treats imported print/pda stylesheet as media="all".
Spreading CSS over several files is useful when several people are involved. One can focus on each aspect:
Alternate styles to override specific formats and hide elements not appropriate:
Each CMS (Content Management System) has its own set of block names.
With Drupal, in the .engine file regions are assigned with a PHP function such as drupal_set_content('left','Hello');
Blocks | Regions |
---|---|
|
|
HTML5 introduced new header, footer, and associated blocks in the HTML markup.
Cascading Style Sheets: Designing for the Web (Addison-Wesley Professional; July 13, 1999) by Hakon Wium Lie and Bert Bos
Cascading Style Sheets: The Definitive Guide (O'Reilly; May 15, 2000) by by Eric Meyer
@media screen { .popup { visibility: visible; } } @media handheld, print { .popup { visibility: hidden; } }
Although the following media types are defined:
Media Type | Used for output to ... |
---|---|
all | all media type devices (the default) |
aural | speech sound synthesizers |
braille | braille tactile feedback devices |
embossed | paged braille printers |
handheld | mobile devices |
printers (static) | |
projection | projected presentations, like slides (static) |
screen | computer screens |
tty | media using a fixed-pitch character grid, like teletypes and terminals |
tv | television-type devices such as WebT |
List of CSS recognition by User Agent (browser) and by handheld device using HTMLDog's CSS test page and Lachlan Hunt's CSS Media Type Tests
Web Hypertext Application Technology
For print, it's better to set body { font-size: 12pt; }
Even though it's nice to promote code reuse, CSS code applicable only to a single HTML file is placed within that file as in-line coding placed between HTML comment block <!-- and --> to keep them from being displayed as content text by older browsers which don't know about CSS.
<style type="text/css" media="all"><!--
This hack is possible because Navigator 4.x ignores @import. An example is margin rules for standards compliant browsers because Netscape 4 misinterprets margins. We also use @import only for workarounds to IE bugs which are correctly handled by NN 4.x.
CSS stylings defined as attributes in the <div> tag are applied to values between <div> and </div> tags. For example:
<div class="my_hr"><hr /></div>
CSS code formats content using declarations. Each declaration contain "property: value" pairs such as color: #000;
Commas separate styles which share the same rule.
Spaces separate the definition of greater selectivity, such as <div p { ... }
Multiple declarations can be specified within a rule.
Styling declarations for this are defined in the "my_hr" style rule such as:
.my_hr { color: #f00; background-color: #f00; height: 5px; }
We use spaces to separate declaration code from the braces to make them easier to locate using ctrl-left and ctrl-right keys provided by text editors.
Such rules are defined either in-line at the top of the html file where they are referenced, or in an external cascading style sheet file with a suffix of ".css".
Embedded styles such as this are embedded along with text content:
<div style=" color: #f00; background-color: #f00; height: 5px; ">
Such declarations are bounded by quotes. This is discouraged due to the general approach of separating content from formatting code.
Debugging technique: to see where a div begins and ends, and also whether or not any nesting is occurring, temporarily give each div a border by temporarily changing CSS to include:
div {border: 1px dotted gray; padding: .5em}
This mark-up change is done automatically by the ___ FireFox plug-in.
TIP: A tag marked as !important cannot be changed by updating its style value.
element.style.setProperty("display", "inline", "important")
does not work in IE.
This article suggests: Always un-float large elements, especially long elements containing things like article text. As a general rule, when a float runs to multiple pages, you're just asking for trouble.
body { float: none !important;
This article suggests: If a floated element runs past the bottom of a printed page, the rest of the float will effectively disappear, as it won't be printed on the next page.
The style of an item can be dynamically with JavaScript such as:
element.style.cssText += 'display:inline !important';
TIP: The += operator adds rather than overwrites the property.
There is a consequence to where a style is specified. Assumptions about style cascade down from each level in the document hierarchy:
If the same property for the same tag is defined in more than one place, style display follows this hierarchy.
One commenter at the DaniWeb IT Discussion Community on CSS said "My biggest problems with CSS arise when I get caught up in how I think it should work, instead of trying to find out how it actually works."
<p id="authid">
Even though browsers haven't been complaining, each ID selector is supposed to be used only once on a page. That's why in some situations they carry more "weight" than class selector which can be used many times.
. (period/dot) precede the definition of class selectors.
<p class="authid">
Selector names starting with a number are ignored in Mozilla/Firefox.
CSS HACK: Selector names beginning with an underline, such as "_forieonly". are ignored by modern browsers but recognized by IE6 and earlier. So this example below has IE6 and below using 'courier new' since it is the only browser needing it:
pre, code, kbd, samp { font-family: monospace, serif; _font-family: 'courier new', monospace; font-size: 1em; }
If one types text between standard tags such as <H1> without specifying any style, the browser's hidden default styles are applied. That's why a blank line precedes large lettering of whatever is between <H1> and </h1>.
This is why, in the past, many webmasters begin headings with <H2 tags.
However, content search engines such as Google look to <h1 tags as names to display to googlers. So it's now a good idea to use them. Just style them to your liking.
A commonly used technique is to eliminate the browser's default styles for margins and padding specifications:
* { margin: 0; padding: 0; border: none; }
The star (or asterisk) is the CSS universal selector. Think of it as a wild card. Used on its own, it represents every element on a page.
This is smart to do because each browser has its own setting.
This global style resets defaults on all browsers.
* { padding:0; margin:0; } h1, h2, h3, h4, h5, h6, p, pre, blockquote, label, ul, ol, dl, fieldset, address { margin:1em 0; } li, dd { margin-left:5%; } fieldset { padding: .5em; } select option{ padding:0 5px; } .hide, .print-logo, .close-button { display:none; } .left{ float:left; } .right{ float:right; } .clear{ clear:both; height:1px; font-size:1px; line-height:1px; } a img{ border:none; }
Concatenation of images onto a single file is an important way of minimizing client response time and load on servers becuase each file specified requires a client-server negotiation on every call.
Borkweb recommends that a 1x1 transparent gif be specified in the img tag within html. The actual image to be display is then specified as the background image within a img.icon subclass within a css class. Then, other sub-classes are specified to position the desired graphic with css code such as:
Hover CSS is used to change the opacity. Note the extra -moz tags for Mozilla browsers.
#sidebar img.icon{ filter:alpha(opacity=50); -moz-opacity: 0.5; opacity: 0.5; } #sidebar img.icon:hover{ filter:alpha(opacity=100); -moz-opacity:1.0; opacity:1.0; }
The image is specified in both the outer element (the hover position) AND the inner "outer a" (base position) to avoid the flickering that occurs when Windows IE is too slow in reloading the background image.
WARNING: Hover effects are not relevant on touch-screen mobile browsers.
John Resign can make iPhone default buttons in CSS
Mootools-style horizontal menu using JQuery
Moreno from Italy wrote
Recommended jQuery Plugins:
Prior to CSS3, there are two approaches: one for fixed width boxes such as navigation bars and a more complex one for variable width boxes.
DEMO: Solar System by Alex Giron illustrates the movement of planets and moons around the sun using CSS3 code such as this (starting from upper left and moving clockwise):
-webkit-border-radius: 5px 200px 30px 50px; -moz-border-radius: 5px 200px 30px 50px;
As of this writing, this works only for Chrome and Firefox, not Safari nor IE.
-
From HTML 4 ... | Design | XHTML ... |
---|---|---|
<b> | Bold | <strong> |
<br> | Line break | </br> or CSS |
<font> | Font | CSS |
bgcolor | Background table color | CSS |
<u>x</u> | Underline | <span style="text-decoration:underline">x</span> or structural markup. |
<img src= ... > | Image | <img src= ... /> |
The CSS Table Gallery provides examples of mixed use CSS with tables, submitted by designers (78 at last count),
HTML Code Tutorial does a good job explaining.
MaxDesigns provides simple and clear examples for cross-browser complexity.
MaxDesign's Lisamatic links to many variations on lists.
CSS purists say that tables "should" not be used for formatting.
But some point out that a table is better for putting a side nav bar the whole height of the page.
points out that this use of HEADERS and ID suggested in The Elements of Design
For a semantic table, use these styles:
#t1,td[headers~=t1] { background:yellow } #mc,td[headers~=mc] { background:yellow } td[headers~=t1][headers~=mc] { background:#ffA500 }
<table> <tr><th></th> <th id="gu" axis="location">Guthrie</th> <th id="mc" axis="location">MacFarlane</th></tr> <tr><th id="t1" axis="time">1.30pm</th> <td headers="gu t1">Brett, John, David</td> <td headers="mc t1">Roger</td></tr> <tr><th id="t2" axis="time">2.15pm</th> <td headers="gu t2">Dean</td> <td headers="mc t2">Lisa, Peter</td></tr> </table>
The <table "summary=" property is not display, but vocalized by screen readers.
The <caption> tag displays the table's title above the table itself.
For simple tables:
<th SCOPE="col" is added to designate column labels.
<th SCOPE="row" is added to designate "stub" labels running down the left side of the table.
headers are used because text to speech engines recognize them better.
The AXIS= property is specified for future filtering capabilities.
"<tfoot>" must be specified between "<thead>" and "<tbody>" (not after <tbody>) because, as the HTML specification says "so that user agents can render the foot before receiving all of the (potentially numerous) rows of data."
.sub1, .sub2, and .sub3 have been defined using CSS margin-left:
for use by markup code such as
<div class="sub1"> entry </div>
Footnotes are marked using the CSS <SUP> supertext markup. They go to the right or text and to the left of numbers.
IE6 does not support the "abbr" attribute (defined in the W3C standard) to display an expansion of the abbreviated text on mouseover. However, this workaround uses javascript to read through the whole document looking for "abbr" to add a "span" tag which has the same intended behavior.
Two versions of CSS are used in this site.
The first approach has a single "global.css" file.
Pages referencing the "all.css" file which imports several css files (with the css at the bottom taking precedent): mambo.css, custom.css, white.css, black.css.
Use of white.css and black.css alternate depending on user selection. The same will be true of print.css and mobile.css in the future.
All content is within div id="content_area" (id="container" in csszen).
The background image for the whole page:
html { background: transparent url(htmlbg.jpg) repeat-x; }
Before CSS3, mothereffingtextshadow.com showed the way.
Inset #CCC colored text with with a minus one-pixel y-radius and one-pixel blur radius:
Embossing with text-shadow:
Don't Meet Your Heroes lists CSS & Web Standards Feeds. Here's your portal right here.
like a Cirque de CSS -- a page that uses many of the bells and whistles that CSS can do.
This is fixed by positioning mark-up which is anathema to CSS purists: an extra div after a float:
<div class="clear-block"></div>
This approach is adopted in file default.css within Drupal's system module, which contains this code:
.clear-block:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; } .clear-block { display: inline-block; } /* Holly Hack Hides/Overides CSS from IE5-mac \*/ * html .clear-block { height: 1%; } .clear-block { display: block; } /* End hide from IE5-mac */
One commenter at the DaniWeb IT Discussion Community on CSS said "My biggest problems with CSS arise when I get caught up in how I think it should work, instead of trying to find out how it actually works."
"HasLayout" Overview by Markus Mielke at Microsoft August 31, 2005
"on Having Layout" by Satz gives a long but thorough explanation.
IE7 PC was delivered with Windows Vista.
margin:20px;
|
There are three bands of space surrounding any text within a div.
The width set for an element refers only to the space occupied by the text. Margins, borders, and padding add to that width.
Hiding objects trips up the behavior of the margin property on some browsers:
Otherwise, margin properties are honored as follows:
So Tantek Celik found this way to prematurely close a style rule in IE5x PC by taking advantage of a bug in how IE5x parses the voice-family tag (in a 'screen' style sheet). Only IE5 would cut the block short when it sees the end-braces character within the string.
.test { width: 500px; padding: 50px; voice-family: "\"}\""; voice-family: inherit; width: 400px; }
So IE5 ends up invoking 500px while other browsers invoke the second width of 400px.
Selector | Property | Default value |
Alternative values |
Note | |
---|---|---|---|---|---|
body { | background: | - | white | Shorthand for setting all background properties in one declaration. | |
background-color: | - | color-rgb, color-hex, color-name
transparent | |||
background-image: | none | - | Not printed by browsers, so use a transparent gif for print. | ||
background-repeat: | repeat forever up and across | no-repeat (shown only once), repeat-y, repeat-x | y=Vertically x=Horizontally | ||
background-attachment: | - | scroll (with the rest of the page) fixed | relative to the rest of the page. | ||
background-position: | - | top left, top center, top right,
center left, center center, center right, bottom left, bottom center, bottom right, x-% y-%, x-pos y-pos | |||
- | width: | - | I prefer more future-proof "auto" over "100%" | ||
h2.x { | - | position: | relative | absolute | |
- | left: | 0 | 2px, -2px | space from left of normal | |
- | top: | 0 | 2px, -2px | space from top of normal | |
- | visibility: | visible | hidden | ||
a { | - | text-decoration: | none | overline, line-through, underline | |
- | color: | black | #123456 | hex (3 words, 6 letters) | |
- | text-shadow: | none | color length | ||
p { | - | text-transform: | none | uppercase, lowercase, capitalize | |
p { | font: | below | - | ||
font-style: | normal | italic, oblique | |||
font-variant: | normal | italic, small-caps | |||
font-weight: | normal | bold, 900 | |||
font-size: | 100% | - | |||
font-family: | times | sans-serif, courier | |||
p { | border: | medium double rgb(250,0,255) | |||
border-style: | none | dotted, dashed, solid, double, groove, ridge, inset, outset | border-width" doesn't work alone, only after border-style. | ||
border-width: | none | medium | |||
border-color: | normal | #0000ff | |||
- | border-top-width: | - | 15px | ||
- | border-right-width: | - | 15px | ||
- | border-bottom-width: | - | 15px | ||
- | border-left-width: | - | 15px | ||
td { | padding: | shorthand | 8px 8px 8px 8px | Needed for Opera browsers | |
padding-top: | 5cm | ||||
padding-right: | 5cm | ||||
padding-bottom: | 5cm | ||||
padding-left: | 5cm | ||||
p { | margin: | shorthand | 1cm 2cm 3cm 4cm | Top Right Bottom Left (TRBL as in "trouble") | |
margin-top: | 8px | 2cm | |||
margin-right: | 8px | 2cm | |||
margin-bottom: | 8px | 2cm | |||
margin-left: | 8px | 2cm | |||
p.x { | line-height: | 0.4cm | "dimension" space between lines | ||
a:first-child | pseudo-class special style for the first child of another element. | ||||
p:first-letter | pseudo-element adds a special effect to the first letter of a text block. | ||||
q:lang(no) { | - | quotes: "~" "~" | pseudo-class defines exception when text is between <q lang="no"> and < | ||
div { | - | overflow: | - | scroll | puts large text in a scroll box |
- | z-index: | 0 | -1 | Lower numbers go behind the bus. | |
- | display: | none | hides | ||
- | inline | no space between divisions | |||
- | text-align: | center | |||
lists { | - | marker-offset: | auto | length | CSS2 |
- | background-image: | repeat | no-repeat | outside indents but is not compatible with list-style-image: | |
- | list-style-position: | inside | outside | outside indents but is not compatible with list-style-image: | |
ul.xx { | - | list-style-image: | url('arrow.gif') | CSS2 offers no mechanism to change the size of the embedded object, or to provide a textual description, like the "alt" or "longdesc" attributes do for images in HTML. | |
- | list-style-type: | disc | circle, square, none | unordered lists | |
ol.xx { | - | list-style-type: | decimal | decimal-leading-zero, lower-roman, upper-roman, lower-alpha, upper-alpha lower-greek lower-latin upper-latin hebrew armenian georgian cjk-ideographic hiragana katakana hiragana-iroha katakana-iroha | ordered lists |
img.xx { | - | float: | left | right | |
- | vertical-align: | text-top | text-bottom | puts image on top or bottom of inline text. | |
<a | accesskey="b" | For Alt+b keypress | |||
<span | - | style="cursor: | auto" > |
Default
auto
text
wait
help
Crosshair
Pointer
Move e-resize w-resize n-resize s-resize ne-resize sw-resize nw-resize se-resize |
Position Is Everything.net by John and Holly Bergevin explain obtuse CSS bugs in modern browsers, provide demo examples of interesting CSS behaviors, and show how to "make it work" without using tables for layout purposes.
centricle.com provides a comprehensive list of CSS hacks and which browsers are affected by each hack.
Others describing browser bugs:
Unlike HTML 4 Transitional, XHTML markup must:
Web pages on proper XHTML semantics:
Search engines rank keyword weight based on semantic structure.
Thoughts on IE hack management
Controlling Presentation with Measurement and Location Properties (removed from MSDN but still available in the Internet Archive).
MSHTML Editing Platform in Internet Explorer 5.5 (removed from MSDN but still available in the Internet Archive).
CSS Mastery: Advanced Web Standards Solutions (OReilly, 2004) by Andy Budd, Simon Collison, Cameron Moll
Bulletproof Web Design : Improving flexibility and protecting against worst-case scenarios with XHTML and CSS (friends of ED, February 13, 2006) by Dan Cederholm of simplebits
Also by Dan Cederholm:
Web Standards Solutions: The Markup and Style Handbook
(Friends of Ed, 2004, 318 pages)
Stylin' with CSS : A Designer's Guide (New Riders, April 26, 2005) and companion website by Charles Wyke-Smith His blog
Accessible XHTML and CSS Web Sites: Problem - Design - Solution (Wrox Press, 2005 (480 pages) by Jon Duckett
javascript: alert(eid.currentStyle.hasLayout);
A true value is reported as "-1". A false value is reported as "0".
Within Javascript, if the clientWidth property is zero then the element does NOT have layout. (The clientWidth/clientHeight properties always returns zero for elements without "layout.") This is different to how Mozilla browsers behave.
It's not a CSS property because there is no "layout" property. It cannot directly be assigned using CSS declarations. But for debugging purposes you can trigger hasLayout by using the IE tool to edit "zoom (css)" (the attributes of a node) to "1".
Practically, having layout basically means an element is rectangular.
Internally within IE, having layout means that an element is responsible for drawing its own content.
MS IE6 "hasLayout" behavior is "non-standard."
Using the MSHTML Editing Platform Live editing, sizing, and dragging of layout elements are enabled with <body contenteditable="true">
But IE Mac has a different rendering engine which doesn't have the "hasLayout" ecentricity.
This holly hack for IE5.01 Windows keeps items from jumping around when you hover over them.
/* Fix IE. Hide from IE Mac \*/ * html ul li { float: left; height: 1%; } * html ul li a { height: 1%; } /* End */
<!--[if lte IE 6]> <SCRIPT LANGUAGE="Javascript"> alert("Congratulations. You are running IE5 or later!"); </SCRIPT> <P>Thank you for closing the message box.</P> <![endif]-->
50 CSS Tricks You Can't Live Without
Cascading Style Sheets: The Definitive Guide, 2nd Edition (Paperback) (OReilly, 2004) by Eric A. Meyer, the best-known expert on CSS. But this more of a good introduction than a comprehensive how to.
|
| ||
|
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you!
Human verify: |