user3148352
user3148352

Reputation:

Creating Drop Down on Hovering using CSS

Hey guys i am working on a website and the site has a few links like home, about us, contact us, products. now if the user hovers on the products link i want it to display a drop down of items where on selection goes to respective pages.

here is my html part:

    <ul id="menu">
        <li><a href="index.html" class="current"><span></span>Home</a></li>
        <li><a href="" target=""><span></span>CPPL Pharma</a></li>    
        <li><a href="" target=""><span></span>CPPL Vets</a></li>
        <li><a href="aboutus.html"><span></span>About Us</a></li>        
        <li><a href="contactus.html"><span></span>Contact Us</a></li>
    </ul>

here is my css part:

body {
    margin: 0;
    padding: 0;
    line-height: 1.5em;
    font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
    font-size: 11px;
    color: #333333;
    background: #ffffff;
}

a:link, a:visited { color: #0066CC; text-decoration: none; font-weight: normal; } 
a:active, a:hover { color: #990000; text-decoration: underline; }

p { margin: 0px; padding: 0px; }

img { margin: 0px; padding: 0px; border: none; }

.cleaner { clear: both; width: 100%; height: 0px; font-size: 0px;  }

.cleaner_h10 { clear: both; width:100%; height: 10px; }
.cleaner_h20 { clear: both; width:100%; height: 20px; }
.cleaner_h30 { clear: both; width:100%; height: 30px; }
.cleaner_h40 { clear: both; width:100%; height: 40px; }
.cleaner_h50 { clear: both; width:100%; height: 50px; }
.cleaner_h60 { clear: both; width:100%; height: 60px; }

.margin_r_20 { margin-right: 20px; }

.margin_r_40 { margin-right: 40px; }

.button_01 a {
    clear: both;
    display: block;
    width: 57px;
    height: 23px;
    padding: 2px 5px 0 22px;
    color: #333333;
    font-size: 13px;
    font-weight: bold;
    text-align: center;
    text-decoration: none;
    background: url(images/website_button_01.jpg) no-repeat;
}

.button_01 a:hover {
    color: #ffffff;
    background: url(images/website_button_01_hover.jpg) no-repeat;
}

h1 {
    margin: 0px;
    padding: 2px 0;
    font-size: 30px;
    font-weight: bold;
}

h2 {
    margin: 0 0 10px 0;
    padding: 2px 0;
    font-size: 24px;
    line-height: 24px;
    font-weight: bold;
    color: #ba0808;
}

h3 {
    margin: 0 0 5px 0;
    padding: 5px 0;
    font-size: 18px;
    font-weight: normal;
    color: #000000;
}

.image_wrapper {
    margin-top: 3px;
    margin-bottom: 5px;
    border: 5px solid #e8e8e8;
    padding: 1px;
    background: #fff;
}

.fl_image {
    float: left;
    margin-right: 30px 
}

.fr_image {
    float: right;
    margin-left: 20px 
}



#website_site_title_bar_wrapper {
    width: 100%;
    height: 100px;
    margin: 0 auto; 
    background:  url(images/website_site_title_bg_wrapper.jpg) repeat-x top;
}

#website_site_title_bar {
    width: 940px;
    height: 100px;
    margin: 0 auto;
    padding: 0 20px;
    background: url(images/website_site_title_bg.jpg) no-repeat top;
}

#site_title {
    float: left;
    width: 280px;
    margin: 0px;
    text-align: center;
    padding: 30px 0 0 0;
}

#site_title h1 a {
    margin: 0px;
    padding: 0px;
    font-size: 28px;
    color: #ba0808;
    font-weight: bold;
    text-decoration: none;
}

#site_title h1 a:hover {
    font-weight: bold;  
    text-decoration: none;
}

#site_title h1 a span {
    display: block;
    font-size: 13px;
    color: #204567;
    font-weight: bold;
    margin-top: 10px;
    margin-left: 5px;
    letter-spacing: 2px;
}


#website_site_title_bar #website_menu {
    float: right;
    width: 540px;
    height: 40px;
    margin: 0;
    padding: 10px 0 0 0;
    list-style: none;
}

#website_menu li {
    padding: 0px;
    margin: 0px;
    display: inline;
}

#website_menu li a {
    position: relative;
    float: left;
    display: block;
    width: 80px;
    height: 24px;
    padding: 6px 0 0 0;
    margin: 0 0 0 10px;
    font-size: 12px;
    text-align: center;
    font-weight: bold;
    text-decoration: none;
    color: #666666; 
    outline: none;
    background: url(images/website_menu_bg_right.jpg) center right no-repeat;
}

#website_menu li a span {
    position: absolute;
    top: 0;
    left: 0;
    width: 5px;
    height: 30px;
    background: url(images/website_menu_bg_left.jpg) no-repeat;
}

#website_menu li a:hover, #website_menu li .current {
    position: relative;
    color: #ffffff;
    background: url(images/website_menu_bg_right_hover.jpg) center right;
}

#website_menu li a:hover span, #website_menu li .current span {
    position: absolute;
    top: 0;
    left: 0;
    width: 5px;
    height: 30px;
    background: url(images/website_menu_bg_left_hover.jpg) no-repeat;
}


#website_site_title_bar #search_box {
    float: right;
    width: 280px;
    height: 30px;
    margin: 0 10px 0 0;
    padding: 10px 0 0 0;
}

#search_box form {
    float: left;
    width: 280px;
    height: 30px;
    margin: 0;
    padding: 0;
}

#searchfield {
    height: 20px;
    width: 195px;
    padding: 3px 5px 3px 5px;
    margin: 0px;
    color: #666666;
    font-size: 12px;
    font-variant: normal;
    line-height: normal;
    background: #ffffff;
    border: 1px solid #666666;  
}

#searchbutton {
    height: 30px;
    width: 70px;
    margin: 0px;
    padding: 3px 0 9px 0;
    cursor: pointer;
    font-size: 12px;
    font-weight: bold;
    text-align: center;
    vertical-align: bottom;
    white-space: pre;
    color: #2b3844;
}


#website_banner_wrapper_outter {
    clear: both;
    width: 100%;
    height: 370px;
    margin: 0 auto; 
    background: url(images/website_banner_bg_wrapper_outter.jpg) top repeat-x;
}

#website_banner_wrapper {
    width: 100%;
    height: 370px;
    margin: 0 auto; 
    background: url(images/website_banner_bg_wrapper.jpg) no-repeat center top;
}

#website_banner {
    width: 960px;
    height: 360px;
    padding: 0 10px;
    margin: 0 auto;
    background: url(images/website_banner_bg.jpg) no-repeat center top;
}

#website_banner_slider {
    clear: both;
    width: 960px;
    height: 290px;
    overflow: hidden;
    margin-bottom: 10px;
    padding: 70px 0 0 0;
    background: none;
}


#website_content {
    width: 940px;
    margin: 0 auto;
    padding: 30px 20px;
    background: url(images/website_content_bg_middle.png) repeat-y center;
}

#website_content #twitter_section {
    clear: both;
    margin-bottom: 20px;
}

#website_content #twitter_section p {
    float: left;
    width: 800px;
    font-size: 26px;
    line-height: 34px;
    color: #2a90c1;
}

#website_content #twitter_section .followme a {
    display: block;
    float: right;
    width: 128px;
    height: 128px;
    background: url(images/twitter.png) no-repeat;
}


#website_content p {
    margin-bottom: 10px;
}

.box {
    float: left;
    width: 270px;
    padding: 0 29px 0 0;
}

.box_border {
    border-right: 1px dotted #999;
}

.box h2 {
    color: #9b1919;
    font-size: 20px;
    margin-bottom: 20px;
}

.box .box_image_wrapper {
    float: left;
    text-align: center;
    margin-right: 20px;
}

.em_text {
    font-size: 14px;
    color: #000;
}

.section_w940 {
    clear: both;
    width: 940px;
}


.section_w450 {
    float: left;
    width: 450px;
}

.news_box{
    margin-bottom: 10px;
    border-bottom: 1px dashed #ccc; 
    color: #2f5e9e;
}

.news_box .post_info {
    color: #666;
}

.news_box .post_info a {
    color: #000000;
    font-weight: bold;
}

.news_box .post_info strong {
    color: #000000;
}

.news_box a {
    color: #2f5e9e;
}

.testimonial {
    font-style: italic;
}


#website_footer_wrapper {
    width: 100%;
    margin: 0 auto;
    background: #677684 url(images/website_footer_bg.jpg) repeat-x top;
}

#website_footer {
    width: 960px;
    margin: 0 auto;
    padding: 60px 10px 20px 10px;
    color: #ffffff;
}

#website_footer  a {
    color: #ffffff;
}

#website_footer .section_w240 {
    float: left;
    width: 200px;
    padding-right: 40px;
}

#website_footer .section_w240 h3 {
    font-size:14px;
    font-weight: bold;
    color: #ffffff;

}

#website_footer .footer_list {
    margin: 0px;
    padding: 0px;
    list-style: none;
}


#website_footer .footer_list li {
    margin: 0px;
    padding: 3px 0;
}

#website_footer .footer_list li a {
    color: #ffffff;
}

#website_footer .footer_list li a:hover {
    color: #FFFF00;
    text-decoration: none;
}

Upvotes: 0

Views: 100

Answers (2)

Chrisi
Chrisi

Reputation: 381

I'd write a javascript function, that hooks to the onMouseOver event of your HTML object an then displays your list as an menu. You can manipulate the width and height properties of a div, change its z-index or use the visibility property.

Upvotes: 0

SW4
SW4

Reputation: 71150

Fiddle

Sample HTML

<ul>
        <li>
            <a href="#">Item #1</a>
            <ul>
                <li><a href="">Sub-Item #1</a></li>
                <li><a href="">Sub-Item #2</a></li>
                <li><a href="">Sub-Item #3</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Item #2</a>
            <ul>
                <li><a href="">Sub-Item #4</a></li>
                <li><a href="">Sub-Item #5</a></li>
                <li><a href="">Sub-Item #6</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Item #3</a>
            <ul>
                <li><a href="">Sub-Item #7</a></li>
                <li><a href="">Sub-Item #8</a></li>
                <li><a href="">Sub-Item #9</a></li>
            </ul>
        </li>
    </ul>

Sample CSS

ul > li {
    display:block;
    float:left;
    margin-right:10px;
    position:relative; 
    background:Red;
    padding:0.5em; 
    line-height:1em
}
ul ul {
    display:none;
    width:150px;
    position:absolute;
    top:2em;
    left:0;
}
ul ul > li {
    float:none;
}
ul > li:hover > ul,
ul > a:hover + ul {
    display:block;
}

Upvotes: 2

Related Questions