如何創建:菜單中的輸入字段
學習如何創建其中包含輸入字段的導航菜單。
如何在導航欄中添加輸入字段
第一步 - 添加 HTML:
<div class="topnav"> <a class="active" href="#home">Home</a> <a href="#about">About</a> <a href="#contact">Contact</a> <div class="search-container"> <form action="/action_page.php"> <input type="text" placeholder="Search.." name="search"> <button type="submit">Submit</button> </form> </div> </div>
第二步 - 添加 CSS:
* {box-sizing: border-box;} /* 設置導航欄的樣式 */ .topnav { overflow: hidden; background-color: #e9e9e9; } /* 設置導航欄鏈接的樣式 */ .topnav a { float: left; display: block; color: black; text-align: center; padding: 14px 16px; text-decoration: none; font-size: 17px; } /* 設置鼠標懸停在導航欄鏈接上時的樣式 */ .topnav a:hover { background-color: #ddd; color: black; } /* 設置當前/活動鏈接的樣式 */ .topnav a.active { background-color: #2196F3; color: white; } /* 設置搜索框容器的樣式 */ .topnav .search-container { float: right; } /* 設置導航欄內的輸入框的樣式 */ .topnav input[type=text] { padding: 6px; margin-top: 8px; font-size: 17px; border: none; } /* 設置搜索框容器內的按鈕樣式 */ .topnav .search-container button { float: right; padding: 6px; margin-top: 8px; margin-right: 16px; background: #ddd; font-size: 17px; border: none; cursor: pointer; } .topnav .search-container button:hover { background: #ccc; } /* 添加響應能力 - 在小屏幕上,垂直而不是水平顯示導航欄 */ @media screen and (max-width: 600px) { .topnav .search-container { float: none; } .topnav a, .topnav input[type=text], .topnav .search-container button { float: none; display: block; text-align: left; width: 100%; margin: 0; padding: 14px; } .topnav input[type=text] { border: 1px solid #ccc; } }