搜尋

首頁  >  問答  >  主體

製作一個響應式的下拉式選單的方法

<p>漢堡選單圖示應該在右側,圖片應該在左側。 </p> <p>當我們點擊選單打開時,它應該會展開一個下拉選單,裡面應該會有logo home <em>About us</em> gallery contact。 </p> <pre class="brush:html;toolbar:false;"><div class="rectangle1"> <ul class="dropdown"> <li class="li1"><a href="home.html"><img class="img" src="YOGI Logo.svg" alt=""> </a></li> <li class="li2"><a class="a1" href="#home">Home</a></li> <li class="li3"><a class="a2" href="aboutus.html">關於我們</a></li> <li class="li4"><a class="a3" href="gallery.html">圖庫</a></li> <li class="li5"><a class="a4" href="contact.html">聯絡我們</a></li> <a class="Contact_Us" href="contact.html"> <img class="Contact-us-img"src="Vector.svg" alt=""> <span class="Contact-us-text">聯絡我們</span> </a> </ul> </div> </pre> <p>我只需要將其轉換為CSS。 </p> <p>我需要它來適應 iPhone 14 Pro 的響應式設計,以成為漢堡菜單。 </p>
P粉521013123P粉521013123527 天前670

全部回覆(2)我來回復

  • P粉748218846

    P粉7482188462023-09-05 09:49:42

    我認為這可能對你有幫助:CSS @media Rule。正如你在教程中所看到的,CSS有一種方法可以檢查螢幕的寬度。

    @media only screen and (max-width: 600px) {
      body {
        background-color: lightblue;
      }
    }
    

    在這個例子中,當寬度小於600像素時,背景顏色將被設定為淺藍色。使用這個規則,你可以相應地改變你的CSS。

    你應該嘗試自己實現它,而不僅僅是從網路上複製貼上。去嘗試,享受使用CSS的樂趣;這是學習它的唯一方法。

    回覆
    0
  • P粉738821035

    P粉7388210352023-09-05 00:41:11

    這是修改後的CSS程式碼,用於實現響應式漢堡選單:

    CSS(styles.css):

    body {
        margin: 0;
        font-family: Arial, sans-serif;
    }
    
    header {
        background-color: #333;
        color: #fff;
        padding: 10px;
        display: flex;
        align-items: center;
    }
    
    .container {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }
    
    .logo {
        color: #fff;
        text-decoration: none;
        font-size: 24px;
    }
    
    .menu-toggle {
        width: 30px;
        height: 30px;
        background-color: #fff;
        cursor: pointer;
        display: none; /* 在较大屏幕上默认隐藏菜单图标 */
    }
    
    .menu-toggle::before, .menu-toggle::after {
        content: "";
        display: block;
        width: 100%;
        height: 3px;
        background-color: #333;
    }
    
    .menu {
        display: flex;
        align-items: center;
    }
    
    .menu ul {
        list-style: none;
        margin: 0;
        padding: 0;
        display: flex;
    }
    
    .menu li {
        padding: 10px;
    }
    
    .menu a {
        color: #fff;
        text-decoration: none;
        font-size: 18px;
    }
    
    /* 移动设备的媒体查询 */
    @media only screen and (max-width: 767px) {
        .menu {
            display: none; /* 在小屏幕上默认隐藏菜单 */
            flex-direction: column;
            background-color: #333;
            position: absolute;
            top: 50px;
            right: 0;
            width: 100%;
        }
    
        .menu.active {
            display: flex; /* 激活时显示菜单 */
        }
    
        .menu li {
            width: 100%;
            text-align: center;
        }
    
        .menu-toggle {
            display: block; /* 在小屏幕上显示菜单图标 */
        }
    }

    回覆
    0
  • 取消回覆