如何添加下拉菜单?

时间:2017-07-12 05:59:09

标签: html css

我有水平菜单栏,我尝试为其中一个项目添加子菜单,但我无法添加它,它附加到我的主菜单,请有人帮我到我失踪的地方 谢谢 HTML



<div id="talltabs-maroon">
	<ul>
		<li class="first"><a href="#">Home <span>Page</span></a></li>
		<li class="active"><a href="#"><span>About us</span></a></li>
		<li class="dropdown"><a class="dropbtn" href="#"> <span> Report				</span></a>
        <ul  class="dropdown-content" style="left:0">
            <li>
                <a  href="">
                <p>Valve Report</p>
                </a>
            </li>
            <li>
                 <a href="">
                 <p>Cylinder Report</p>
                 </a>
            </li>
        </ul>
        </li>
		<li class="last"><a href="#">Contact <span>Us</span></a></li>
	</ul>
</div>
&#13;
&#13;
&#13;

主菜单的CSS

&#13;
&#13;
#talltabs-maroon {
    clear: left;
    float: left;
    padding: 0;
    border-top: 3px solid #CD324F;
    width: 100%;
    overflow: hidden;
    font-family: Georgia, serif;
    height: 90px;
    position: inherit;
}

    #talltabs-maroon ul {
        float: left;
        margin: 0;
        padding: 0;
        list-style: none;
        position: relative;
        left: 50%;
        text-align: center;
    }

        #talltabs-maroon ul li {
            display: block;
            float: left;
            list-style: none;
            margin: 0;
            padding: 0;
            position: relative;
            right: 50%;
        }

            #talltabs-maroon ul li a {
                display: block;
                float: left;
                margin: 0 3px 0 0;
                padding: 0px 10px 6px 10px;
                background: #CD324F;
                text-decoration: none;
                color: #fff;
            }

                #talltabs-maroon ul li a p:hover {
                    color: aqua;
                }

                #talltabs-maroon ul li a:hover {
                    padding: 20px 10px 6px 10px;
                    color: black
                }

            #talltabs-maroon ul li.active a,
            #talltabs-maroon ul li.active a:hover {
                padding: 25px 10px 6px 10px;
                border-width: 5px;
                border-color: aqua;
                color: aqua;
            }
&#13;
&#13;
&#13;

我试过的下拉菜单的CSS。

&#13;
&#13;
.dropbtn {
    list-style-type: none;
    color: white;
    padding: 14px;
    font-size: 14px;
    border: none;
    cursor: pointer;
}

.dropdown {
    position: relative;
    display: block;
}

.dropdown-content {
    list-style-type: none;
    display: none;
    position: absolute;
    right: 0;
    /*background-color: black;*/
    background-image: url('../../Images/black-olive.jpg'); /*dropdowm popup*/
    min-width: 160px;
    box-shadow: 0px 8px 16px 5px rgba(0,0,0,0.2);
    z-index: 1;
    padding-right: 2px;
    margin-right: 2px;
}

    .dropdown-content a {
        color: white;
        padding: 10px 14px;
        text-decoration: none;
        display: block;
    }

        .dropdown-content a:hover {
            /*background-color: gray;*/
            color: black;
        }

.dropdown:hover .dropdown-content {
    display: block;
}

.dropdown:hover .dropbtn {
    /*background-color: #3e8e41;*/
}
&#13;
&#13;
&#13;

请求帮助我。 谢谢 天衣。

3 个答案:

答案 0 :(得分:1)

以下是我对同一个例子的回答,我改变了完整的CSS,

    body {
        font-family: 'Lucida Grande', 'Helvetica Neue', Helvetica, Arial, sans-serif;
    }

    ul {
        text-align: left;
        display: inline;
        margin: 0;
        padding: 15px 4px 17px 0;
        list-style: none;
    }

        ul li {
            display: inline-block;
            margin-right: -1px;
            position: relative;
            padding: 15px 20px;
            background: #CD324F;
            cursor: pointer;
            color: black;
            height: 40px;
            width: auto;
            text-align:center;
        }
        ul li a{
            color:black;
        }

            ul li:hover {
                background: #CD324F;
                color: #fff;
                height: 45px;
            }

            ul li a:hover {
                color: #fff;
            }

            ul li ul {
                padding: 0;
                position: absolute;
                top: 68px;
                left: 0;
                width: 160px;
                -webkit-box-shadow: none;
                -moz-box-shadow: none;
                box-shadow: none;
                display: none;
                opacity: 0;
                visibility: hidden;
            }

                ul li ul li {
                    background: #ce5068;
                    display: block;
                    color: #CD324F;
                    height: 35px;
                }

                    ul li ul li:hover {
                        background: #CD324F;
                        height: 35px;
                    }

            ul li:hover ul {
                display: block;
                opacity: 1;
                visibility: visible;
            }
<div style="height: 77px; width:100%; margin-top:65px;text-align:center; border-top:solid; border-top-color:#CD324F">
 <ul><li>Home</li>
  <li>About</li>
  <li>
    Portfolio
    <ul>
      <li>Web Design</li>
      <li>Web Development</li>
      <li>Illustrations</li>
    </ul>
  </li>
  <li>Blog</li>
  <li>Contact</li>
</ul>
</div>

结果:在悬停式投资组合中,将出现下拉列表

result screenshot

JSFiddle上的工作示例。

答案 1 :(得分:0)

我真的建议你看一下bootstrap的下拉菜单。它易于使用,大多数事情已经为您完成。祝好运 这是链接:https://www.w3schools.com/bootstrap/bootstrap_dropdowns.asp

答案 2 :(得分:0)

你的代码有点混乱,我为你创建了一个简单的演示程序。

这是我的HTML代码

body {
	background: #212121;
	font-size:22px;
	line-height: 32px;
	color: #ffffff;
	word-wrap:break-word !important;
	font-family: 'Open Sans', sans-serif;
	}

h1 {
	font-size: 60px;
	text-align: center;
	color: #FFF;
}	

h3 {
	font-size: 30px;
	text-align: center;
	color: #FFF;
}

h3 a {
	color: #FFF;
}

a {
	color: #FFF;
}

h1 {
	margin-top: 100px;
	text-align:center;
	font-size:60px;
	font-family: 'Bree Serif', 'serif';
	}

#container {
	margin: 0 auto;
}

p {
	text-align: center;
}

nav {
	margin: 50px 0;
	background-color: #E64A19;
}

nav ul {
	padding: 0;
  margin: 0;
	list-style: none;
	position: relative;
	}
	
nav ul li {
	display:inline-block;
	background-color: #E64A19;
	}

nav a {
	display:block;
	padding:0 10px;	
	color:#FFF;
	font-size:20px;
	line-height: 60px;
	text-decoration:none;
}

nav a:hover { 
	background-color: #000000; 
}

/* Hide Dropdowns by Default */
nav ul ul {
	display: none;
	position: absolute; 
	top: 60px; /* the height of the main nav */
}
	
/* Display Dropdowns on Hover */
nav ul li:hover > ul {
	display:inherit;
}
	
/* Fisrt Tier Dropdown */
nav ul ul li {
	width:170px;
	float:none;
	display:list-item;
	position: relative;
}
<div id="container">
    <nav>
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">WordPress</a>
            <!-- First Tier Drop Down -->
            <ul>
                <li><a href="#">Themes</a></li>
                <li><a href="#">Plugins</a></li>
                <li><a href="#">Tutorials</a></li>
            </ul>        
            </li>
            <li><a href="#">Web Design</a>
            <!-- First Tier Drop Down -->
            <ul>
                <li><a href="#">Resources</a></li>
                <li><a href="#">Links</a></li>
                <li><a href="#">Tutorials</a>
			</ul>
		</nav>
	</div>