Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS for text input type in HTML form not working [duplicate]

Tags:

html

css

I have the below CSS for an HTML form:

input[type=text], input[type=password]  {
  transition: height 0.4s ease-in-out, width 0.4s ease-in-out, background 0.4s ease-in-out;
  padding: 18px;
  height: 20px;
  width: 150px;
  border-bottom: 2px solid #FFD800;
  border-top: 0px;
  border-right: 0px;
  border-left: 0px;
  background-color: #E2E2E2;
  color: dimgray;
}   
input[type=text], input[type=password]:focus {
  height: 30px;
  width: 200px;
  animation-name: smooth;
  background-color: #FFD800;
  animation-duration: 0.5s;
  animation: smooth 0.5s forwards;
  color: black;
}
<div class="body">
  <form action="" autocomplete="off" method="POST">
    <br><br><h2 align="center">Login</h2><br>
    <input type="text" placeholder="Username" name="Username"><br><br>
    <input type="password" placeholder="Password" name="Password"><br><br>
    <input type="submit" value="submit" name="submit">
  </form>
</div>

However, only the password field looks as it should.

Here is a snippet of what I see

Please advise. Thank you.

like image 209
Bob Avatar asked Dec 10 '20 12:12

Bob


People also ask

Can you have multiple labels for an input?

Assigning multiple labels to the same form field can cause problems for some combinations of screen readers and browsers, and the results are inconsistent from one combination to the next. Some combinations will read the first label. Some will read the last label. Others will read both labels.

How do you make an input unchangeable in HTML?

The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control. If the readonly attribute is specified on an input element, because the user can not edit the input, the element does not participate in constraint validation.

Can we change input type with CSS?

No. CSS is a presentation language. It cannot be used to alter the semantics and structure of a document. The closest you could come would be to determine which if a file and date input were visible in the document.

How to validate password length in html?

The maxLength property sets or returns the value of the maxlength attribute of a password field. The maxlength attribute specifies the maximum number of characters allowed in a password field. Tip: To set or return the width of a password field, in number of characters, use the size property.


1 Answers

Welcome to Stack Overflow and great first question including code.

You're missing the :focus on input[type=text] to add the styling when focusing on text input.

Change

input[type=text],
input[type=password]:focus

to

input[type=text]:focus,
input[type=password]:focus

and it will work.

Working example:

input[type=text],
input[type=password] {
  transition: height 0.4s ease-in-out, width 0.4s ease-in-out, background 0.4s ease-in-out;
  padding: 18px;
  height: 20px;
  width: 150px;
  border-bottom: 2px solid #FFD800;
  border-top: 0px;
  border-right: 0px;
  border-left: 0px;
  background-color: #E2E2E2;
  color: dimgray;
}

input[type=text]:focus,
input[type=password]:focus {
  height: 30px;
  width: 200px;
  animation-name: smooth;
  background-color: #FFD800;
  animation-duration: 0.5s;
  animation: smooth 0.5s forwards;
  color: black;
}
<div class="body">

  <form action="" autocomplete="off" method="POST">

    <br><br>
    <h2 align="center">Login</h2><br>
    <input type="text" placeholder="Username" name="Username"><br><br>
    <input type="password" placeholder="Password" name="Password"><br><br>
    <input type="submit" value="submit" name="submit">

  </form>

</div>
like image 163
Roy Avatar answered Oct 25 '22 06:10

Roy