Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,987 courses, including more Web and personalized recommendations.Start Your Free Trial Now
- View Offline
One of the most annoying aspects of running CSS is the annoyance of having to deal with browser incompatibility in writing hacks or alternate styles for various browsers. Although all browsers have their individual quirks, by far the worst offender is Internet Explorer. Entire Web sites are dedicated to dealing with errors in that particular browser. Now one way to deal with IE Errors are to place hacks and browser workarounds directly within your CSS. This creates messy code, makes your IE- specific workarounds harder to find and makes editing your styles even more difficult.
A better way of dealing with Internet Explorer is to place those specific styles in external style sheets, and then pass them to Explore through the use of what we call conditional comments. So here I have the tours.htm file open. And what I want to do is go ahead and just preview this page in Internet Explorer, so we can see what the issue is. So here we are in Internet Explorer, and right now everything looks fairly okay. But if I scroll down, ooh, there is a problem. Right after my tour descriptions, things just start falling apart, and they go from bad to worse.
Okay, so that's obviously not the user experience we are going for within Internet Explorer 6. So let's go see how we can fix this. So here we are, back in Dreamweaver. And I want to show you that inside the _CSS directory, I have a style sheet already written, ie6.css, and what that's doing is it's fixing a few float problems that Internet Explorer had in dealing with those tour descriptions. So a lot of times, it's trial and error. Sometimes you are going to know specific issues to Internet Explorer and kind of how to fix them.
But you notice that we are going ahead and placing them in its own style sheet, called ie6, and we are commenting those guys out. So we really don't want to pass these styles to anybody but Internet Explorer 6. Okay. I am going to go ahead and close this and go back in tours really quickly. Now what I am going to do is I am going to switch to Code View. And I am going to highlight the link tag for my main styles, and I'm a copy that. And then on this blank line just below my print style sheet, I am going to paste it. Now the reason I am doing that is because I want to import my Internet Explorer 6 styles in, and I just don't feel like typing all that out.
I am going to highlight main .css and change it to ie6. And then for media screen projection is fine. But I want to replace the title. I don't want any browser to think that these are the main styles. So I am going to get rid of that title attribute. So this is what it should read: link href _css ie6 css. And the rest of it I am just going to sort of leave from before, except for the title attribute, and we are moving that. Now I am going to save this, and if I preview this in another browser, let's say Firefox, for example, we are going to end up with some real problems.
As you can see, we started having some issues as we scroll down and these guys -- the spacing is just going all crazy. The reason that spacing is going crazy is because of the fixes that we did for Internet Explore 6. What works for ie6 is not going to work for everybody. So we only want these styles to be passed to that browser. So one of the ways to do that with Internet Explorer is through the use of conditional comments. Let me show you what I mean. Go ahead and highlight that link tag. You want the whole thing highlighted, from sort of finish. And this of course is the link tag that's dealing with ie6.
And then go over to your Snippets panel. Now this is one of the pre-built Snippets. It has nothing to do with what the snippets we created earlier in our title. I am going to go ahead and open up the Comments Snippets region. Now these are really neat. These have HTML Comments. They have CSS Comments. They have all sorts of cool stuff. I just want to expand that a little bit more so I can read these better. So notice that we have all sorts of interesting comments here, Greater Than or Equal to IE7 Conditional, IE5 Mac Comment, If IE6 Conditional, If Less Than IE8. So this is really cool.
I am going to do Less Than or Equal to IE6. So with that highlighted, I am just going to double-click Less than or Equal to IE6. You'll notice what it does is it surrounds that link tag with this, If Less Than or Equal to IE6 Conditional statement. Now every single browser in the world is going to ignore that, except for Internet Explorer. It's going to look at those conditional statements and say, ooh, you are talking to me, are you? And then it says, okay, I need to add this link. So what's going to happen now is when I save this file, if I preview that in my browser in Firefox, I notice that it don't really have any issues.
However, if I go back to Internet Explore, I am just going to scroll down a little bit, and sure enough, everything looks fine. All the spacing looks the way I needed to; everything looks really great. All right, cool. So now I am going to go back into Dreamweaver. And what I'd really like to do is go ahead and change this site-wide. It works on this page, but it doesn't work on the rest of them. And there really isn't an attached style sheet sitewide, and there is certainly not a use conditional comment sitewide. So we are going to use Find and Replace to kind of help us out with this. What I am going to do is I am going to highlight all of my link tags, starting with main.css, and going to the conditional comment, I am just going to copy them, so Ctrl+C or Command+C copy.
Then I am going to take the conditional comment and just strip it out and save my file. Now why am I doing that? Well, because I don't want it to appear on this page twice when I do my find and replace. That's why. So I am going to go up to Edit and choose Find and Replace. Here I am going to choose Entire Current Local Site, and for Replace, I am going to go ahead and paste the code that we copied earlier. So it should have both link tags and the conditional comment, and then for the Find I am going to paste it again, but this time, I am just going to delete the conditional comment.
And be sure not to hit Return or Enter with that. You want to just hit Delete or Backspace to get rid of that stuff. So now double-check what's going on here. I am looking in the Source Code. I want to replace the entire current local site. I am going to find those two link tags to the main.css and the print.css. And I am going to replace them with both of those titles, plus the ie6 down there, okay. So I'm going to go ahead and hit Replace All. It's going to warn me. It's going to say I can't undo this, are you sure? I am going to say Yes. And now it's going to go add it to this file.
You can actually see it added to the code, but look at the Search dialog box. Look how many it's added to. It's added to a bunch of them right there. That's really cool. Now I would also need to repeat this procedure by going into the subdirectories, like explorers and resources, and doing the same thing, because they are going to have a slightly different document-relative link. So they weren't found in this Find and Replace. We would need to do a specific Find and Replace for each subdirectory level. And it works just to be fine, and be a lot faster than doing it by hand. Okay, well conditional comments, these things are great. Using them for Internet Explorer- specific style sheets is going to allow you to pass all the ultimate styles necessary for Internet Explorer to function properly without having to clutter up your existing code, or worrying about other browsers being affected by the styles.
I mean, that's the best part to me is that the other browsers just ignore them. It's also a nice way of getting all of the Internet Explorer-specific styles in one place to make updating and editing them even easier.
- Building a CSS-friendly workspace in Dreamweaver
- Creating style sheets visually
- Hand-coding styles
- Working with code hinting
- Organizing style sheets with color coding and sections
- Writing global classes
- Eliminating style conflicts
- Deploying styles
- Dealing with browser compatibility issues