简体   繁体   中英

Input field "placeholder" not working/showing

I'm a newbie front-end dev, I'm having trouble understanding why the placeholder is not working, I tried using LABEL tag but it won't disappear as I start typing into the text input. I am serving the HTML with express to heroku. here's the code:

          <!-- <label for="firstname">FirstName</label> -->
          <input type="text" class="form-control" id="firstName" name="firstname" placeholder="FirstName" required autofocus/>
        </div>
        <div class="form-floating">
          <!-- <label for="lastname">LastName</label> -->
          <input type="text" class="form-control" id="lastName" name="lastname" placeholder="LastName" required/>
        </div>
        <div class="form-floating">
          <!-- <label for="email">example@gmail.com</label> -->
          <input type="email" class="form-control" name="email" placeholder="example@gmail.com" required/>
        </div>
        <button class="w-100 btn btn-lg btn-primary" type="submit">
          Sign Up
        </button>
        <p class="mt-5 mb-3 text-muted">&copy; Example Inc.</p>

edit: I am using bootstrap for styling and Express and NodeJs for serving files to the frontend. here is the overall code for signup page:

<link href="../css/styles.css" rel="stylesheet" />
  </head>
  <body class="text-center">
    <main class="form-signin">
      <form method="post" action="/">
        <img
          class="mb-4"
          src="../images/example.png"
          alt=""
          width="72"
          height="57"
        />
        <h1 class="h3 mb-3 fw-normal">Sign up to my newsletter</h1>

        <div class="form-floating">
          <!-- <label for="firstname">FirstName</label> -->
          <input type="text" class="form-control" id="firstName" name="firstname" placeholder="FirstName" required autofocus/>
        </div>
        <div class="form-floating">
          <!-- <label for="lastname">LastName</label> -->
          <input type="text" class="form-control" id="lastName" name="lastname" placeholder="LastName" required/>
        </div>
        <div class="form-floating">
          <!-- <label for="email">example@gmail.com</label> -->
          <input type="email" class="form-control" name="email" placeholder="example@gmail.com" required/>
        </div>
        <button class="w-100 btn btn-lg btn-primary" type="submit">
          Sign Up
        </button>
        <p class="mt-5 mb-3 text-muted">&copy; Example Inc.</p>
      </form>
    </main>
  </body>

and minimal styling used in stylesheet:

html,
body {
  height: 100%;
}

body {
  display: flex;
  align-items: center;
  padding-top: 40px;
  padding-bottom: 40px;
  background-color: #f5f5f5;
}

.form-signin {
  width: 100%;
  max-width: 330px;
  padding: 15px;
  margin: auto;
}


.form-signin .form-floating:focus-within {
  z-index: 2;
}

.form-signin #firstName {
  margin-bottom: -1px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 0;

}
.form-signin #lastName {
  margin-bottom: -1px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

.form-signin input[type="email"] {
  margin-bottom: 10px;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

This is how it is displayed on the web app

在此处输入图像描述

As Phil said in the comment to your post the issue is that you're using Bootstrap Floating Labels (by wrapping your inputs with a .form-floating) but you're not supplying labels (yours are commented out).

<div class="form-floating">
    <label for="firstname">FirstName</label>
    <input 
        type="text"
        class="form-control"
        id="firstName" 
        name="firstname" 
        placeholder="FirstName" 
        required 
        autofocus/>
</div>

Alternatively you can remove the .form-floating surrounding div and use the placeholders as normal

<div>
    <label for="firstname">FirstName</label>
    <input 
        type="text"
        class="form-control"
        id="firstName" 
        name="firstname" 
        placeholder="FirstName" 
        required 
        autofocus/>
</div>

Here they are uncommented and working correctly: on codepen

从 div 标签中删除类form-floating它将起作用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM