首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >纯html和Css点击下拉菜单

纯html和Css点击下拉菜单
EN

Stack Overflow用户
提问于 2022-04-26 22:58:47
回答 1查看 135关注 0票数 0

我正在尝试创建一个onclick下拉菜单,它将出现在移动设备上,当屏幕更宽时(出现在另一个部分)就会消失,但是@media查询允许我这样做,但是菜单中的链接是不可点击的。

我试着玩z指数,但没有成功,也有其他来自互联网的想法,但我没有成功。请告诉我出了什么问题。

我指的下拉列表是下面代码中的“选择一个类别”和“项目列表”。

我使用了这个源代码中的代码片段来编写下拉列表:https://lage.us/CSS-Onclick-Dropdown-Menu.html

我尽了最大的努力来修改下面的代码。

代码语言:javascript
复制
 .grid-container {
   background: #E2E5AA;
   display: border-box;
   padding: 0px;
   margin: 0px;
   background-size: cover;
 }

  .header {
   grid-area: header;
 }

  #navigation {   grid-area: navigation;}
  #services {grid-area: services;}
 .drop-down {grid-area: dropdown;}
  #introduction {grid-area: description;}
#client-terminal {grid-area: client; }


 @media screen and (min-width: 250px) {
   .grid-container {
     width: 100%;
     height: 100%;
     display: grid;
     grid-template-areas: 'header header header client'
       'navigation navigation navigation dropdown'
       'description description description description'
       'services services services services';
   }

   .my-dropdown {
     color: #E2E5AA;
     position: relative;
     padding: 4px;
     background: #790D76;
     border: none;
     cursor: pointer;
     font-size: 15px;
     font-weight: normal;
     float: right;
     z-index: 1;
   }

   .my-dropdown:before {
     content: "Price-List";
     color: #E2E5AA;
     position: relative;
     padding: 4px;
     background: #790D76;
     border: none;
     cursor: ;
     font-size: 15px;
     font-weight: normal;
     float: right;
     z-index: 1;
   }

   .my-dropdown:focus {
     pointer-events: none;
   }

   .my-dropdown:focus .dropdown-content {
     opacity: 1;
     visibility: visible;
   }

   .my-dropdown:hover {
     background-color: #8F228C
   }

   .drop-down {
     position: relative;
     display: inline-block;
     background-color: #790D76;
   }

   .dropdown-content {
     visibility: hidden;
     position: absolute;
     background-color: #E2E5AA;
     min-width: ;
 2;
     overflow: hidden;
     pointer-events: auto;
   }

   .dropdown-content a {
     color: black;
     padding: 4px;
     text-decoration: none;
     display: block;
     font-size: 15px;
     z-index:3;
   }

   /* style option on hover*/

   .dropdown-content a:hover {
     background-color: #8F228C;
   }

  #navigation {
     background-color: #790D76;
   }
  
   #client-terminal {
     background-color: #290C28;
   }

  
   .dropdown-2-button:before {
     content: "Select A Category";
     color: #E2E5AA;
     padding: 4px;
     background: #E2E5AA;
     border: none;
     cursor: pointer;
     font-size: 15px;
     font-weight: normal;
     float: left;
     margin: 0px 0px 15px 0px;
     background-color: #790D76;
   }

   .dropdown-2-button:focus {
     pointer-events: none;
   }

   .dropdown-2-button:focus .dropdown-2 {
     opacity: 1;
     visibility: visible;
   }

   #services {
     position: relative;
     display: inline-block;
     background-color: transparent;
     margin: 0px;
   }

   .dropdown-2 {
     list-style-type: none;
     padding: 0;
     margin: 0;
     position: absolute;
     background-color: #E2E5AA;
     min-width: relative;
     z-index: 1;
     overflow: hidden;
     pointer-events: auto;
     visibility: hidden;
     border-radius: 3px;
   }

   .dropdown-2 a {
     color: black;
     padding: 4px;
     text-decoration: none;
     display: block;
     font-size: 15px;
   }

   .dropdown-2 a:hover {
     background-color: #790D76;
   }
  
   #introduction {
     display: block;
   }

   #introduction sub-heading {
     display: block;
     font-size: 20px;
     padding: 10px;
     text-align: center;
     font-weight: bold;
   }

   #introduction p {
     text-align: center;
     font-size: 15px;
   }

   #introduction a {
     text-decoration: none;
     color: purple;
     display: inline-block;
   }

   #introduction a:hover {
     background-color: #8F228C;
     color: #E2E5AA
   }
   
   .c-drpbtn p,
   .a_b-drpbtn p,
   .cr-drpbtn p,
   .m_m-drpbtn p,
   .sheq-drpbtn p {
     display: none;
   }

 }


 @media screen and (min-width: 640px) {
   .grid-container {
     width: 100%;
     height: 100%;
     padding: 0px;
     margin: 0px;
     display: grid;
     grid-template-areas: 'header header header header'
       'navigation navigation navigation client'
       'dropdown dropdown description description'
       'dropdown dropdown services services'
       'footer footer footer footer';
   }


   #introduction {
     padding: 20px;
     font-size: 130%;
     font-weight: bold
   }

   #introduction p {
     font-size: 110%
   }


 .c-drpbtn p,
   .a_b-drpbtn p,
   .cr-drpbtn p,
   .m_m-drpbtn p,
   .sheq-drpbtn p {
     color: black;
     position: relative;
     padding: 4px;
     background: transparent;
     opacity: 0.6;
     border: none;
     cursor: pointer;
     font-weight: normal;
     z-index: 1;
     display: none;
     margin: 4px;
   }
   
   .c-drpbtn:focus,
   .a_b-drpbtn:focus,
   .cr-drpbtn:focus,
   .m_m-drpbtn:focus,
   .sheq-drpbtn:focus {
 pointer-events: none;
   }
   
    .c-drpbtn:focus p,
   .a_b-drpbtn:focus p,
   .cr-drpbtn:focus p,
   .m_m-drpbtn:focus p,
   .sheq-drpbtn:focus p {
     opacity: 1;
     display: block;
   }
   
    .c-drpbtn:hover,
   .a_b-drpbtn:hover,
   .cr-drpbtn:hover,
   .m_m-drpbtn:hover,
   .sheq-drpbtn:hover {
     background-color: #8F228C;
   }
   
   .c-drpbtn:before,
   .a_b-drpbtn:before,
   .cr-drpbtn:before,
   .m_m-drpbtn:before,
   .sheq-drpbtn:before {
     color: black;
     display: block;
     padding: 20px;
     background: transparent;
     opacity: 1;
     border: none;
     cursor: pointer;
     font-weight: bold;
     text-align: center;
     margin: auto;
     z-index: 1;
   }

   .a_b-drpbtn:before {
     content: "\2193  Item 1";
   }

   .c-drpbtn:before {
     content: "\2193  Item 2";
   }

.m_m-drpbtn:before {
     content: "\2193 Item 3";
   }

   .sheq-drpbtn:before {
     content: "\2193  Item 4";
   }


   .cr-drpbtn:before {
     content: "\2193  Item 5";
     }

   .dropdown-2-button {
     visibility: hidden;
   }

   .drop-down {
     display: block;
     background: #E2E5AA;
     width: 100%;
     border: #790D76 solid;
     border-width: 0px 10px 0px 4px;
   }

   .dropdown-content {
     visibility: visible;
     display: block;
     overflow: 0px;
     width: 100%;
     position: relative;
     background: #E2E5AA;
     pointer-events: auto;
     font-size: 130%;
   }

   .my-dropdown:hover {
     background-color: #E2E5AA;
   }

 

   .my-dropdown {

     color: black;
     position: relative;
     padding: 4px;
     background: #E2E5AA;
     border: none;
     cursor: pointer;
     font-size: 15px;
     font-weight: normal;
     text-align: left;
     float: left;
     z-index: 1;
   }

   .my-dropdown:before {
     content: "item-List";
     color: black;
     position: relative;
     padding: 4px;
     background: transparent;
     border: none;
     cursor: pointer;
     font-size: 15px;
     font-weight: normal;
     text-align: left;
     z-index: 1;
   }

   

   .dropdown-content a:hover {
     background-color: #8F228C;
   }
   
   #client-terminal {
     background-color: #790D76;
   }
 }
代码语言:javascript
复制
<!DOCTYPEhtml>
  <html>

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <head>
    </head>

    <body>

      <div class="grid-container">

        <div class="header" style="diplay: inline-block">
        </div>
        <div id="navigation">
        </div>


        <div id="services">
          <div tabindex="0" class="dropdown-2-button">
            <div class="dropdown-2" id="dropdown3">
              <a href="item1.php">item 1</a>
              <a href="item2.php">item 2</a>
              <a href="item3.php">item 3</a>
              <a href="item4.php">item 4</a>
              <a href="item5.php">item 5</a>
            </div>
          </div>
        </div>


        <div class="drop-down">
          <div tabindex="0" class="my-dropdown">
            <div class="dropdown-content">
              <a href="p1.php">p1</a>
              <a href="p2.php">p2</a>
              <a href="p3.php">p3</a>
              <a href="p4.php">p4</a>
              <a href="p5.php">p5</a>
            </div>
          </div>
        </div>


        <div id="client-terminal">
        </div>
        <div id="introduction">

          <div class="a_b-drpbtn" tabindex="0">
            <p>abc<br>abc<br>abc<br>abc</p>
          </div>
          <div class="cr-drpbtn" tabindex="0">
            <p>abc<br>abc<br>abc<br>abc <br><a href="https://abc.php">test link</a></p>
          </div>
          <div class="sheq-drpbtn" tabindex="0">
            <p>abc<br>abc<br>abc<br>abc</p>
          </div>
          <div class="c-drpbtn" tabindex="0">
            <p>abc<br>abc<br>abc<br>abc</p>
          </div>
          <div class="m_m-drpbtn" tabindex="0">
            <p>abc<br>abc<br>abc<br>abc</p>
          </div>

        </div>

      </div>
    </body>

  </html>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-27 11:41:41

问题是要集中精力。保持在移动环境中的悬停状态,集中在桌面上。

编辑的CSS-

代码语言:javascript
复制
.grid-container {
    background: #e2e5aa;
    display: border-box;
    padding: 0px;
    margin: 0px;
    background-size: cover;
  }

  .header {
    grid-area: header;
  }

  #navigation {
    grid-area: navigation;
  }
  #services {
    grid-area: services;
  }
  .drop-down {
    grid-area: dropdown;
  }
  #introduction {
    grid-area: description;
  }
  #client-terminal {
    grid-area: client;
  }

  /*addd for mobile*/
  .my-dropdown:hover .dropdown-content {
    opacity: 1;
    visibility: visible;
  }

  /*added for mobile*/
  .dropdown-2-button:hover .dropdown-2 {
    opacity: 1;
    visibility: visible;
  }

  @media screen and (min-width: 250px) {
    .grid-container {
      width: 100%;
      height: 100%;
      display: grid;
      grid-template-areas:
        "header header header client"
        "navigation navigation navigation dropdown"
        "description description description description"
        "services services services services";
    }

    .my-dropdown {
      color: #e2e5aa;
      position: relative;
      padding: 4px;
      background: #790d76;
      border: none;
      cursor: pointer;
      font-size: 15px;
      font-weight: normal;
      float: right;
      z-index: 1;
    }

    .my-dropdown:before {
      content: "Price-List";
      color: #e2e5aa;
      position: relative;
      padding: 4px;
      background: #790d76;
      border: none;
      /* cursor: ; */ /*error*/
      font-size: 15px;
      font-weight: normal;
      float: right;
      z-index: 1;
    }

    .my-dropdown:focus {
      pointer-events: none;
    }

    /* .my-dropdown:focus .dropdown-content {
             opacity: 1;
             visibility: visible;
           } */

    .my-dropdown:hover {
      background-color: #8f228c;
    }

    .drop-down {
      position: relative;
      display: inline-block;
      background-color: #790d76;
    }

    .dropdown-content {
      visibility: hidden;
      position: absolute;
      background-color: #e2e5aa;
      /* min-width: ;
         2; */
      /*error*/
      overflow: hidden;
      pointer-events: auto;
    }

    .dropdown-content a {
      color: black;
      padding: 4px;
      text-decoration: none;
      display: block;
      font-size: 15px;
      z-index: 3;
    }

    /* style option on hover*/

    .dropdown-content a:hover {
      background-color: #8f228c;
    }

    #navigation {
      background-color: #790d76;
    }

    #client-terminal {
      background-color: #290c28;
    }

    .dropdown-2-button:before {
      content: "Select A Category";
      color: #e2e5aa;
      padding: 4px;
      background: #e2e5aa;
      border: none;
      cursor: pointer;
      font-size: 15px;
      font-weight: normal;
      float: left;
      margin: 0px 0px 15px 0px;
      background-color: #790d76;
    }

    .dropdown-2-button:focus {
      pointer-events: none;
    }

    /* .dropdown-2-button:focus .dropdown-2 {
             opacity: 1;
             visibility: visible;
           } */

    #services {
      position: relative;
      display: inline-block;
      background-color: transparent;
      margin: 0px;
    }

    .dropdown-2 {
      list-style-type: none;
      padding: 0;
      margin: 0;
      position: absolute;
      background-color: #e2e5aa;
      min-width: relative;
      z-index: 1;
      overflow: hidden;
      pointer-events: auto;
      visibility: hidden;
      border-radius: 3px;
      top: 30px; /*bring the dropdown menu down*/
    }

    .dropdown-2 a {
      color: black;
      padding: 4px;
      text-decoration: none;
      display: block;
      font-size: 15px;
    }

    .dropdown-2 a:hover {
      background-color: #790d76;
    }

    #introduction {
      display: block;
    }

    #introduction sub-heading {
      display: block;
      font-size: 20px;
      padding: 10px;
      text-align: center;
      font-weight: bold;
    }

    #introduction p {
      text-align: center;
      font-size: 15px;
    }

    #introduction a {
      text-decoration: none;
      color: purple;
      display: inline-block;
    }

    #introduction a:hover {
      background-color: #8f228c;
      color: #e2e5aa;
    }

    .c-drpbtn p,
    .a_b-drpbtn p,
    .cr-drpbtn p,
    .m_m-drpbtn p,
    .sheq-drpbtn p {
      display: none;
    }
  }

  @media screen and (min-width: 640px) {
    .grid-container {
      width: 100%;
      height: 100%;
      padding: 0px;
      margin: 0px;
      display: grid;
      grid-template-areas:
        "header header header header"
        "navigation navigation navigation client"
        "dropdown dropdown description description"
        "dropdown dropdown services services"
        "footer footer footer footer";
    }

    #introduction {
      padding: 20px;
      font-size: 130%;
      font-weight: bold;
    }

    #introduction p {
      font-size: 110%;
    }

    .c-drpbtn p,
    .a_b-drpbtn p,
    .cr-drpbtn p,
    .m_m-drpbtn p,
    .sheq-drpbtn p {
      color: black;
      position: relative;
      padding: 4px;
      background: transparent;
      opacity: 0.6;
      border: none;
      cursor: pointer;
      font-weight: normal;
      z-index: 1;
      display: none;
      margin: 4px;
    }

    .c-drpbtn:focus,
    .a_b-drpbtn:focus,
    .cr-drpbtn:focus,
    .m_m-drpbtn:focus,
    .sheq-drpbtn:focus {
      pointer-events: none;
    }

    .c-drpbtn:focus p,
    .a_b-drpbtn:focus p,
    .cr-drpbtn:focus p,
    .m_m-drpbtn:focus p,
    .sheq-drpbtn:focus p {
      opacity: 1;
      display: block;
    }

    .c-drpbtn:hover,
    .a_b-drpbtn:hover,
    .cr-drpbtn:hover,
    .m_m-drpbtn:hover,
    .sheq-drpbtn:hover {
      background-color: #8f228c;
    }

    .c-drpbtn:before,
    .a_b-drpbtn:before,
    .cr-drpbtn:before,
    .m_m-drpbtn:before,
    .sheq-drpbtn:before {
      color: black;
      display: block;
      padding: 20px;
      background: transparent;
      opacity: 1;
      border: none;
      cursor: pointer;
      font-weight: bold;
      text-align: center;
      margin: auto;
      z-index: 1;
    }

    .a_b-drpbtn:before {
      content: "\2193  Item 1";
    }

    .c-drpbtn:before {
      content: "\2193  Item 2";
    }

    .m_m-drpbtn:before {
      content: "\2193 Item 3";
    }

    .sheq-drpbtn:before {
      content: "\2193  Item 4";
    }

    .cr-drpbtn:before {
      content: "\2193  Item 5";
    }

    .dropdown-2-button {
      visibility: hidden;
    }

    .drop-down {
      display: block;
      background: #e2e5aa;
      width: 100%;
      border: #790d76 solid;
      border-width: 0px 10px 0px 4px;
    }

    .dropdown-content {
      visibility: visible;
      display: block;
      overflow: 0px;
      width: 100%;
      position: relative;
      background: #e2e5aa;
      pointer-events: auto;
      font-size: 130%;
    }

    .my-dropdown:hover {
      background-color: #e2e5aa;
    }

    .my-dropdown {
      color: black;
      position: relative;
      padding: 4px;
      background: #e2e5aa;
      border: none;
      cursor: pointer;
      font-size: 15px;
      font-weight: normal;
      text-align: left;
      float: left;
      z-index: 1;
    }

    .my-dropdown:before {
      content: "item-List";
      color: black;
      position: relative;
      padding: 4px;
      background: transparent;
      border: none;
      cursor: pointer;
      font-size: 15px;
      font-weight: normal;
      text-align: left;
      z-index: 1;
    }

    .dropdown-content a:hover {
      background-color: #8f228c;
    }

    #client-terminal {
      background-color: #790d76;
    }
  }

  @media (min-width: 768px) {
    /*add your breakpoint instead of 768*/
    .my-dropdown:focus .dropdown-content {
      opacity: 1;
      visibility: visible;
    }

    .dropdown-2-button:focus .dropdown-2 {
      opacity: 1;
      visibility: visible;
    }
  }

编辑HTML-

代码语言:javascript
复制
<div class="grid-container">
      <div class="header" style="diplay: inline-block"></div>
      <div id="navigation"></div>

      <div id="services">
        <div tabindex="0" class="dropdown-2-button">
          <div class="dropdown-2" id="dropdown3">
            <a href="item1.php">item 1</a>
            <a href="item2.php">item 2</a>
            <a href="item3.php">item 3</a>
            <a href="item4.php">item 4</a>
            <a href="item5.php">item 5</a>
          </div>
        </div>
      </div>

      <div class="drop-down">
        <div tabindex="0" class="my-dropdown">
          <div class="dropdown-content">
            <a href="p1.php">p1</a>
            <a href="p2.php">p2</a>
            <a href="p3.php">p3</a>
            <a href="p4.php">p4</a>
            <a href="p5.php">p5</a>
          </div>
        </div>
      </div>

      <div id="client-terminal"></div>
      <div id="introduction">
        <div class="a_b-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
        <div class="cr-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc <br /><a href="https://abc.php">test link</a></p>
        </div>
        <div class="sheq-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
        <div class="c-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
        <div class="m_m-drpbtn" tabindex="0">
          <p>abc<br />abc<br />abc<br />abc</p>
        </div>
      </div>
    </div>

下面列出的HTML错误-

代码语言:javascript
复制
this should be - display: inline-block
<div class="header" style="diplay: inline-block"></div>

下面列出的CSS错误-

代码语言:javascript
复制
.my-dropdown:before {
          content: "Price-List";
          color: #e2e5aa;
          position: relative;
          padding: 4px;
          background: #790d76;
          border: none;
          /* cursor: ; */ /*error*/
          font-size: 15px;
          font-weight: normal;
          float: right;
          z-index: 1;
        }
    
        .dropdown-content {
          visibility: hidden;
          position: absolute;
          background-color: #e2e5aa;
          /* min-width: ;
             2; */
          /*error*/
          overflow: hidden;
          pointer-events: auto;
        }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72021445

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档