body { background-color: #333; background-image: url(brushed_alu_dark.png); color: #fff; margin: 0; padding: 0; font: 0.75em/1.3 "Lucida Grande", "Lucida Sans Unicode", "Lucida Sans", Verdana, Tahoma, sans-serif; } .wrapper { width: 80%; margin: 20px auto 40px auto; background-color: #fff; color: #333; background-image: url(brushed_alu.png); -webkit-box-shadow: 3px 3px 10px 8px rgba(0, 0, 0, 0.4); -moz-box-shadow: 3px 3px 10px 8px rgba(0, 0, 0, 0.4); box-shadow: 3px 3px 10px 8px rgba(0, 0, 0, 0.4); -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; } .recipe { padding: 1em; } .recipe img { float:right; width: 200px; margin: 0 0 1em 1em; -webkit-box-shadow: 3px 3px 5px 3px rgba(0, 0, 0, 0.4); -moz-box-shadow: 3px 3px 5px 3px rgba(0, 0, 0, 0.4); box-shadow: 3px 3px 5px 3px rgba(0, 0, 0, 0.4); -webkit-transform: rotate(5deg); -moz-transform: rotate(5deg); -o-transform: rotate(5deg); -ms-transform: rotate(5deg); transform: rotate(5deg); } h1 { font-size: 150%; } h2 { font-size: 125%; } h2.instructions { background-image: url(instructions.png); background-repeat: no-repeat; background-position: left center; padding-left: 30px; } ul.ingredients { clear:both; border-top: 1px solid #999; border-bottom: 1px solid #999; list-style: none; margin: 1em 0 1em 0; padding: 1em 0 1em 30px; background-image: url(ingredients.png); background-repeat: no-repeat; background-position: 0 1em; } a.more:link, a.more:visited { display: block; padding: 0.3em 20px 0.3em 0; text-align: right; color: #666; font-weight: bold; background-image: url(arrow.png); background-position: right center; background-repeat: no-repeat; text-decoration: none; }
transform
does not rotate the main
image.
nth-child
, as seen in Figure 7.4.
The CSS3 nth-child
selector is used as
follows:
.datatable tr:nth-child(odd) { background-color: #dfe7f2; color: #000000; }Internet Explorer 8 has no support for
nth-child
, so there are no striped rows, as you can
see in Figure 7.5.
One way to patch this is by using Selectivizr. This script provides
CSS3 selector support to Internet Explorer, and does so without you needing to make
any other changes to your CSS. A script that adds in support like this
is sometimes referred to as a polyfill.
<head>
<meta charset="utf-8" />
<title>Chapter 7: Selectivizr</title>
<link rel="stylesheet" href="selectors.css" />
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/
↵jquery.min.js"></script>
<script type="text/javascript" src="selectivizr-min.js"></script>
</head>
class
of odd
to every other table row. In reality,
you’d probably include a JavaScript file here with a number of functions
in it:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/ ↵jquery.min.js"></script> <script> $(document).ready(function(){ $("tr:nth-child(odd)").addClass("odd"); }); </script>
class
of odd
alongside our
nth-child
selector:
.datatable tr:nth-child(odd), .datatable tr.odd {
background-color: #dfe7f2;
color: #000000;
}
fixSelectors
function that goes through
and adds classes to enable support for CSS3 selectors. This does involve
a little more work than just including Selectivizr; however, it does
mean that I’m always aware of what the JavaScript is doing. For very
rapid development, or when you are prototyping layouts, scripts such as
Selectivizr are very useful. Browsers lacking support for these
selectors are going to become far less commonly used, making the
employment of Selectivizr more compelling as there’s no need for
additional CSS selectors. If the browsers eventually stop registering in
the stats on your site, you can simply remove the JavaScript
include.
<head> <meta charset="utf-8" /> <title>Chapter 7: Selectivizr</title> <link rel="stylesheet" href="selectors.css" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/ ↵jquery.min.js"></script> <!--[if (gte IE 6)&(lte IE 8)]> <script type="text/javascript" src="selectivizr-min.js"></script> <![endif]--> </head>
<!--[if IE 6]> <link rel="stylesheet" type="text/css" href="ie6fixes.css" /> <![endif]-->This includes ie7fixes.css if the browser is equal to IE7:
<!--[if IE 7]> <link rel="stylesheet" type="text/css" href="ie7fixes.css" /> <![endif]-->This code will reveal a stylesheet to all versions of Internet Explorer less than or equal to version 7. It’s useful if you have some layout fixes for both these old browsers:
<!--[if lte IE 7]> <link rel="stylesheet" type="text/css" href="iefixes.css" /> <![endif]-->For further examples of conditional comments and a full guide to their use, see the SitePoint CSS Reference page.
border-radius
is unsupported can be a reasonable
solution. But what if your client or boss is insistent that the rounded
corners work in older browsers?
border-radius
along with other CSS3 properties.
CSS3
PIE (Progressive Internet Explorer) is one such script. We
can take a look at how this works using our example page. In Internet
Explorer 8, our layout displays very much as it does in earlier versions
of Internet Explorer, with no rounded corners or
drop shadows as in Figure 7.7.
Download CSS PIE and unzip the files. The file
PIE.htc needs to be placed into your site.
In any rule set that contains rounded corners or drop shadows, add
the behavior as shown in our code. The path to
PIE.htc needs to be relative to the page rather
than the CSS file, so I would suggest that in a live site you make it a
path from root; for example, behavior:
url(/path/to/PIE.htc);
:
.wrapper { width: 80%; margin: 20px auto 40px auto; background-color: #fff; color: #333; background-image: url(brushed_alu.png); -webkit-box-shadow: 3px 3px 10px 8px rgba(0, 0, 0, 0.4); -moz-box-shadow: 3px 3px 10px 8px rgba(0, 0, 0, 0.4); box-shadow: 3px 3px 10px 8px rgba(0, 0, 0, 0.4); -webkit-border-radius: 10px; -moz-border-radius: 10px; border-radius: 10px; behavior: url(PIE.htc); } .recipe img { float:right; width: 200px; margin: 0 0 1em 1em; -webkit-box-shadow: 3px 3px 5px 3px rgba(0, 0, 0, 0.4); -moz-box-shadow: 3px 3px 5px 3px rgba(0, 0, 0, 0.4); box-shadow: 3px 3px 5px 3px rgba(0, 0, 0, 0.4); -webkit-transform: rotate(5deg); -moz-transform: rotate(5deg); -o-transform: rotate(5deg); -ms-transform: rotate(5deg); transform: rotate(5deg); behavior: url(PIE.htc); }
behavior
property. This property
enables you to attach a script to your CSS. The script is in a
.htc file (HTML component). Other
browsers will ignore this property, and only Internet Explorer will
attempt to run your .htc files.
Similar scripts are available that attempt to support
other unsupported features such as CSS3 transforms. It’s worth
mentioning that whenever you use one of these scripts, you should
carefully test all the target browsers, as it’s possible that problems
will be introduced by their inclusion.
An alternative solution would be to avoid using CSS3 at all and
return to older methods of using images to achieve this effect.
Depending on the number of older browser users and the particular effect
you require, this may sometimes be necessary. What’s great about using a
polyfill like PIE is that at the point in time when these older browser
users cease to visit your site, you can simply remove the behavior.
Otherwise, if you add extra markup and images to create the layout
without using CSS3, this weight is then downloaded by all users, and
you’d need to rebuild the site in the future to take advantage of
CSS3.
doctype
as the first line in your markup; for most new sites, this will be the
HTML5 doctype
as used in all the
examples in this book. If you need to use HTML4 or XHTML, make sure you use a full Strict or Transitional
doctype
, including the URL. Omitting a
doctype
or using an incorrect
doctype
may cause your layout to display in
quirks mode in some browsers.[1] This implements an old, incorrect box model used in very
early browsers—best avoided in modern sites. If you want to know more
about this, see the article at Activating Browser Modes with
Doctype.
min-height
(the minimum height an element should
take), but it incorrectly interprets height
as
min-height
. So, even though
height
is used to specify a fixed height in other
browsers, Internet Explorer 6 takes it to mean the minimum height;
therefore a block element will expand taller than its specified height
if need be.
To work around this issue, we simply use the
height
property in our IE6-specific stylesheet
wherever we’ve used min-height
in our main
stylesheet.
hasLayout
that’s an internal component of the
rendering engine, and the source of many seemingly bizarre rendering
bugs. When an element is responsible for sizing and arranging its
contents, it’s said to have a layout. If an element is without a
layout, it relies on its parent or an ancestor element to take care of
its size and position. When an element has no layout, there’s the
potential for weird stuff to happen—like content disappearing and the
layout behaving erratically. Some elements, like table cells,
automatically have a layout; however, div
elements do not. Specifying some CSS
properties, such as setting float
to
left
or right
, also cause an
element to gain a layout. In making an element gain a layout, most of
these problems disappear. The trick is to find a CSS property that
gives an element a layout without having a detrimental effect on your
overall design.
In IE6, I find the simplest way to trigger a layout on
an element is to give it a height value of 1%. As I just mentioned,
IE6 treats height as min-height
, so a height of
1% actually renders as a minimum height of 1%—so this is perfectly
safe to apply and the box will still be sized to suit its contents.
Obviously you need to do this in your IE6-specific stylesheet that’s
included with conditional comments.
IE7, by contrast, supports the height
property correctly, so we’re unable to use it as we might with IE6.
However, setting the min-height
property to any
value—even to 0
—in IE7 causes the element to gain a
layout. This is a safe approach because the default value for
min-height
is 0
anyway.
It isn’t always apparent which element is going to need the
layout trigger applied, but if you work methodically, you may well
find the one that causes everything to jump into shape. I usually work
from the innermost container out, so if I have a div
nested inside two more div
s, I add the height
to the inner div
and refresh to see
if it made a difference. Otherwise, I remove it and try the next
div
, and so on.
position
to relative
on an
element will sometimes fix a problem. Keep in mind that setting
position
to relative
on an
element will mean all its child elements will now use that element for
a positioning context. Otherwise this should be safe to do.
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>HTML 5 complete</title> <!--[ lte IE 8]> <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"> </script> <![endif]--> <style> article, aside, details, figcaption, figure, footer, header, hgroup, menu, nav, section { display: block; } </style> </head> <body> <p>Hello World</p> </body> </html>In this document, you can also see that the HTML5 structural elements have been set to
display: block
. This would
normally happen inside your stylesheet for the page. We’ll be covering
the
display
property in Chapter 9 more thoroughly; but by setting these elements
to display: block
, we’re able to apply CSS to them
much as we would a div
element on our
page.
div
element. If navigation is marked up using
the nav
element, a device could
indicate to the user where the navigation was on the page; knowing where
the header and footer of a document is offers potential in terms of
being able to parse and extract content for reuse.
If you decide to take advantage of these elements, you need to be
aware that any CSS used to style them won’t be applied by Internet
Explorer 8 and below. The
HTML5 shiv JavaScript will enable these elements to be styled;
however, you are then building in a reliance on JavaScript for users of
those browsers.
Depending on the traffic to your site, you can decide whether
using these semantic elements and thus requiring JavaScript is
acceptable. The alternative is to continue using the div
element to mark up areas on your page
(perhaps adding a class or ID using the HTML5 semantic element name)
until such time as the nonsupporting browsers are enough of an
acceptable minority for you to feel happy about switching. The decisions
you make are likely to be different for each site depending on the site
and audience.
@font-face
) render differently across operating
systems.
Some browsers are available across all operating systems; Firefox,
Chrome, and Opera, for example, can be installed on Windows, OS X, and
Linux. There is a version of Safari, the default browser on OS X, for
Windows. As Chrome is based on WebKit—the same rendering engine as Safari—there are many
similarities across these two browsers; however, it’s unwise to assume
that if it works in one it will work in the other (such as the support
of CSS3, for example). They are, after all, on different release
cycles.
first-child
to remove a margin on the first
element, this will fail to work in Internet Explorer 6, and you’ll
need to patch this with a JavaScript polyfill, use some alternate CSS,
or live with the difference in spacing. The website
When can I use
… is an excellent resource for checking which browsers support
which properties.
-moz
,
-webkit
, and so on, are not part of the CSS3
specification, so they’ll be unrecognized by the validator and throw a
warning or error. The syntax of beginning an extension to CSS with a
“-”
is correct, however, so the validator does
understand about the existence of these extensions. Therefore, you can
use the additional options fields at the W3C Markup Validator to hide these messages.
At the
CSS
Validator, seen in Figure 7.11, open the
More Options area and make sure that under
Profile you have selected CSS level
3. You can then select No warnings and
set the Vendor Extensions notification to show up
as Warnings. This will allow you to validate your
CSS for the important errors. I’d advise you to also check the warnings
as well, but it can be hard to start looking for problems if you’re
swamped with hundreds of messages about the legitimate use of prefixed
properties.
[1] Many modern browsers have two rendering modes. Quirks mode renders documents according to the buggy implementations of older browsers such as Internet Explorer 6. Standards or compliance mode renders documents as per the W3C specifications, or as close to them as they can.
[2] See http://ejohn.org/blog/html5-shiv/ for an explanation of this tool.
18.118.186.143