固定导航与下拉菜单

时间:2016-07-04 14:19:12

标签: html css navigation

所以我一直在设计css中的导航。我希望它是一个带有下拉菜单的固定导航。我可以获得顶部导航或下拉列表,但我不能同时做到这两点。有没有人有任何建议?

CSS:

body {margin: 0; font-family: 'SF Comic Script';}

.topnav-brand {
 float: left;
 margin-left: 30px;
 padding: 15px 15px;
 font-size: 24px;
 line-height: 20px;
 height: 24px;
 text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25);
}

.topnav-brand:hover,
.topnav-brand:focus {
   text-decoration: none;
   color: #5e5e5e;
   background-color: transparent;
}

ul.topnav {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: #7a003d;
background-image: -webkit-linear-gradient(top, #ad0057 0%, #7a003d 100%);
background-image: -o-linear-gradient(top, #ad0057 0%, #7a003d 100%);
background-image: -webkit-gradient(linear, left top, left bottom, from(#ad0057), to(#7a003d));
background-image: linear-gradient(to bottom, #ad0057 0%, #7a003d 100%);
background-repeat: repeat-x;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffad0057', endColorstr='#ff7a003d', GradientType=0);
filter: progid:DXImageTransform.Microsoft.gradient(enabled = false);
border-radius: 2px;
position: fixed;
top: 0;
width: 100%;
}

ul.topnav li {
    float: left;
}

ul.topnav li a, .dropbtn {
    display: inline-block;
    color: white;
    text-align: center;
    padding: 14px 16px;
    text-decoration: none;
}

ul.topnav li a:hover, .dropdown:hover .dropbtn {
    background-color: #470024;

}

li.dropdown {
    display: inline-block;
}

.dropdown-content {
    display: none;
    position: absolute;
    min-width: 160px;
    background-color: #470024;
    top: 100%;
}

.dropdown-content a {
    color: white;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.dropdown-content a:hover {color: #7a003d; background-color: #ffffff;
    background-image: -webkit-linear-gradient(top, #f2f2f2 0%, #ffffff 100%);
    background-image: -o-linear-gradient(top, #f2f2f2 0%, #ffffff 100%);
    background-image: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#ffffff));
    background-image: linear-gradient(to bottom, #f2f2f2 0%, #ffffff 100%);
    background-repeat: repeat-x;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#fff2f2f2', endColorstr='#ffffffff', GradientType=0);
    filter: progid:DXImageTransform.Microsoft.gradient(enabled = false);
    border-radius: 2px;}

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

HTML(正确链接)

<ul class="topnav">
  <a class="topnav-brand">Computer Services Unit</a>
  <li><a href="#home">Infrastructure</a></li>
  <li class="dropdown"><a href="#news" class="dropbtn">Classroom &amp; Collaborative Services</a>
   <div class="dropdown-content">
    <a href="#">Mobile</a>
    <a href="#">Network</a>
    <a href="#">Link 3</a>
  </div>
  </li>
  <li><a href="#contact">Tech Shop</a></li>
  <li><a href="#about">Application &amp; Web Development</a></li>
  <li><a href="#about">Business Analysis &amp; Prohect Management</a></li>
</ul>

1 个答案:

答案 0 :(得分:0)

请尝试此代码,它会对您有所帮助。

body {
	padding-top:50px;
	margin:0;
}
nav {
	position:fixed;
	top:0;
	left:0;
	width:100%;
	z-index:999;
}
nav ul {
	list-style-type: none;
	margin: 0;
	padding: 0;
	background-color: Crimson;
	display: inline-block;
	width: 100%;
}
nav li {
	float: right;
	position: relative;
}
nav li a {
	display: block;
	color: white;
	text-align: center;
	padding: 14px 16px;
	text-decoration: none;
}
nav li a:hover {
	background-color: #111;
}
nav ul ul {
	position: absolute;
	display: none;
	left: 0;
	z-index: 999;
}
nav ul ul li {
	float: none;
	width:100%;
}
nav ul ul ul {
	left: 100%;
	top: 0;
	display: float;
}
nav li:hover > ul {
	display: block;
}
<nav>
  <ul class="topnav">
    <li style="float:left"> <a class="topnav-brand">Computer Services Unit</a> </li>
    <li><a href="#home">Infrastructure</a></li>
    <li><a href="#news" class="dropbtn">Classroom &amp; Collaborative Services</a>
      <ul>
        <li> <a href="#">Mobile</a></li>
        <li> <a href="#">Network</a></li>
        <li><a href="#">Link 3</a></li>
      </ul>
    </li>
    <li><a href="#contact">Tech Shop</a></li>
    <li><a href="#about">Application &amp; Web Development</a></li>
    <li><a href="#about">Business Analysis &amp; Prohect Management</a></li>
  </ul>
</nav>