如何创建纯 CSS(垂直)下拉菜单?

发布于 2024-12-16 16:20:31 字数 606 浏览 0 评论 0原文

下午好,

我当前的任务是为网站创建几个样式表。其中一种网站样式要求我创建一个下拉菜单,但是我根本不允许更改 HTML 代码,所以基本上我被要求仅使用 CSS 创建一个类似下拉菜单的菜单。

以下是我必须以下拉菜单形式显示的 HTML 代码:

<div id="global-nav">
<ul>
<li><a href="#products">Products</a>
  <ul>
  <li><a href="#widgets">Widgets</a></li>
  <li><a href="#sites">Sites</a></li>
  <li><a href="#gadgets">Gadgets</a></li>
  </ul>
</li>
</ul>

但也有不同的要求: 每个列表项前面不应有任何点或圆圈。

我想知道是否可以仅使用 CSS 来完成此任务。 有什么办法可以用 CSS 做到这一点吗?

Good afternoon,

My current task is to create several stylesheets for a website. One of the websites styles requires me to create a drop-down menu, I however am not allowed to change the HTML code at all, so basically I'm asked to create a drop-down like menu with CSS only.

Here is the HTML code I have to display in form of a drop-down menu:

<div id="global-nav">
<ul>
<li><a href="#products">Products</a>
  <ul>
  <li><a href="#widgets">Widgets</a></li>
  <li><a href="#sites">Sites</a></li>
  <li><a href="#gadgets">Gadgets</a></li>
  </ul>
</li>
</ul>

There however are different requirements as well:
There shouldn't be any dots or circles preceding each list item.

I'm wondering whether it is possible to accomplish this task with CSS only or not.
Is there any way I can do this with CSS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他不在意 2024-12-23 16:20:31

水平扩展的垂直菜单

jsBin demo

*{padding:0;margin:0;}
body{font:16px/1 sans-serif}


/*VERTICAL MENU*/
nav.vertical{
  position:relative;
  width:200px;
}

/* ALL UL */
nav.vertical ul{
  list-style: none;
}
/* ALL LI */
nav.vertical li{
  position:relative;
}
/* ALL A */
nav.vertical a{
  display:block;
  color:#eee;
  text-decoration:none;
  padding:10px 15px;
  background:#667;
  transition:0.2s;
}
/* ALL A HOVER */
nav.vertical li:hover > a{
  background:#778;
}

/* INNER UL HIDE */
nav.vertical ul ul{
  position:absolute;
  left:0%;
  top:0;
  width:100%;
  visibility:hidden;
  opacity:0;
  transition: transform 0.2s;
  transform: translateX(50px);
}
/* INNER UL SHOW */
nav.vertical li:hover > ul{
  left:100%;
  visibility:visible;
  opacity:1;
  transform: translateX(0px);
}
<nav class="vertical">
  <ul>
    <li><a href="">Home</a></li>
    <li><a href="#">Products +</a>
      <ul>
        <li><a href="#">Widgets</a></li>
        <li>
          <a href="#">Sites +</a>
          <ul>
            <li><a href="#">Site 1</a></li>
            <li><a href="#">Site 2</a></li>
          </ul>
        </li>
        <li>
          <a href="#">Gadgets +</a>
          <ul>
            <li><a href="#">Gadget 1</a></li>
            <li><a href="#">Gadget 2</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="">Contact</a></li>
  </ul>
</nav>

垂直菜单(仅限移动设备)

此菜单可能最适合移动设备(较小屏幕的 CSS),否则显示/隐藏会影响用户体验

jsBin 演示

*{padding:0;margin:0;}
body{font:16px/1 sans-serif}


/*VERTICAL MENU*/
nav.vertical{
  position:relative;
  background:#667;
}

/* ALL UL */
nav.vertical ul{
  list-style: none;
}
/* ALL LI */
nav.vertical li{
  position:relative;
}
/* ALL A */
nav.vertical a{
  display:block;
  color:#eee;
  text-decoration:none;
  padding:10px 15px;
  transition:0.2s;
}
/* ALL A HOVER */
nav.vertical li:hover > a{
  background:#778;
}

/* INNER UL HIDE */
nav.vertical ul ul{
  background:rgba(0,0,0,0.1);
  padding-left:20px;
  transition: max-height 0.2s ease-out;
  max-height:0;
  overflow:hidden;
}
/* INNER UL SHOW */
nav.vertical li:hover > ul{
  max-height:500px;
  transition: max-height 0.25s ease-in;
}
<nav class="vertical">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Services +</a>
      <ul>
        <li><a href="#">Service 1</a></li>
        <li><a href="#">Service 2</a></li>
        <li><a href="#">Service 3</a></li>
      </ul>
    </li>
    <li><a href="#">Products +</a>
      <ul>
        <li><a href="#">Widgets</a></li>
        <li>
          <a href="#">Sites +</a>
          <ul>
            <li><a href="#">Site 1</a></li>
            <li><a href="#">Site 2</a></li>
          </ul>
        </li>
        <li><a href="#">Gadgets +</a>
          <ul>
            <li><a href="#">Gadget 1</a></li>
            <li><a href="#">Gadget 2</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="">Contact</a></li>
  </ul>
</nav>

Vertical menu with horizontal expansion

jsBin demo

*{padding:0;margin:0;}
body{font:16px/1 sans-serif}


/*VERTICAL MENU*/
nav.vertical{
  position:relative;
  width:200px;
}

/* ALL UL */
nav.vertical ul{
  list-style: none;
}
/* ALL LI */
nav.vertical li{
  position:relative;
}
/* ALL A */
nav.vertical a{
  display:block;
  color:#eee;
  text-decoration:none;
  padding:10px 15px;
  background:#667;
  transition:0.2s;
}
/* ALL A HOVER */
nav.vertical li:hover > a{
  background:#778;
}

/* INNER UL HIDE */
nav.vertical ul ul{
  position:absolute;
  left:0%;
  top:0;
  width:100%;
  visibility:hidden;
  opacity:0;
  transition: transform 0.2s;
  transform: translateX(50px);
}
/* INNER UL SHOW */
nav.vertical li:hover > ul{
  left:100%;
  visibility:visible;
  opacity:1;
  transform: translateX(0px);
}
<nav class="vertical">
  <ul>
    <li><a href="">Home</a></li>
    <li><a href="#">Products +</a>
      <ul>
        <li><a href="#">Widgets</a></li>
        <li>
          <a href="#">Sites +</a>
          <ul>
            <li><a href="#">Site 1</a></li>
            <li><a href="#">Site 2</a></li>
          </ul>
        </li>
        <li>
          <a href="#">Gadgets +</a>
          <ul>
            <li><a href="#">Gadget 1</a></li>
            <li><a href="#">Gadget 2</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="">Contact</a></li>
  </ul>
</nav>

Vertical menu (mobile only)

this one might best suit for mobile (smaller screens CSS) otherwise the show/hide would troll with User Experience

jsBin demo

*{padding:0;margin:0;}
body{font:16px/1 sans-serif}


/*VERTICAL MENU*/
nav.vertical{
  position:relative;
  background:#667;
}

/* ALL UL */
nav.vertical ul{
  list-style: none;
}
/* ALL LI */
nav.vertical li{
  position:relative;
}
/* ALL A */
nav.vertical a{
  display:block;
  color:#eee;
  text-decoration:none;
  padding:10px 15px;
  transition:0.2s;
}
/* ALL A HOVER */
nav.vertical li:hover > a{
  background:#778;
}

/* INNER UL HIDE */
nav.vertical ul ul{
  background:rgba(0,0,0,0.1);
  padding-left:20px;
  transition: max-height 0.2s ease-out;
  max-height:0;
  overflow:hidden;
}
/* INNER UL SHOW */
nav.vertical li:hover > ul{
  max-height:500px;
  transition: max-height 0.25s ease-in;
}
<nav class="vertical">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">Services +</a>
      <ul>
        <li><a href="#">Service 1</a></li>
        <li><a href="#">Service 2</a></li>
        <li><a href="#">Service 3</a></li>
      </ul>
    </li>
    <li><a href="#">Products +</a>
      <ul>
        <li><a href="#">Widgets</a></li>
        <li>
          <a href="#">Sites +</a>
          <ul>
            <li><a href="#">Site 1</a></li>
            <li><a href="#">Site 2</a></li>
          </ul>
        </li>
        <li><a href="#">Gadgets +</a>
          <ul>
            <li><a href="#">Gadget 1</a></li>
            <li><a href="#">Gadget 2</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="">Contact</a></li>
  </ul>
</nav>

薄荷梦 2024-12-23 16:20:31

只是上述出色解决方案的稍微增强版本。

<style type="text/css">

#global-nav {
    width: 121px;
    float: left;
    background: #e8eef4;
}

#global-subnav {
    width: 121px;
    background: #09C;
}

#global-nav a {
    color: #034af3;
    cursor: pointer;
    display: block;
    height: 40px;
    line-height: 40px;   
    text-indent: 10px;               
    text-decoration:none;
    font-weight: bold;
    width: 100%;
}

#global-nav ul{
    background: yellow;
    padding: 0;
    margin: 0;
}

#global-subnav ul{
    background: orangered;
    position: relative;
    top: -10px;
    left: 40px;
}

#global-nav li{
    list-style: none;   
    border-bottom: #5C87B2 solid;
    border-width: 3px;
}

#global-nav ul ul li{
    display:none;
}

#global-nav li:hover {
    background: #fff;
}

#global-nav li:hover ul li{
    display:block;
}

</style>





<div id="global-nav">
    <ul>
        <li><a href="#One">One</a>
            <div id="global-subnav">
                <ul>
                    <li><a href="#A">A</a></li>
                    <li><a href="#B">B</a></li>
                    <li><a href="#C">C</a></li>
                </ul>
            </div>
        </li>
        <li><a href="#Two">Two</a>
            <div id="global-subnav">
                    <ul>
                        <li><a href="#1">1</a></li>
                        <li><a href="#2">2</a></li>
                        <li><a href="#3">3</a></li>
                    </ul>
            </div>
        </li>
    </ul>
</div>

Just a slightly enhanced version of the great solution above.

<style type="text/css">

#global-nav {
    width: 121px;
    float: left;
    background: #e8eef4;
}

#global-subnav {
    width: 121px;
    background: #09C;
}

#global-nav a {
    color: #034af3;
    cursor: pointer;
    display: block;
    height: 40px;
    line-height: 40px;   
    text-indent: 10px;               
    text-decoration:none;
    font-weight: bold;
    width: 100%;
}

#global-nav ul{
    background: yellow;
    padding: 0;
    margin: 0;
}

#global-subnav ul{
    background: orangered;
    position: relative;
    top: -10px;
    left: 40px;
}

#global-nav li{
    list-style: none;   
    border-bottom: #5C87B2 solid;
    border-width: 3px;
}

#global-nav ul ul li{
    display:none;
}

#global-nav li:hover {
    background: #fff;
}

#global-nav li:hover ul li{
    display:block;
}

</style>





<div id="global-nav">
    <ul>
        <li><a href="#One">One</a>
            <div id="global-subnav">
                <ul>
                    <li><a href="#A">A</a></li>
                    <li><a href="#B">B</a></li>
                    <li><a href="#C">C</a></li>
                </ul>
            </div>
        </li>
        <li><a href="#Two">Two</a>
            <div id="global-subnav">
                    <ul>
                        <li><a href="#1">1</a></li>
                        <li><a href="#2">2</a></li>
                        <li><a href="#3">3</a></li>
                    </ul>
            </div>
        </li>
    </ul>
</div>
稀香 2024-12-23 16:20:31

上一篇文章的代码是错误的。

文档中不能有超过 1 个同名 ID,因此如果使用上面的代码,则需要将

ID="global-subnav" 更改为 class ="global-subnav"

,然后将 CSS 从

#global-subnav 更改为 .global-subnav

The code is wrong on the last post.

You can't have more than 1 ID with the same name in a document, so if you use the code above, you'll need to change

ID="global-subnav" to class="global-subnav"

and then change the CSS from

#global-subnav to .global-subnav

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文