Featured

Simple Drop Down Menu using CSS Only

In this article we will learn how to create a simple drop-down menu using CSS3 only.
I don't think if anybody isn't familiar with a drop-down menu. But if you don't know about drop-down menu, then this is a navigation bar on the top of web pages with some DROP-DOWN effect just like this one:
http://goo.gl/KTlvkM

Now  just look at this HTML code:

<ul id="top-menu">
  <li><a href="#">Languages</a>
    <ul>
      <li><a href="#">Ruby</a></li>
      <li><a href="#">HTML</a></li>
      <li><a href="#">CSS</a></li>
      <li><a href="#">PHP</a></li>
      <li><a href="#">JavaScript</a></li>
      <li><a href="#">C++</a></li>
    </ul>
  </li>
  <li><a href="#">Browsers</a>
    <ul>
      <li><a href="#">Chrome</a></li>
      <li><a href="#">Opera</a></li>
      <li><a href="#">IE</a></li>
      <li><a href="#">Firefox</a></li>
      <li><a href="#">Safari</a></li>
    </ul>
 </li>
 <li><a href="#">Operation systems</a>
   <ul>
     <li><a href="#">Windows</a></li>
     <li><a href="#">Linux</a></li>
     <li><a href="#">FreeBSD</a></li>
     <li><a href="#">Mac OS</a></li>
   </ul>
 </li>
 <li><a href="#">Others</a></li>
</ul>

This will create simple looking nested lists, looking like this:


These are just multi level lists. Drop-Down menus are created using lists. CSS converts this multi-level lists into a drop down menu.
Let's look at the CSS. This CSS is divided into several groups. Each group has its own functionality.
Look at this code snippet:

#top-menu, li ul {
     list-style: none;
     margin: 0px;
     padding: 0px;
     }

The above CSS code has removed all bullets and aligned them in single line.

Now we need to align the main menu (Language, Browser, Operation Systems and Others) into a single line.

#top-menu li {
     float: left;
     position: relative;
     }
#top-menu li a {
     display: block;
     }
Above CSS code aligned the main menu in the single row on left

To place a sub menu item over the main menu item you must apply position: absolute; property to that sub menu item:

#top-menu li ul {
     position: absolute;
     }

Applying the above CSS will align the sub menu items under the main menu vertically.
Now this is the main part of the drop down menu. Now we have to write the code that will change all these menus in such a ways that, when we hover over some main menu item, the corresponding drop down menu should appear, otherwise it should be hidden under that main menu entry.

#top-menu li ul {
     visibility: hidden;
     }
#top-menu li:hover ul {
     visibility: visible;
     }

The first block of the above code is to hide the sub-menu entries in the normal position. The second part will be executed when we hove over the main menu options. It will show its effect if the main menu entry has some sub-menu underneath. When we hove over that entry, the corresponding sub menu entry will be shown in the drop down. You can apply padding to to make the entries separate to look readable.
Below pen shows the output and the code as well.

See the Pen simple CSS drop-down menu by Muhammad Arslan Aslam (@jacksparrow43) on CodePen.

Styling Lists Using CSS

Like other items, there are some CSS properties to design and style HTML Lists too.
CSS List Properties allow you to :

  1. Set different list item markers for ordered lists
  2. Set different list item markers for unordered lists
  3. Set an image as list item marker
There are two types of lists in HTML:
  1. Ordered Lists - The list items are marked with numbers or alphabets
  2. Unordered Lists - The list items are marked with bullets
With CSS list items can be styled further. An image can be used as list item marker.

The type of list item marker is specified by list-style-type property:

ul.a { list-style-type: circle; } ul.b { list-style-type: square; } ol.c { list-style-type: upper-roman; } ol.d { list-style-type: lower-alpha; }
Example: http://css3wdesign.net16.net/list-item-markers.html

An Image as the List Item Marker

CSS allows us to use any image as the list item marker. Just use the list-style-image instead of list-style-type, and place the URL before :

ul { list-style-image: url('sqblue.gif'); }
Example: http://css3wdesign.net16.net/image-marker.html

In the above mentioned example, the image is slightly bigger than the usual bullet. The image specified as the image marker, will be slightly more bigger in IE and Opera than the image in Safari, Chrome and Firefox.
To overcome this, you have to specify all of the image attributes independently. Below is a Crossbrowser solution for this:

Crossbrowser solution

The below code will show the image marker in all browsers equally:

ul { list-style-type: none; padding: 0px; margin: 0px; } ul li { background-image: url(sqblue.gif); background-repeat: no-repeat; background-position: 0px 5px; padding-left: 14px; }
Try using this URL in all browsers: http://css3wdesign.net16.net/image-marker2.html

Explained example:
  • For ul: 
    • to remove the list item marker first, set the list-style-type to none
    • set the margin to 0 (for crossbrowser compatibility) 
  • For ul li 
    1. place URL of image in the background-image property and set it to the no-repeat
    2. specify the image position using background-position (left 0px and down 5px)
    3. position the text in the list item: padding-left 

list-style: none;

Sometime, we don't need to specify any of the list-item style. CSS also allows to do so.
Use the list-style: none;
 and the list will be without any style. Below is the output of the list-style: none; 

  • Item one
  • Item two
  • Item three
  • Item four

CSS list-style-type Property (All Values - Table)

A complete description of all of the values of   list-style-type :


Value Description
disc Default Value. The marker is a filled circle.
armenian The marker is traditional Armenion numbering.
circle The marker is a circle.
cjk-ideographic The marker is a plain ideographic numbers.
decimal The marker is a number.
decimal-leading-zero The marker is a number with leading zero (01, 02, 03 etc)
georgian The marker is traditional Georgian numbering.
hebrew The marker is traditional Hebrew numbering.
hiragana The marker is traditional Hiragana numbering.
hiragana-iroha The marker is traditional Hiragana iroha numbering.
katakana The marker is traditional Katakana numbering.
katakana-iroha The marker is traditional Katakana iroha numbering.
lower-alpha The marker is lower-alpha (a, b, c, d etc. )
lower-greek The marker is lower-greek.
lower-latin The marker is lower-latin (a, b, c, d etc.)
lower-roman The marker is lower-roman (i, ii, iii, iv etc.)
none No marker is shown.
square The marker is a square.
upper-alpha The marker is upper alpha (A, B, C, D etc)
upper-latin The marker is upper latin (A, B, C, D etc)
upper-roman The marker is upper roman (I, II, III, IV etc)
initial Sets this property to its default value.
inherit Inherits this property from its parent element.
This example explains all above examples: http://css3wdesign.net16.net/all-bullets.html

www.CodeNirvana.in

Copyright © CSS3 (Web Designing) | Designed By Code Nirvana