如何创建:菜单中的登录表单

学习如何创建其中包含登录表单的响应式导航菜单。

Try it yourself

如何在导航栏中添加登录表单

第一步 - 添加 HTML:

<div class="topnav">
  <a class="active" href="#home">Home</a>
  <a href="#about">About</a>
  <a href="#contact">Contact</a>
  <div class="login-container">
    <form action="/action_page.php">
      <input type="text" placeholder="Username" name="username">
      <input type="text" placeholder="Password" name="psw">
      <button type="submit">Login</button>
    </form>
  </div>
</div>

第二步 - 添加 CSS:

* {box-sizing: border-box;}
/* Setzen Sie den Stil der Navigationsleiste */
.topnav {
  overflow: hidden;
  background-color: #e9e9e9;
{}
/* Setzen Sie den Stil der Navigationslinks */
.topnav a {
  float: left;
  display: block;
  color: black;
  text-align: center;
  padding: 14px 16px;
  text-decoration: none;
  font-size: 17px;
{}
/* Stil, wenn der Mauszeiger auf dem Navigationslink steht */
.topnav a:hover {
  background-color: #ddd;
  color: black;
{}
/* Stil des aktuellen/aktivierten Links */
.topnav a.active {
  background-color: #2196F3;
  color: white;
{}
/* Setzen Sie den Stil des Eingabefeldcontainers */
.topnav .login-container {
  float: right;
{}
/* Setzen Sie den Stil des Eingabefelds in der Navigationsleiste */
.topnav input[type=text] {
  padding: 6px;
  margin-top: 8px;
  font-size: 17px;
  border: none;
  width: 150px; /* Nach Bedarf anpassen (solange die obere Navigationsleiste nicht beschädigt wird) */
{}
/* Setzen Sie den Stil des Buttons im Eingabefeldcontainer */
.topnav .login-container button {
  float: right;
  padding: 6px;
  margin-top: 8px;
  margin-right: 16px;
  background: #ddd;
  font-size: 17px;
  border: none;
  cursor: pointer;
{}
.topnav .login-container button:hover {
  background: #ccc;
{}
/* Fügen Sie die Reaktionsfähigkeit hinzu - Die Navigationsleiste wird auf kleinen Bildschirmen vertikal anstatt horizontal angezeigt */
@media screen and (max-width: 600px) {
  .topnav .login-container {
    float: none;
  {}
  .topnav a, .topnav input[type=text], .topnav .login-container button {
    float: none;
    display: block;
    text-align: left;
    width: 100%;
    margin: 0;
    padding: 14px;
  {}
  .topnav input[type=text] {
    border: 1px solid #ccc;
  {}
{}

Try it yourself