목차
回复内容:
웹 프론트엔드 H5 튜토리얼 如何通过html和css完成下拉菜单的制作?

如何通过html和css完成下拉菜单的制作?

Jun 07, 2016 am 08:43 AM

例如这种,当然这只是一个例子,其他的也行,如果可以,请简要说明一下用到哪些技能点和原理。

回复内容:

几个下拉菜单display:none
按钮 :hover 给下拉菜单display:block
也许可以 无脚本,纯HTML和 CSS 实现的多级下拉菜单,在非 IE 内核浏览器和 IE8+ 表现完美。

如何通过html和css完成下拉菜单的制作?

主体使用ul>li>ul>li>......嵌套定义多级。

<span class="cp"><!DOCTYPE HTML></span>
<span class="nt"><html></span>
<span class="nt"><head></span>
  <span class="nt"><meta</span> <span class="na">http-equiv=</span><span class="s">"Content-Type"</span> <span class="na">content=</span><span class="s">"text/html; charset=utf-8"</span><span class="nt">></span>
  <span class="nt"><title></span>纯 CSS 多级菜单<span class="nt"></title></span>
  <span class="nt"><style></span>
    <span class="nc">.menu</span><span class="p">{}</span>
    <span class="nc">.menu</span> <span class="nt">ul</span><span class="o">,</span><span class="nc">.menu</span> <span class="nt">li</span><span class="p">{</span><span class="k">margin</span><span class="o">:</span><span class="m">0</span><span class="p">;</span> <span class="k">padding</span><span class="o">:</span><span class="m">0</span><span class="p">;</span><span class="k">list-style</span><span class="o">:</span><span class="k">none</span> <span class="k">outside</span><span class="p">;}</span>
    <span class="nc">.menu</span><span class="o">></span><span class="nt">ul</span><span class="p">{</span><span class="k">overflow</span><span class="o">:</span><span class="k">auto</span><span class="p">;</span> <span class="k">display</span><span class="o">:</span><span class="k">inline</span><span class="o">-</span><span class="k">block</span><span class="p">;}</span>
    <span class="nc">.menu</span> <span class="nt">a</span><span class="o">,</span><span class="nc">.menu</span> <span class="nt">span</span><span class="p">{</span> <span class="k">cursor</span><span class="o">:</span><span class="k">default</span><span class="p">;</span> <span class="k">height</span><span class="o">:</span><span class="m">30px</span><span class="p">;</span> <span class="k">line-height</span><span class="o">:</span><span class="m">30px</span><span class="p">;</span> <span class="k">padding</span><span class="o">:</span><span class="m">0</span> <span class="m">5px</span><span class="p">;</span> <span class="k">display</span><span class="o">:</span><span class="k">block</span><span class="p">;</span> <span class="k">text</span><span class="o">-</span><span class="k">overflow</span><span class="o">:</span><span class="n">ellipsis</span><span class="p">;</span> <span class="k">overflow</span><span class="o">:</span><span class="k">hidden</span><span class="p">;</span> <span class="k">text-decoration</span><span class="o">:</span><span class="k">none</span><span class="p">;</span> <span class="k">color</span><span class="o">:</span><span class="n">MenuText</span><span class="p">;}</span>
    <span class="nc">.menu</span> <span class="nt">a</span><span class="nd">:hover</span><span class="o">,</span><span class="nc">.menu</span> <span class="nt">span</span><span class="nd">:hover</span><span class="o">,</span><span class="nc">.menu</span> <span class="nt">li</span><span class="nd">:hover</span><span class="o">></span><span class="nt">a</span><span class="o">,</span><span class="nc">.menu</span> <span class="nt">li</span><span class="nd">:hover</span><span class="o">></span><span class="nt">span</span><span class="p">{</span><span class="k">color</span><span class="o">:</span><span class="n">HighlightText</span><span class="p">;}</span>
    <span class="nc">.menu</span> <span class="nt">li</span><span class="nd">:hover</span><span class="p">{</span><span class="k">background-color</span><span class="o">:</span><span class="n">Highlight</span><span class="p">;}</span>
    <span class="nc">.menu</span> <span class="nt">li</span><span class="nd">:hover</span><span class="o">></span><span class="nt">ul</span><span class="p">{</span><span class="k">display</span><span class="o">:</span><span class="k">block</span><span class="p">;}</span>
    <span class="nc">.menu</span><span class="o">></span><span class="nt">ul</span><span class="o">></span><span class="nt">li</span><span class="p">{</span><span class="k">float</span><span class="o">:</span><span class="k">left</span><span class="p">;}</span>
    <span class="nc">.menu</span><span class="o">></span><span class="nt">ul</span><span class="o">></span><span class="nt">li</span> <span class="nt">ul</span><span class="p">{</span><span class="k">display</span><span class="o">:</span><span class="k">none</span><span class="p">;</span><span class="k">float</span><span class="o">:</span><span class="k">left</span><span class="p">;</span> <span class="k">background-color</span><span class="o">:</span><span class="m">#fff</span><span class="p">;</span> <span class="k">border</span><span class="o">:</span><span class="m">1px</span> <span class="k">solid</span> <span class="n">Highlight</span><span class="p">;</span> <span class="k">position</span><span class="o">:</span><span class="k">absolute</span><span class="p">;}</span>
    <span class="nc">.menu</span><span class="o">></span><span class="nt">ul</span><span class="o">></span><span class="nt">li</span> <span class="nt">li</span><span class="p">{</span><span class="k">padding-left</span><span class="o">:</span><span class="m">25px</span><span class="p">;</span><span class="k">margin</span><span class="o">:</span><span class="m">2px</span><span class="p">;</span><span class="k">background</span><span class="o">:</span><span class="k">transparent</span> <span class="sx">url(data:image/gif;base64,R0lGODlhEAAQAIAAAJmZmf///yH5BAAAAAAALAAAAAAQABAAAAIjhI9pwe2+nmRRIQrmjBrmYB1Y93Ak+IXVd6LtiIZwa5JqWAAAOw==)</span> <span class="k">no-repeat</span> <span class="m">5px</span> <span class="k">center</span><span class="p">;</span> <span class="k">width</span><span class="o">:</span><span class="m">150px</span><span class="p">;}</span>
    <span class="nc">.menu</span><span class="o">></span><span class="nt">ul</span><span class="o">></span><span class="nt">li</span><span class="o">></span><span class="nt">ul</span><span class="o">></span><span class="nt">li</span> <span class="nt">ul</span><span class="p">{</span><span class="k">margin-left</span><span class="o">:</span><span class="m">140px</span><span class="p">;</span> <span class="k">margin-top</span><span class="o">:-</span><span class="m">30px</span><span class="p">;}</span>
    <span class="nc">.menu</span> <span class="nt">li</span><span class="nc">.expand</span><span class="o">></span><span class="nt">a</span><span class="o">,</span><span class="nc">.menu</span> <span class="nt">li</span><span class="nc">.expand</span><span class="o">></span><span class="nt">span</span><span class="p">{</span><span class="k">background</span><span class="o">:</span><span class="k">transparent</span> <span class="sx">url(data:image/png;base64,R0lGODlhAwAQAIABAAAAAP///yH5BAEAAAEALAAAAAADABAAAAIKjI8JBtv/wko0FQA7)</span> <span class="k">no-repeat</span> <span class="k">right</span> <span class="k">center</span><span class="p">;</span> <span class="k">padding-right</span><span class="o">:</span><span class="m">10px</span><span class="p">;</span> <span class="k">margin-right</span><span class="o">:</span><span class="m">5px</span><span class="p">;}</span>
    <span class="nc">.menu</span><span class="o">></span><span class="nt">ul</span><span class="o">></span><span class="nt">li</span><span class="nc">.expand</span><span class="o">></span><span class="nt">a</span><span class="o">,</span><span class="nc">.menu</span><span class="o">></span><span class="nt">ul</span><span class="o">></span><span class="nt">li</span><span class="nc">.expand</span><span class="o">></span><span class="nt">span</span><span class="p">{</span> <span class="k">background-image</span><span class="o">:</span><span class="sx">url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAYAAAADCAYAAACwAX77AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAAaSURBVBhXY2BgYPiPAwOFMSXBgjAA0wnmAwASVQv19UVKmwAAAABJRU5ErkJggg==)</span><span class="p">;}</span>
  <span class="nt"></style></span>
<span class="nt"></head></span>

<span class="nt"><body></span>

  <span class="nt"><div</span> <span class="na">id=</span><span class="s">"mainnav"</span> <span class="na">class=</span><span class="s">"menu"</span><span class="nt">></span>
    <span class="nt"><ul></span>
      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
      <span class="nt"><li</span> <span class="na">class=</span><span class="s">"expand"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></span>
        <span class="nt"><ul></span>
          <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
          <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
          <span class="nt"><li</span> <span class="na">class=</span><span class="s">"expand"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>很长很长很长很长很长很长很长很长很长很长<span class="nt"></a></span>
            <span class="nt"><ul></span>
              <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
              <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
              <span class="nt"><li</span> <span class="na">class=</span><span class="s">"expand"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>LinkLink<span class="nt"></a></span>
                <span class="nt"><ul></span>
                  <span class="nt"><li</span> <span class="na">class=</span><span class="s">"expand"</span><span class="nt">><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></span>
                    <span class="nt"><ul></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                    <span class="nt"></ul></span>
                  <span class="nt"></li></span>
                  <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                  <span class="nt"><li</span> <span class="na">class=</span><span class="s">"expand"</span><span class="nt">><span></span>span 菜单项<span class="nt"></span></span>
                    <span class="nt"><ul></span>
                      <span class="nt"><li><span></span>span 菜单项<span class="nt"></span></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                    <span class="nt"></ul></span>
                  <span class="nt"></li></span>
                  <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                  <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
                <span class="nt"></ul></span>
              <span class="nt"></li></span>
              <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
              <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
            <span class="nt"></ul></span>
          <span class="nt"></li></span>
          <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
          <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
        <span class="nt"></ul></span>
      <span class="nt"></li></span>
      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
      <span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Link<span class="nt"></a></li></span>
    <span class="nt"></ul></span>
  <span class="nt"></div></span>

<span class="nt"></body></span>
<span class="nt"></html></span>
로그인 후 복사
既然你这么问了,我就当你是新手了,还好,我也是新手。

下面开说:
感觉用select标签效果好像不是很好,效果如下:
如何通过html和css完成下拉菜单的制作?不如直接像 @周黎伟所说的,用一个button加上4个div

首先复习一下基本知识:
display:
如何通过html和css完成下拉菜单的制作?如何通过html和css完成下拉菜单的制作?如何通过html和css完成下拉菜单的制作?所以这里我用inline-block

代码如下:
如何通过html和css完成下拉菜单的制作?
希望大神们能批评指正

最后的效果如图:
如何通过html和css完成下拉菜单的制作?
<span class="cp"><!DOCTYPE html></span>
<span class="nt"><html</span> <span class="na">lang=</span><span class="s">"en"</span><span class="nt">></span>
<span class="nt"><head></span>
    <span class="nt"><meta</span> <span class="na">charset=</span><span class="s">"UTF-8"</span><span class="nt">></span>
    <span class="nt"><title></span>下拉菜单<span class="nt"></title></span>
    <span class="nt"><style </span><span class="na">type=</span><span class="s">"text/css"</span><span class="nt">></span>
    
    <span class="nc">.bt</span><span class="p">{</span>
        <span class="k">width</span><span class="o">:</span> <span class="m">50px</span><span class="p">;</span><span class="k">height</span><span class="o">:</span> <span class="m">28px</span><span class="p">;</span>
        <span class="k">border</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#ddd</span><span class="p">;</span>
        <span class="k">background-color</span><span class="o">:</span> <span class="m">#eee</span><span class="p">;</span>
        <span class="k">font</span><span class="o">:</span><span class="m">12px</span> <span class="s2">"宋体"</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="nc">.ct</span><span class="p">{</span>
        <span class="k">display</span><span class="o">:</span> <span class="k">none</span><span class="p">;}</span>

    <span class="nc">.dt</span><span class="p">{</span>
        <span class="k">height</span><span class="o">:</span> <span class="m">30px</span><span class="p">;</span>
        <span class="k">margin-top</span><span class="o">:</span> <span class="m">0px</span><span class="p">;</span>
        <span class="k">border</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#ddd</span><span class="p">;</span>
        <span class="k">background-color</span><span class="o">:</span> <span class="nb">white</span><span class="p">;</span>
        <span class="k">padding</span><span class="o">:</span><span class="k">auto</span> <span class="m">10px</span><span class="p">;</span>
       <span class="p">}</span>

    <span class="nc">.dt1</span><span class="p">{</span>
        <span class="k">height</span><span class="o">:</span> <span class="m">30px</span><span class="p">;</span>
        <span class="k">margin-top</span><span class="o">:</span> <span class="m">1px</span><span class="p">;</span>
        <span class="k">border</span><span class="o">:</span> <span class="m">1px</span> <span class="k">solid</span> <span class="m">#ddd</span><span class="p">;</span>
        <span class="k">background-color</span><span class="o">:</span> <span class="nb">white</span><span class="p">;</span>
        <span class="k">padding</span><span class="o">:</span><span class="k">auto</span> <span class="m">10px</span><span class="p">;}</span>
 
    <span class="nc">.dt</span><span class="nd">:hover</span><span class="o">,</span><span class="nc">.dt1</span><span class="nd">:hover</span><span class="p">{</span><span class="k">background-color</span><span class="o">:</span> <span class="m">#ddd</span><span class="p">;}</span>
    <span class="nc">.bt</span><span class="nd">:hover</span><span class="p">{</span><span class="k">background-color</span><span class="o">:</span> <span class="nb">green</span><span class="p">;}</span>
    <span class="nc">.at</span><span class="nd">:hover</span> <span class="nc">.ct</span><span class="p">{</span><span class="k">display</span><span class="o">:</span> <span class="k">inline</span><span class="o">-</span><span class="k">block</span><span class="p">;}</span>
 
    <span class="nt"></style></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><div</span> <span class="na">class=</span><span class="s">"at"</span><span class="nt">></span>
    <span class="nt"><div></span>
        <span class="nt"><button</span> <span class="na">class=</span><span class="s">"bt"</span> <span class="na">type=</span><span class="s">"button"</span> <span class="nt">></span>按钮<span class="nt"></button></span>
    <span class="nt"></div></span>
    <span class="nt"><div</span> <span class="na">class=</span><span class="s">"ct"</span> <span class="nt">></span>
        <span class="nt"><div</span> <span class="na">class=</span><span class="s">"dt1"</span><span class="nt">></span>下拉菜单<span class="nt"></div></span>
        <span class="nt"><div</span> <span class="na">class=</span><span class="s">"dt"</span><span class="nt">></span>下拉菜单<span class="nt"></div></span>
        <span class="nt"><div</span> <span class="na">class=</span><span class="s">"dt"</span><span class="nt">></span>下拉菜单<span class="nt"></div></span>
        <span class="nt"><div</span> <span class="na">class=</span><span class="s">"dt"</span><span class="nt">></span>下拉菜单<span class="nt"></div></span>
    <span class="nt"></div></span>
    <span class="nt"></div></span>  
<span class="nt"></div></span>  
<span class="nt"></body></span>
<span class="nt"></html></span>
로그인 후 복사
要求不高的话用bootstrap 就好了。

做这个功能不难,难的是如何封装和复用。

个人看好polymer这种解决方案。
<span class="cp"><!DOCTYPE html></span>
<span class="nt"><html></span>
<span class="nt"><head></span>
	<span class="nt"><meta</span> <span class="na">charset=</span><span class="s">"utf-8"</span><span class="nt">></span>
	<span class="nt"><title></span>css实现下拉菜单<span class="nt"></title></span>
	<span class="nt"><style </span><span class="na">type=</span><span class="s">"text/css"</span><span class="nt">></span>
	<span class="o">*</span><span class="p">{</span>
		<span class="k">margin</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
		<span class="k">padding</span><span class="o">:</span> <span class="m">0</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nf">#nav</span><span class="p">{</span>
		<span class="k">background-color</span><span class="o">:</span> <span class="m">#eee</span><span class="p">;</span>
		<span class="k">width</span><span class="o">:</span> <span class="m">300px</span><span class="p">;</span>
		<span class="k">height</span><span class="o">:</span> <span class="m">40px</span><span class="p">;</span>
		<span class="k">margin</span><span class="o">:</span> <span class="m">0</span> <span class="k">auto</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt">ul</span><span class="p">{</span>
		<span class="k">list-style</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt">ul</span> <span class="nt">li</span> <span class="p">{</span>
		<span class="k">float</span><span class="o">:</span> <span class="k">left</span><span class="p">;</span>
		<span class="k">line-height</span><span class="o">:</span> <span class="m">40px</span><span class="p">;</span>
		<span class="k">text-align</span><span class="o">:</span> <span class="k">center</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt">a</span><span class="p">{</span>
		<span class="k">text-decoration</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span>
		<span class="k">color</span><span class="o">:</span><span class="m">#000</span><span class="p">;</span>
		<span class="k">padding</span><span class="o">:</span> <span class="m">0</span> <span class="m">10px</span><span class="p">;</span>
		<span class="k">display</span><span class="o">:</span> <span class="k">block</span><span class="p">;</span>	
	<span class="p">}</span>
	<span class="nt">a</span><span class="nd">:hover</span><span class="p">{</span>
		<span class="k">color</span><span class="o">:</span> <span class="m">#fff</span><span class="p">;</span>
		<span class="k">background-color</span><span class="o">:</span> <span class="m">#666</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt">ul</span> <span class="nt">li</span> <span class="nt">ul</span> <span class="nt">li</span><span class="p">{</span>
		<span class="k">float</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span>
		<span class="k">background-color</span><span class="o">:</span> <span class="m">#eee</span><span class="p">;</span>
		<span class="k">margin-top</span><span class="o">:</span> <span class="m">2px</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt">ul</span> <span class="nt">li</span> <span class="nt">ul</span> <span class="p">{</span>
		<span class="k">position</span><span class="o">:</span> <span class="k">absolute</span><span class="p">;</span>
		<span class="k">display</span><span class="o">:</span> <span class="k">none</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt">ul</span> <span class="nt">li</span> <span class="nt">ul</span> <span class="nt">li</span> <span class="nt">a</span><span class="nd">:hover</span><span class="p">{</span>
		<span class="k">background-color</span><span class="o">:</span> <span class="m">#06f</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt">ul</span> <span class="nt">li</span><span class="nd">:hover</span> <span class="nt">ul</span><span class="p">{</span>
		<span class="k">display</span><span class="o">:</span> <span class="k">block</span><span class="p">;</span>
	<span class="p">}</span>
	<span class="nt"></style></span>
<span class="nt"></head></span>
<span class="nt"><body></span>
<span class="nt"><div</span> <span class="na">id=</span><span class="s">"nav"</span><span class="nt">></span>
	<span class="nt"><ul></span>
		<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>按钮1<span class="nt"></a></li></span>
		<span class="nt"><li></span>
			<span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>按钮2<span class="nt"></a></span>
			<span class="nt"><ul></span>
				<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>下拉菜单项<span class="nt"></a></li></span>
				<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>下拉菜单项<span class="nt"></a></li></span>
			<span class="nt"></ul></span>
		<span class="nt"></li></span>
		<span class="nt"><li></span>
			<span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>按钮3<span class="nt"></a></span>
			<span class="nt"><ul></span>
				<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>下拉菜单项<span class="nt"></a></li></span>
				<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>下拉菜单项<span class="nt"></a></li></span>
				<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>下拉菜单项<span class="nt"></a></li></span>
			<span class="nt"></ul></span>
		<span class="nt"></li></span>
		<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>按钮4<span class="nt"></a></li></span>
		<span class="nt"><li><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>按钮5<span class="nt"></a></li></span>
	<span class="nt"></ul></span>
<span class="nt"></div></span>
<span class="nt"></body></span>
<span class="nt"></html></span>
로그인 후 복사
这个是网易云课堂前端微专业的页面布局的期末测试题。 我也试一下。
这里大部分答案都是hover事件触发下拉菜单出现。其实我总觉得这样不太自然,所以我尽力去做成(伪)点击触发。
---------------------------------------------------------------------
# 具体想法:
菜单是, 下拉菜单的容器是
然后利用a:focus + .dropdown{ ... } 来实现。
---------------------------------------------------------------------
要点1:CSS如何实现影响其他元素?
看这里 > html - How to affect other elements when a div is hovered
要点2: 如何做出(伪)点击触发?
基本用 a:focus,个人感觉自然一点。当然如果失去焦点,下拉菜单容器也会相应地隐藏。
--------------------------------------------------------------------
# HTML BODY 部分代码:
<span class="nt"><body></span>
     <span class="nt"><a</span> <span class="na">href=</span><span class="s">"#"</span><span class="nt">></span>Menu<span class="nt"></a></span>
	  <span class="nt"><div</span> <span class="na">class=</span><span class="s">"drop-down"</span> <span class="na">id=</span><span class="s">"drop-down"</span><span class="nt">></span>
		   <span class="nt"><ul></span>
			<span class="nt"><li></span>Option 1<span class="nt"></li></span>				
                        <span class="nt"><li></span>Option 2<span class="nt"></li></span>
			<span class="nt"><li></span>Option 3<span class="nt"></li></span>
			<span class="nt"><li></span>Option 4<span class="nt"></li></span>
		   <span class="nt"></ul></span>
	  <span class="nt"></div></span>
<span class="nt"></body></span>
로그인 후 복사
更正一点小错误,只有一个单选按钮时,选中后就不能取消选中了。所以最好用复选按钮。

原答案如下
===========================

楼上有同学写了css用伪类:hover实现的下拉菜单 这是一种 我再补充下

下拉菜单抛开样式的不同 交互上无非两种
鼠标移到button上出现菜单(如楼上)
以及点击后出现菜单(我要说的)

题主问的是html+css 而很多同学提到了用js添加click事件 这也是大多数下拉菜单的做法
不过为了不跑题 就只用html+css 不用js 那就考css基本功了

这里简单说下思路

html结构如下
<span class="nt"><button></span>点击我出现下拉菜单<span class="nt"></button></span>
<span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbok"</span><span class="nt">></span> 
<span class="nt"><div></span>
	<span class="nt"><ul></span>
		<span class="nt"><li></span>XXX<span class="nt"></li></span>
		<span class="nt"><li></span>XXX<span class="nt"></li></span>
		<span class="nt"><li></span>XXX<span class="nt"></li></span>
		<span class="nt"><li></span>XXX<span class="nt"></li></span>
	<span class="nt"></ul></span>
<span class="nt"></div></span>
로그인 후 복사
用列表,隐藏,然后hover.的时候block就可以了 pepsized.com/css-only-a

如果想用纯css实现click效果:
tympanus.net/codrops/20
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

뜨거운 도구

메모장++7.3.1

메모장++7.3.1

사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

내 HTML5 웹 사이트에 오디오를 추가하는 방법은 무엇입니까? 내 HTML5 웹 사이트에 오디오를 추가하는 방법은 무엇입니까? Mar 10, 2025 pm 03:01 PM

이 기사는 & lt; audio & gt를 사용하여 HTML5에 오디오를 포함시키는 방법을 설명합니다. 형식 선택에 대한 모범 사례 (MP3, OGG Vorbis), 파일 최적화 및 재생에 대한 JavaScript 컨트롤을 포함한 요소. 다중 오디오를 사용하는 것을 강조합니다

사용자 입력에 HTML5 양식을 사용하는 방법은 무엇입니까? 사용자 입력에 HTML5 양식을 사용하는 방법은 무엇입니까? Mar 10, 2025 pm 02:59 PM

이 기사에서는 HTML5 양식을 작성하고 검증하는 방법을 설명합니다. 그것은 & lt; form & gt; 요소, 입력 유형 (텍스트, 이메일, 번호 등) 및 속성 (필수, 패턴, 최소, 최대). HTML5의 장점은 오래된 방법에 비해 형성됩니다

HTML5 페이지 가시성 API를 사용하여 페이지가 표시되는시기를 감지하려면 어떻게합니까? HTML5 페이지 가시성 API를 사용하여 페이지가 표시되는시기를 감지하려면 어떻게합니까? Mar 13, 2025 pm 07:51 PM

이 기사에서는 HTML5 페이지 가시성 API를 사용하여 페이지 가시성을 감지하고 사용자 경험을 향상 시키며 리소스 사용량을 최적화하는 것에 대해 설명합니다. 주요 측면에는 미디어 일시 정지, CPU 부하 감소 및 가시성 변경에 기반한 분석 관리가 포함됩니다.

뷰포트 메타 태그를 사용하여 모바일 장치에서 페이지 스케일링을 제어하려면 어떻게합니까? 뷰포트 메타 태그를 사용하여 모바일 장치에서 페이지 스케일링을 제어하려면 어떻게합니까? Mar 13, 2025 pm 08:00 PM

이 기사에서는 Viewport Meta 태그를 사용하여 모바일 장치의 페이지 스케일링을 제어하여 폭과 최적의 응답 성 및 성능을위한 초기 스케일과 같은 설정에 중점을 둡니다. character count : 159

Geolocation API로 사용자 위치 개인 정보 및 권한을 어떻게 처리합니까? Geolocation API로 사용자 위치 개인 정보 및 권한을 어떻게 처리합니까? Mar 18, 2025 pm 02:16 PM

이 기사는 지리적 위치 API를 사용하여 사용자 위치 개인 정보 및 권한 관리, 권한 요청, 데이터 보안 보장 및 개인 정보 보호법 준수에 대한 모범 사례를 강조하는 것에 대해 설명합니다.

HTML5 및 JavaScript로 대화 형 게임을 만드는 방법은 무엇입니까? HTML5 및 JavaScript로 대화 형 게임을 만드는 방법은 무엇입니까? Mar 10, 2025 pm 06:34 PM

이 기사는 JavaScript를 사용하여 대화식 HTML5 게임을 만드는 자세한 내용입니다. 게임 디자인, HTML 구조, CSS 스타일, JavaScript 로직 (이벤트 처리 및 애니메이션 포함) 및 오디오 통합을 다룹니다. 필수 JavaScript 라이브러리 (Phaser, PI

대화식 사용자 인터페이스에 HTML5 드래그 앤 드롭 API를 어떻게 사용합니까? 대화식 사용자 인터페이스에 HTML5 드래그 앤 드롭 API를 어떻게 사용합니까? Mar 18, 2025 pm 02:17 PM

이 기사는 HTML5 드래그 앤 드롭 API를 사용하여 대화식 사용자 인터페이스를 생성하고 요소를 드래그 가능하게 만들고 주요 이벤트를 처리하며 사용자 정의 피드백으로 사용자 경험을 향상시키는 방법을 자세히 설명합니다. 또한 일반적인 함정에 대해 설명합니다

클라이언트와 서버 간의 양방향 통신에 HTML5 WebSockets API를 어떻게 사용합니까? 클라이언트와 서버 간의 양방향 통신에 HTML5 WebSockets API를 어떻게 사용합니까? Mar 12, 2025 pm 03:20 PM

이 기사에서는 실시간, 양방향 클라이언트 서버 커뮤니케이션을위한 HTML5 WebSockets API를 설명합니다. 클라이언트 측 (JavaScript) 및 서버 측 (Python/Flask) 구현에 대해 자세히 설명하여 확장 성, 상태 관리,

See all articles