水平菜单到垂直菜单,而无需添加任何其他HTML属性

时间:2019-03-14 08:04:47

标签: html css navbar

必须更改CSS的哪一部分才能将此“水平CSS”菜单转换为“左侧”侧边栏菜单?是否可以仅在CSS中完成而无需向HTML添加其他属性?可以在Pure CSS和Js中完成吗?请没有框架。我对学习如何执行此操作感兴趣,因此可以更好地了解将来如何以及为什么要做类似的元素。

body {
  margin: 0;
  padding: 0;
  background: #eee;
}


/* Navigation Bar */

.nav {
  background: linear-gradient(to bottom, #57b759, #007a00);
  /* Green gradient - light to dark. */
  font-size: 14px;
  font-family: Arial, sans-serif;
  text-align: left;
}

.nav>ul a {
  color: #fff;
}

.nav>ul,
.nav>ul ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav>ul li {
  position: relative;
}

.nav>ul a {
  padding: 5px 10px;
  margin: 5px 10px;
  display: block;
  text-decoration: none;
  border-radius: .2em;
}

.nav>ul a:hover {
  opacity: 1.0;
  background-color: rgba(0, 0, 0, 0.3);
}


/* Active Menu Item */

.nav .active {
  opacity: 1.0;
  background-color: rgba(255, 255, 255, 0.5);
  color: #000;
  border-radius: .2em;
}


/* Navigation Bar - Level 1 Drop Down Menu */

.nav>ul>li {
  display: inline-block;
  vertical-align: top;
  margin-left: -4px;
}

.nav>ul>li:first-child {
  margin-left: 0;
}

.nav>ul>li>a {}

.nav>ul>li>a:hover {}


/* Navigation Bar - Level 2 */

.nav>ul>li>ul {
  text-align: left;
  width: auto;
  /* Change auto value with 200px if you want a bigger menu */
  display: none;
  background: #007a00;
  /* Dark green. */
  position: absolute;
  top: 100%;
  left: 0;
  padding-bottom: 5px;
  min-width: 150px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li:hover>ul {
  display: block;
}

.nav>ul ul li a {}

.nav>ul ul li a:hover {}


/* Navigation Bar - Level 3 */

.nav>ul>li>ul>li>ul {
  text-align: left;
  display: none;
  background: #57b759;
  /* Light green. */
  position: absolute;
  left: 100%;
  top: 0;
  padding-bottom: 5px;
  min-width: 150px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li>ul>li:hover>ul {
  display: block;
}

.nav>ul ul ul li {}

.nav>ul ul ul li a {}

.nav>ul ul ul li a:hover {}


/* Navigation Bar - Level 4 */

.nav>ul>li>ul>li>ul>li>ul {
  text-align: left;
  display: none;
  background: #57b759;
  /* Light green. */
  position: absolute;
  left: 100%;
  top: 0;
  padding-bottom: 5px;
  min-width: 250px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li>ul>li>ul>li:hover>ul {
  display: block;
}

.nav>ul ul ul ul li {}

.nav>ul ul ul ul li a {}

.nav>ul ul ul ul li a:hover {}
<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Nav</title>
</head>

<body>
  <nav class="nav">
    <ul>
      <li><a href="#">Section 1</a></li>
      <li><a href="#">Section 2 ▾</a>
        <ul>
          <li><a href="#">Section 2.1</a></li>
          <li><a href="#">Section 2.2</a></li>
          <li><a href="#">Section 2.3</a></li>
        </ul>
      </li>
      <li><a href="#" class="active">Section 3 ▾</a>
        <ul>
          <li><a href="#" class="active">Section 3.1</a></li>
          <li><a href="#">Section 3.2 ▸</a>
            <ul>
              <li><a href="#">Section 3.2.1</a></li>
              <li><a href="#">Section 3.2.2</a></li>
              <li><a href="#">Section 3.2.3 ▸</a>
                <ul>
                  <li><a href="#">Section 3.2.3.1</a></li>
                  <li><a href="#">Section 3.2.3.2</a></li>
                </ul>
              </li>
            </ul>
          </li>
          <li><a href="#">Section 3.3</a></li>
        </ul>
      </li>
    </ul>
    </div>

</body>

</html>

2 个答案:

答案 0 :(得分:2)

在媒体查询中添加767px的css,因此当屏幕缩小到767px以下时,导航宽度变为200px position:fixed {{1} }

height:100%

@media screen and (max-width: 767px) {
  .nav {
    text-align: right;
    width: 200px;
    position: fixed;
    height: 100%;
  }
  .nav  ul  li {
    display: block;
  }
  .nav > ul > li > ul {
    top:0px;
    left:100%;
  }
}
body {
  margin: 0;
  padding: 0;
  background: #eee;
}


/* Navigation Bar */

.nav {
  background: linear-gradient(to bottom, #57b759, #007a00);
  /* Green gradient - light to dark. */
  font-size: 14px;
  font-family: Arial, sans-serif;
  text-align: left;
}

.nav>ul a {
  color: #fff;
}

.nav>ul,
.nav>ul ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav>ul li {
  position: relative;
}

.nav>ul a {
  padding: 5px 10px;
  margin: 5px 10px;
  display: block;
  text-decoration: none;
  border-radius: .2em;
}

.nav>ul a:hover {
  opacity: 1.0;
  background-color: rgba(0, 0, 0, 0.3);
}


/* Active Menu Item */

.nav .active {
  opacity: 1.0;
  background-color: rgba(255, 255, 255, 0.5);
  color: #000;
  border-radius: .2em;
}


/* Navigation Bar - Level 1 Drop Down Menu */

.nav>ul>li {
  display: inline-block;
  vertical-align: top;
  margin-left: -4px;
}

.nav>ul>li:first-child {
  margin-left: 0;
}

.nav>ul>li>a {}

.nav>ul>li>a:hover {}


/* Navigation Bar - Level 2 */

.nav>ul>li>ul {
  text-align: left;
  width: auto;
  /* Change auto value with 200px if you want a bigger menu */
  display: none;
  background: #007a00;
  /* Dark green. */
  position: absolute;
  top: 100%;
  left: 0;
  padding-bottom: 5px;
  min-width: 150px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li:hover>ul {
  display: block;
}

.nav>ul ul li a {}

.nav>ul ul li a:hover {}


/* Navigation Bar - Level 3 */

.nav>ul>li>ul>li>ul {
  text-align: left;
  display: none;
  background: #57b759;
  /* Light green. */
  position: absolute;
  left: 100%;
  top: 0;
  padding-bottom: 5px;
  min-width: 150px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li>ul>li:hover>ul {
  display: block;
}

.nav>ul ul ul li {}

.nav>ul ul ul li a {}

.nav>ul ul ul li a:hover {}


/* Navigation Bar - Level 4 */

.nav>ul>li>ul>li>ul>li>ul {
  text-align: left;
  display: none;
  background: #57b759;
  /* Light green. */
  position: absolute;
  left: 100%;
  top: 0;
  padding-bottom: 5px;
  min-width: 250px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li>ul>li>ul>li:hover>ul {
  display: block;
}

.nav>ul ul ul ul li {}

.nav>ul ul ul ul li a {}

.nav>ul ul ul ul li a:hover {}

@media screen and (max-width: 767px) {
  .nav {
    text-align: right;
    width: 200px;
    position: fixed;
    height: 100%;
  }
  .nav  ul  li {
    display: block;
  }
  .nav > ul > li > ul {
    top:0px;
    left:100%;
  }
}

右侧边栏

<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  <title>Nav</title>
</head>

<body>
  <nav class="nav">
    <ul>
      <li><a href="#">Section 1</a></li>
      <li><a href="#">Section 2 ▾</a>
        <ul>
          <li><a href="#">Section 2.1</a></li>
          <li><a href="#">Section 2.2</a></li>
          <li><a href="#">Section 2.3</a></li>
        </ul>
      </li>
      <li><a href="#" class="active">Section 3 ▾</a>
        <ul>
          <li><a href="#" class="active">Section 3.1</a></li>
          <li><a href="#">Section 3.2 ▸</a>
            <ul>
              <li><a href="#">Section 3.2.1</a></li>
              <li><a href="#">Section 3.2.2</a></li>
              <li><a href="#">Section 3.2.3 ▸</a>
                <ul>
                  <li><a href="#">Section 3.2.3.1</a></li>
                  <li><a href="#">Section 3.2.3.2</a></li>
                </ul>
              </li>
            </ul>
          </li>
          <li><a href="#">Section 3.3</a></li>
        </ul>
      </li>
    </ul>
    </div>

</body>

</html>
body {
  margin: 0;
  padding: 0;
  background: #eee;
}


/* Navigation Bar */

.nav {
  background: linear-gradient(to bottom, #57b759, #007a00);
  /* Green gradient - light to dark. */
  font-size: 14px;
  font-family: Arial, sans-serif;
  text-align: left;
}

.nav>ul a {
  color: #fff;
}

.nav>ul,
.nav>ul ul {
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav>ul li {
  position: relative;
}

.nav>ul a {
  padding: 5px 10px;
  margin: 5px 10px;
  display: block;
  text-decoration: none;
  border-radius: .2em;
}

.nav>ul a:hover {
  opacity: 1.0;
  background-color: rgba(0, 0, 0, 0.3);
}


/* Active Menu Item */

.nav .active {
  opacity: 1.0;
  background-color: rgba(255, 255, 255, 0.5);
  color: #000;
  border-radius: .2em;
}


/* Navigation Bar - Level 1 Drop Down Menu */

.nav>ul>li {
  display: inline-block;
  vertical-align: top;
  margin-left: -4px;
}

.nav>ul>li:first-child {
  margin-left: 0;
}

.nav>ul>li>a {}

.nav>ul>li>a:hover {}


/* Navigation Bar - Level 2 */

.nav>ul>li>ul {
  text-align: left;
  width: auto;
  /* Change auto value with 200px if you want a bigger menu */
  display: none;
  background: #007a00;
  /* Dark green. */
  position: absolute;
  top: 100%;
  left: 0;
  padding-bottom: 5px;
  min-width: 150px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li:hover>ul {
  display: block;
}

.nav>ul ul li a {}

.nav>ul ul li a:hover {}


/* Navigation Bar - Level 3 */

.nav>ul>li>ul>li>ul {
  text-align: left;
  display: none;
  background: #57b759;
  /* Light green. */
  position: absolute;
  left: 100%;
  top: 0;
  padding-bottom: 5px;
  min-width: 150px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li>ul>li:hover>ul {
  display: block;
}

.nav>ul ul ul li {}

.nav>ul ul ul li a {}

.nav>ul ul ul li a:hover {}


/* Navigation Bar - Level 4 */

.nav>ul>li>ul>li>ul>li>ul {
  text-align: left;
  display: none;
  background: #57b759;
  /* Light green. */
  position: absolute;
  left: 100%;
  top: 0;
  padding-bottom: 5px;
  min-width: 250px;
  border-radius: 0 0 .5em .5em;
  z-index: 9999999;
}

.nav>ul>li>ul>li>ul>li:hover>ul {
  display: block;
}

.nav>ul ul ul ul li {}

.nav>ul ul ul ul li a {}

.nav>ul ul ul ul li a:hover {}

@media screen and (max-width: 767px) {
  .nav {
    text-align: left;
    width: 200px;
    position: fixed;
    height: 100%;
    right:0px;
  }
  .nav  ul  li {
    display: block;
  }
  .nav > ul > li ul {
    top:0px!important;
    right:100%;
    left:auto!important;
  }
}

答案 1 :(得分:1)

是的,在css中只有很小的更改,您将得到您想要的 只需更新CSS

    /*adapted from https://www.andrewnoske.com/wiki/CSS_-_Multilevel_Navigation_Bar */
    body { margin: 0; padding: 0;  background: #eee; }
    /* Navigation Bar */
    .nav {
      background: linear-gradient(to bottom, #57b759, #007a00);  /* Green gradient - light to dark. */
      font-size: 14px;
      font-family: Arial, sans-serif;
      text-align: left; 
    }
    .nav > ul a {
      color: #fff; 
    }
    .nav > ul,
    .nav > ul ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    .nav > ul li {
      position: relative;
    }
    .nav > ul a {
      padding:10px;
      display: block;
      text-decoration: none;
      border-radius: .2em; 
    }
    .nav > ul a:hover {
      opacity: 1.0;
      background-color: rgba(0, 0, 0, 0.3); 
    }

    /* Active Menu Item */
    .nav .active {
      opacity: 1.0;
      background-color: rgba(255, 255, 255, 0.5); 
      color: #000; 
      border-radius: .2em;
    }

    /* Navigation Bar - Level 1 Drop Down Menu */
    .nav > ul > li {
      display: inline-block;
      vertical-align: top;
      margin-left: -4px;
    }
    .nav > ul > li:first-child {
      margin-left: 0;
    }
    .nav > ul > li > a {}
    .nav > ul > li > a:hover {}

    /* Navigation Bar - Level 2 */
    .nav > ul > li > ul {
      text-align: left;
      width: auto;  /* Change auto value with 200px if you want a bigger menu */
      display: none;
      background: #007a00;  /* Dark green. */
      position: absolute;
      top: 0;
      left: 0;
      padding-bottom: 5px;
      min-width: 150px;
      border-radius: 0 0 .5em .5em;
      z-index: 9999999;
    }
    .nav > ul > li:hover > ul {
      display: block;
    }
    .nav > ul ul li a {}
    .nav > ul ul li a:hover {}

    /* Navigation Bar - Level 3 */
    .nav > ul > li > ul > li > ul {
      text-align: left;
      display: none;
      background: #57b759;  /* Light green. */
      position: absolute;
      left: 100%;
      top: 0;
      padding-bottom: 5px;
      min-width: 150px;
      border-radius: 0 0 .5em .5em;
      z-index: 9999999;
    }
    .nav > ul > li > ul > li:hover > ul {
      display: block;
    }
    .nav > ul ul ul li {}
    .nav > ul ul ul li a {}
    .nav > ul ul ul li a:hover {}

    /* Navigation Bar - Level 4 */
    .nav > ul > li > ul > li > ul > li > ul {
      text-align: left;
      display: none;
      background: #57b759;  /* Light green. */
      position: absolute;
      left: 100%;
      top: 0;
      padding-bottom: 5px;
      min-width: 250px;
      border-radius: 0 0 .5em .5em;
      z-index: 9999999;
    }
    .nav > ul > li > ul > li > ul > li:hover > ul {
      display: block;
    }
    .nav > ul ul ul ul li {}
    .nav > ul ul ul ul li a {}
    .nav > ul ul ul ul li a:hover {}

.nav > ul {
  display: flex;
  flex-direction: column;
}
.nav {
  width: 200px;
  height: 100vh;
}
.nav > ul > li > ul {
  z-index: 9999999;
  right: 0;
  left: 200px;
}
<nav class="nav">
  <ul>
    <li><a href="#">Section 1</a></li>
    <li><a href="#">Section 2 ▾</a>
      <ul>
        <li><a href="#">Section 2.1</a></li>
        <li><a href="#">Section 2.2</a></li>
        <li><a href="#">Section 2.3</a></li>
      </ul>
    </li>
    <li><a href="#" class="active">Section 3 ▾</a>
      <ul>
        <li><a href="#" class="active">Section 3.1</a></li>
        <li><a href="#">Section 3.2 ▸</a>
          <ul>
            <li><a href="#">Section 3.2.1</a></li>
            <li><a href="#">Section 3.2.2</a></li>
            <li><a href="#">Section 3.2.3 ▸</a>
              <ul>
                <li><a href="#">Section 3.2.3.1</a></li>
                <li><a href="#">Section 3.2.3.2</a></li>
              </ul>
            </li>
          </ul>
        </li>
        <li><a href="#">Section 3.3</a></li>
      </ul>
    </li>
  </ul>
</nav>