Showing posts with label CSS Tutorial. Show all posts
Showing posts with label CSS Tutorial. Show all posts

Drop Down Menus, Horizontal Style | Css Edge

The first and most important part of creating our menu is the menu structure itself. The best way to do this is to build an unordered list, with each sub-menu appearing as a list within its parent list item. Sound complicated? It’s actually very straightforward:

  <ul> 
<li><a href="#">Home</a></li>
<li><a href="#">About</a>
<ul>
<li><a href="#">History</a></li>
<li><a href="#">Team</a></li>
<li><a href="#">Offices</a></li>
</ul>
</li>
<li><a href="#">Services</a>
<ul>
<li><a href="#">Web Design</a></li>
<li><a href="#">Internet
Marketing</a></li>
<li><a href="#">Hosting</a></li>
<li><a href="#">Domain Names</a></li>
<li><a href="#">Broadband</a></li>
</ul>
</li>
<li><a href="#">Contact Us</a>
<ul>
<li><a href="#">United Kingdom</a></li>
<li><a href="#">France</a></li>
<li><a href="#">USA</a></li>
<li><a href="#">Australia</a></li>
</ul>
</li>
</ul>

That’s it: some simple HTML that is both accessible and easy to edit.

Visually appealing?


If you have previewed the menu above, you’ll see a pretty boring list of items. And I promised you it would be visually appealing! Let’s add some style.

The first step is to remove the indents and bullets from the unordered list and define the width of our menu items.

ul {
margin: 0;
padding: 0;
list-style: none;
width: 150px;
}

Next, we need to position our list items. Fortunately, these will stack vertically by default, which is what we require. However, we must set the position as relative, because we will need to position the sub-menus absolutely within them.

ul li {
position: relative;
}

Next step is the sub-menus. We want each sub-menu to appear to the right of its parent menu item when that item is hovered over.

li ul {
position: absolute;
left: 149px;
top: 0;
display: none;
}

Using the “left” and “top” attributes, we can absolutely position each sub-menu within its parent menu item. You will notice I have set the “left” property to 149px (1px less than the width of the menu items), which allows the sub-menus to overlap the main menu and not produce a double border. (You’ll see what I mean later.)

We have also set display to “none,” as we don’t want the sub-menus to be visible by default.

So now we have the framework in place, but it’s still looking a bit plain. Let’s style those links.

ul li a {
display: block;
text-decoration: none;
color: #777;
background: #fff;
padding: 5px;
border: 1px solid #ccc;
border-bottom: 0;
}

I have styled the links to my taste, but they can be changed to your preference, as you wish. It is important to set display to “block,” as we want each link to take up all the available space of its containing list item.

So things are looking a little better, although users of Internet Explorer for Windows may disagree. Unfortunately, IE Win interprets the line breaks between our nicely formatted HTML list items as white space, so you will notice that the menu items don’t stack up neatly in that browser. However, there is a way around IE’s bugs:

/* Fix IE. Hide from IE Mac \*/
* html ul li { float: left; }
* html ul li a { height: 1%; }
/* End */

We can apply the Holly Hack above, which hides these rules from all browsers but IE Win. Perfect. You will notice the height: 1% rule that has also been added. Unfortunately (again!) the float fix uncovers another IE bug, which requires a height value to make the links display as block-level elements.

You will also notice the need to close the menu, which can be done by adding the missing border to the bottom of the list. So, the ul rule becomes:

ul {
margin: 0;
padding: 0;
list-style: none;
width: 150px;
border-bottom: 1px solid #ccc;
}

With some luck, everyone should now be able to see the unfunctional menu.

Making it work


Now the fun bit. We need to make those sub-menus appear when we hover over the menu items.

li:hover ul { display: block; }

Voila...here’s the bare bones menu in action.

“Woo hoo! It works!” I hear 1% of you shout. “Awesome!”

OK, OK, so that darn IE/Win has to ruin everything and not do as it’s told. IE/Win only allows the :hover pseudo-class to be applied to a link — so the li:hover that makes the sub-menus appear means nothing to IE.

A tiny jot of JavaScript is required to kick IE back into action (line wraps marked » — Ed.):

startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("nav");
for (i=0; i<navRoot.childNodes.length; i++) {
node = navRoot.childNodes[i];
if (node.nodeName=="LI") {
node.onmouseover=function() {
this.className+=" over";
}
node.onmouseout=function() {
this.className=this.className.replace»
(" over", "");
}
}
}
}
}
window.onload=startList;


Great thanks and appreciation is due here to Patrick Griffiths and Dan Webb, who introduced this trickery in a previous ALA article, Suckerfish Dropdowns. Thanks, guys!

So, the hover rule now becomes:

li:hover ul, li.over ul { 
display: block; }

Additionally, we also need to associate the JavaScript with our main ul, which becomes:

<ul id="nav">

Hopefully, with the above tweaks in place, everyone should be able to see a simple version of the menu in action.

IE5.01 Jumping Menu Bug

Anyone using IE5.01 on Windows will notice that the menu jumps around when you hover over some of its items. The problem is easily fixed by modifying our previous hacks as follows:

/* Fix IE. Hide from IE Mac \*/
* html ul li { float: left; height: 1%; }
* html ul li a { height: 1%; }
/* End */


Mystery IE6 Bug:

During the development of this article, I uncovered a strange bug that I believe is only apparent in IE6. A background must be declared on the li a, else when a sub-menu stretches further (vertically) than the main menu itself, the links start to disappear before you have time to click them. Strange! Try it to see for yourself.

CSS Transparency Settings for All Browsers

Transparency is one of those weird things that is treated completely differently in all browsers. To cover all your bases, you need four separate CSS statements. Fortunately they don’t interfere with each other really, so using them all every time you wish to add transparency is no big hassle and worry-free. Here they are, and are currently set to 50% transparency:

.transparent_class {
filter:alpha(opacity=50);
-moz-opacity:0.5;
-khtml-opacity: 0.5;
opacity: 0.5;
}
 

Here is what each of those CSS properties is for:


  • opacity: 0.5; This is the “most important” one because it is the current standard in CSS. This will work in most versions of Firefox, Safari, and Opera. This would be all you need if all browsers supported current standards. Which, of course, they don’t.
  • filter:alpha(opacity=50); This one you need for IE.
  • -moz-opacity:0.5; You need this one to support way old school versions of the Mozilla browsers like Netscape Navigator.
  • -khtml-opacity: 0.5; This is for way old versions of Safari (1.x) when the rendering engine it was using was still referred to as KTHML

CASCADING STYLE SHEETS TUTORIAL PART 3 OF 3

In part 2 of the tutorial we broke down the major sections of HTML on the page and established separation using DIV tags with unique ID's attached to them:

...

Using DIV's (to position the major page sections) is the alternate method to what most people use: tables. I would argue one method is not necessarily better than the other. But consider that CSS in the 'official' method to position page elements and tables should only be used to hold tabular data.

On the other hand there are simply times when using tables is much easier and CSS just doesn't cut it.

That said, our current layout (left or right side navigation) CSS is far easier to use and is an overall better solution.

Now that we have that covered, everything gets really easy from here. We've established our main document and the major sections are in place, all we need to do is add our text and images.

Breaking down the page:

This page is simple, we have just a single heading:

The Main Heading

And a single paragraph:

Go to the Web Designers Killer Handbook home page and grab the practice HTML page that we will used as the starting template for this tutorial. You can find it under the heading: 'To create the practice HTML page do the following:'. Follow the instructions there and create your basic HTML page ... and do it now!

We define how the paragraphs and the headings will look in our CSS code:

p {

width: 80%;

}

h1 {

font-family: Georgia, "Times New Roman", Times, serif;

font-size: 18px;

font-weight: bold;

color: #000000;

}

This is pretty much self-explanatory. The only thing that should be mentioned is that we set the width of the

tags to 80%. This allows us to control all of our text width in one easy to edit spot.

The only thing missing from the page is the actual navigation. The best and easiest way to do this, is by using list (

  • ) tags. That makes sense, since navigational menus are essentially a list of pages.

    We styled the list item tags with this CSS:

    li {

    list-style-type: none;

    line-height: 150%;

    list-style-image: url(../images/arrowSmall.gif);

    }

    The above code uses an image as the bullets for the list and makes the space between the listed items 1 and ½ times larger than normal (I like a little more 'breathing room'). You don't have to have an image for the bullets, you could leave it with no images and no bullets by just removing the attribute:

    list-style-image: url(../images/arrowSmall.gif);

    Or you could use on of the built in bullet options: 'disc', 'circle' and 'square'.

    Next you should add to the HTML page an unordered list (

      ) in between the navigation DIV's just under the 'main navigation' heading:

      The Main navigation

      • Page One
      • Page Two

      To make things easier (for the purpose of the article), change the CSS affecting the list item tags (

    • ) so that we use a built in bullet:

      li {

      list-style-type: disc;

      line-height: 150%;

      }

      Now we have the navigation!

      That pretty much covers our goals for this tutorial, but we have just scratched the surface of CSS. As you can see, we created a nice looking page, while using very few types of HTML tags. At this time there isn't much text on the page, but we could add to the page easily, building it out to include lots of information and images with practically no HTML work at all!

      Video Tutorial on CSS for Page Layout:

      I've created a quick video that talks about page layout in CSS. I talk about some ideas that will make your life a lot easier when it comes to building pages with CSS.

      CSS for Page Layout

      I hope the CSS tutorial was useful for you all, it may have been a little hard to get your head wrapped around this subject at first, but in time you will see that your efforts will pay off. Please let me know how you felt about this article.

    • CASCADING STYLE SHEETS TUTORIAL PART 2

      In Part 1 we created a classic two-column layout with left side navigation using CSS and only a few types of HTML tags.

      Part 1 presented the code for the page and explained what HTML tags we were going to use. Now we will look at the actual HTML code used so far and the CSS.

      Our page so far is really very simple. As you may already know, all the content (text, images, Flash etc) that the user sees when viewing a web page is marked-up/coded with HTML in-between the and tags*.

      In this case we have this:







      The Main Heading





      Go to the Web Designers Killer Handbook home page and grab the practice HTML page that we will used as the starting template for this tutorial. You can find it under the heading: 'To create the practice HTML page do the following:'.

      Follow the instructions there and create your basic HTML page ... and do it now!







      In the above code we see that we have 2 main sections demarked by using
      tags. As you learned in part one of this tutorial,
      tags are designed to be used to create a 'division' in the document or in other words create a container. We have created two such containers and given them each of them a unique ID: