响应式下拉菜单?

时间:2019-04-11 08:45:40

标签: html css

我需要一个响应式下拉菜单,我已经看过教程等。但是所有内容都涉及我现在无法使用的javascript。菜单最好使用汉堡包。我一直在尝试并设法在没有汉堡的情况下做到这一点,但是它看上去很可怕,内容也遍及了它。

我正在附加我的代码,但没有响应式菜单。仅使用CSS,任何帮助将不胜感激。

<!DOCTYPE html>
<html lang = "en">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width",initial-scale=1.0>

          <title>MardiDrama Club</title>
          <link rel="stylesheet" type="text/css" href="style.css">
    </head>

    <body>
        <div class = "nav">
                <ul>
                    <li><a href="events.html">Events</a></li>
                    <li><a href="classes.html">Classes</a>
                <ul>
                    <div class="links"><li><a href="musical_theatre.html">Musical Theatre</a></li> 
                    <li><a href="after_school.html">After School</a></li>   
                    <li><a href="holiday_programmes.html">Holiday Programmes</a>
                </div></ul>

                    <li><a href="where.html">Where</a></li>
                    <li><a href="about.html">About</a></li>
                    <li><a href="register.html">Register</a></li>
                    <li><a href="blog.html">Blog</a></li>                    
                </li>
                    <li><a href="index.html">HomePage</a></li>
                </li>

                </ul>

                <div class="logo"><a href="index.html"><img src="assets/images/maridrama_logo.png" class="centre"></a></div>

            </div>
@media only screen and (max-width: 600px) {
  body article{
    background-color: white;
  }

  html{
    height: 100%;
  }
}
body{
    background-color: white;
    margin-right: auto;
    margin-left: auto;
    margin-top: 0;
    margin-bottom: 0;
    padding: 0;
    margin: 0 auto;
    font-family: "Century Gothic";
}


.nav{
    width: 100%;
    height: 70vh;
    background:linear-gradient(rgba(0,0,0,0.5),rgba(0,0,0,0.5)), url(assets/images/kilyan-sockalingum-478724-unsplash.jpg);
    background-size: cover;
    background-position: center;
}

ul{

    list-style-type: none;
    margin: 0 auto;
}

ul li{
    float: right;
    width: 160px;
    font-size: 15px;
    line-height: 80px;
    text-align: center;
}
.links{
    float: right;
    width: 160px;
    font-size: 15px;
    line-height: 80px;
    text-align: center;

}

ul li a{
    text-decoration: none;
    color: #fff;
    display: block;
    width: 160px;
}

ul li a:hover{
    background-color: #96C7ED;
    transition: 1s all ease;
}

ul li ul li{

    display: none;
    width: 100%;
    font-size: 13px;
    transition:  1s all;
}

ul li:hover ul li{
    display: block;
}

div.logo {
    position: absolute;
    top: 0;
    left: 0;
    background : url(assets/images/LogoMakr_59ebir.png) no-repeat 0 0;

}

1 个答案:

答案 0 :(得分:0)

我们为此提供了手动选项。就像我在评论中提到的那样,Bootstrap默认情况下为您提供此功能。同样,任何其他框架(如Zurb Foundation或UI套件)也为您提供响应式导航功能。对于纯HTML / CSS,您可以将此模板用于响应式导航栏:

body {
  margin: 25px;
  -webkit-animation: bugfix infinite 1s;
  /* needed for checkbox hack */
  background: #021320;
}

@-webkit-keyframes bugfix {
  from {
    padding: 0;
  }
  to {
    padding: 0;
  }
}
/* needed for checkbox hack */
h1, p {
  color: white;
}

#nav {
  position: relative;
}
#nav ul {
  display: none;
  width: 100%;
  list-style: none;
  margin: 0px;
  padding: 0px;
}
#nav ul li a {
  display: block;
  padding: 1em;
  background: #0F9962;
  color: white;
  text-decoration: none;
  border-right: 1px solid #0a6b44;
}
#nav ul li a:hover {
  background: #0d8253;
}
#nav ul li:last-of-type a {
  border-right: 0px;
}
#nav ul li ul li a {
  padding-left: 1.5em;
}
#nav ul li ul li ul li a {
  padding-left: 3.125em;
}
#nav input.trigger {
  position: absolute;
  top: -9999px;
  left: -9999px;
}
#nav input.trigger:checked ~ ul, #nav input.trigger:checked ~ ul li ul {
  display: block !important;
}
@media (min-width: 48em) {
  #nav input.trigger:checked ~ ul, #nav input.trigger:checked ~ ul li ul {
    /* older flexbox */
    display: -webkit-box;
    display: -moz-box;
    display: box;
    -webkit-box-orient: horizontal;
    -moz-box-orient: horizontal;
    box-orient: horizontal;
    /* newer flexbox */
    display: flex;
    flex-direction: row;
  }
}
#nav label {
  position: relative;
  display: block;
  min-height: 2em;
  padding: .45em;
  font-size: 1.1em;
  margin: 0;
  cursor: pointer;
  background: #005292;
  line-height: 2em;
  color: #bfe1fb;
}
#nav label:after {
  position: absolute;
  right: 1em;
  top: .2em;
  content: "\2261";
  font-size: 1.8em;
  color: white;
}
@media (min-width: 48em) {
  #nav ul {
    /* older flexbox */
    display: -ms-flexbox;
    flex-direction: -ms-row;
    display: -webkit-box;
    display: -moz-box;
    display: box;
    -webkit-box-orient: horizontal;
    -moz-box-orient: horizontal;
    box-orient: horizontal;
    /* newer flexbox */
    display: flex;
    flex-direction: row;
  }
  #nav ul li {
    position: relative;
    text-align: center;
    /* older flexbox */
    -ms-flex: 1;
    -webkit-box-flex: 1;
    -moz-box-flex: 1;
    box-flex: 1;
    /* newer flexbox */
    flex: 1;
  }
  #nav ul li ul {
    display: none !important;
    position: absolute;
    top: 3.0625em;
    left: 0;
    display: block;
    width: 12.5em;
    z-index: 200;
  }
  #nav ul li ul li {
    text-align: left;
  }
  #nav ul li ul li ul {
    z-index: 300;
    top: 0px;
    left: 12.4em;
  }
  #nav ul li ul li ul li a {
    padding-left: 30px !important;
  }
  #nav ul li:hover > ul {
    display: block !important;
  }
  #nav label {
    display: none;
  }
}
<nav role="navigation" id="nav">
              <input class="trigger" type="checkbox" id="mainNavButton">
              <label for="mainNavButton" onclick>Menu</label>
              <ul>
                <li><a href="#">Home</a></li>
                <li><a href="#">About Us</a>
                    <ul>
                      <li><a href="#">Sub Nav Item</a></li>
                      <li><a href="#">Sub Nav Item</a>
                        <ul>
                          <li><a href="#">Sub Sub Nav Item</a></li>
                          <li><a href="#">Sub Sub Nav Item</a></li>
                          <li><a href="#">Sub Sub Nav Item</a></li>
                          <li><a href="#">Sub Sub Nav Item</a></li>
                        </ul>
                      </li>
                      <li><a href="#">Sub Nav Item</a></li>
                      <li><a href="#">Sub Nav Item</a></li>
                    </ul>
                </li>
                <li><a href="#">Products</a></li>
                <li><a href="#">Blog</a></li>
                <li><a href="#">Specials</a></li>
                <li><a href="#">Contact Us</a></li>
              </ul>
              </ul> 
            </nav>

此链接中还提供了其他方法,这也是我的答案代码的来源:https://1stwebdesigner.com/code-snippets-responsive-navigation-menu/