目次
回复内容:
ホームページ ウェブフロントエンド 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ヘンタイを無料で生成します。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

HTML5 Webサイトにオーディオを追加する方法は? HTML5 Webサイトにオーディオを追加する方法は? Mar 10, 2025 pm 03:01 PM

この記事では、&lt; audio&gt;を使用してHTML5にオーディオを埋め込む方法について説明します。フォーマット選択のベストプラクティス(MP3、OGG Vorbis)、ファイルの最適化、および再生のためのJavaScriptコントロールを含む要素。 複数のオーディオfを使用して強調しています

ユーザー入力にHTML5フォームを使用する方法は? ユーザー入力にHTML5フォームを使用する方法は? Mar 10, 2025 pm 02:59 PM

この記事では、HTML5フォームを作成および検証する方法について説明します。 &lt; form&gt;の詳細要素、入力タイプ(テキスト、電子メール、番号など)、および属性(必須、パターン、MIN、最大)。 HTML5の利点は、古い方法よりも形成されます

HTML5ページの可視性APIを使用して、ページが表示されたときに検出するにはどうすればよいですか? HTML5ページの可視性APIを使用して、ページが表示されたときに検出するにはどうすればよいですか? Mar 13, 2025 pm 07:51 PM

この記事では、HTML5ページの可視性APIを使用して、ページの可視性を検出し、ユーザーエクスペリエンスを向上させ、リソースの使用量を最適化します。重要な側面には、メディアの一時停止、CPU負荷の削減、視認性の変化に基づいて分析の管理が含まれます。

ViewPortメタタグを使用して、モバイルデバイスのページスケーリングを制御するにはどうすればよいですか? ViewPortメタタグを使用して、モバイルデバイスのページスケーリングを制御するにはどうすればよいですか? Mar 13, 2025 pm 08:00 PM

この記事では、ビューポートメタタグを使用してモバイルデバイスのページスケーリングを制御し、最適な応答性とパフォーマンスのために幅や初期スケールなどの設定に焦点を当てています。

Geolocation APIでユーザーの場所のプライバシーと許可を処理するにはどうすればよいですか? Geolocation APIでユーザーの場所のプライバシーと許可を処理するにはどうすればよいですか? Mar 18, 2025 pm 02:16 PM

この記事では、Geolocation 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を使用してインタラクティブなユーザーインターフェイスを作成し、要素をドラッグ可能にし、キーイベントを処理し、カスタムフィードバックを使用してユーザーエクスペリエンスを強化する手順を作成する方法について説明します。また、aへの一般的な落とし穴についても説明します

クライアントとサーバー間の双方向通信にHTML5 WebSockets APIを使用するにはどうすればよいですか? クライアントとサーバー間の双方向通信にHTML5 WebSockets APIを使用するにはどうすればよいですか? Mar 12, 2025 pm 03:20 PM

この記事では、リアルタイムの双方向のクライアントサーバー通信のために、HTML5 WebSockets APIについて説明します。 クライアントサイド(JavaScript)とサーバー側(Python/Flask)の実装を詳述し、スケーラビリティ、状態管理、などの課題に対処します。

See all articles