在CSS中制作菜单,如图所示

时间:2017-02-06 22:49:34

标签: javascript html css html5 css3

我对如何重现以下内容有疑问: enter image description here

我想知道如何让这个菜单符合上面的图片。我不想要任何准备好的东西,而是想要获得预期结果的方法。

到目前为止,我有以下代码:

body{
  background: white;
  margin: 0;
  padding: 0;
}
nav {
  width: 100%;
  background: #000;
  border-bottom: 5px solid white;
}
nav:after{
  content: '';
  border-bottom: 10px solid black;
  width: 100%;
  position: fixed;
  margin-top: 5px;
}
nav li {
  display: inline-block; 
  list-style: none;
}
nav li a {
  color: #fff;
  display: inline-block; 
  font-weight: bold; 
  padding: 20px 15px 15px 15px; 
  text-decoration: none;
}
nav li a:hover {
  background: red;
  color: #fff;
}
nav li a:hover:after {
  content: '';
  border-bottom: 10px solid yellow;
  position: fixed;
  width: auto;
  margin-top: 54px;
  left: 0;
  right: 0;
  z-index: 1;
}
<nav>
 <ul>
  <li><a href="">Menu 1</a></li>
  <li><a href="">Menu 2</a></li>
  <li><a href="">Menu 3</a></li>
 </ul>
</nav>

我希望有人可以帮助我。提前谢谢!

3 个答案:

答案 0 :(得分:2)

你可以试试这个解决方案应该有帮助

body {
  background: white;
  margin: 0;
  padding: 0;
}

nav {
  width: 100%;
  background: #000;
  border-bottom: 5px solid white;
  position: relative;
}

nav:after {
  content: '';
  height: 8px;
  width: 100%;
  background: inherit;
  position: absolute;
  bottom: -15px;
  left: 0px;
  z-index: 1;
}

nav ul {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  max-width: 100%;
  margin: auto;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  padding-bottom: .6em;
}

nav li {
  display: inline-block;
  list-style: none;
  position: relative;
  padding: 0 .5em;
}
nav li:last-child a:before {
  display: none;
}

nav li a {
  color: #fff;
  display: inline-block;
  padding: 1.6em 0.6em 0.7em 0.6em;
  text-decoration: none;
  position: relative;
  font-size: 18px;
  line-height: 1;
}
nav li a:before {
  content: "|";
  display: block;
  position: absolute;
  right: -10px;
  top: 1.6em;
  -webkit-transform: translateY(-4%);
          transform: translateY(-4%);
  line-height: inherit;
  font-size: inherit;
  color: #fff;
}
nav li a:after {
  display: none;
  content: "";
  position: absolute;
  bottom: -25px;
  left: 0px;
  width: 100%;
  height: 8px;
  background: #ffaf1a;
  z-index: 2;
}

nav li a:hover {
  background: red;
  color: #fff;
}
nav li a:hover:after {
  display: block;
}
<nav>
 <ul>
  <li><a href="">Menu 1</a></li>
  <li><a href="">Menu 23</a></li>
  <li><a href="">Menu 345</a></li>
  <li><a href="">Menu 44567</a></li>
  <li><a href="">Menu 567889</a></li>
 </ul>
</nav>

答案 1 :(得分:0)

使用以下代码将元素粘贴到导航栏底部:https://jsfiddle.net/d379Lagm/

body{
  background: white;
  margin: 0;
  padding: 0;
}
nav {
  width: 100%;
  background: #000;
  border-bottom: 5px solid white;
  outline: 5px solid #000;

}


nav ul {
  list-style: none;
  padding: 0;
  margin: 0;

}

nav li {
  display: inline-block; 
  list-style: none;
}
nav li a {
  color: #fff;
  display: inline-block; 
  font-weight: bold; 
  padding: 20px 15px 15px 15px; 
  text-decoration: none;
  position: relative;
  border-bottom: 5px solid transparent;
}
nav li a:hover {
  background: red;
  color: #fff;

}
nav li a:after {
  position: absolute;
  left: 0;
  right: 0;
  background: transparent;
  height: 5px;
  content: "";
  transform: translateY(950%);
}
nav li a:hover:after {
  background: yellow;

}

答案 2 :(得分:0)

这是:https://jsfiddle.net/97cyvmcy/

只需使用两个线性渐变即可达到预期效果:

红色和黄色条纹:

li a:hover {
  background: linear-gradient(to bottom, red 0%, red 90%, yellow 90%, yellow 100%);
}

另一个底部有黑白条纹:

nav:after {
  content:' ';
  width: 100%;
  height: 15px;
  position: absolute;
  bottom: 10px;
  background:linear-gradient(to bottom, black 0%, black 50%, white 50%, white 100%);
}