Can you style HTML input elements filled by browser? - html

I have ASP.NET MVC app with login page:
<form id="account" method="post">
<h2>Use email account to log in.</h2>
<hr />
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-floating">
<input asp-for="Input.Email" class="form-control" autocomplete="username" aria-required="true"/>
<label asp-for="Input.Email" class="form-label"></label>
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
<div class="form-floating" >
<input asp-for="Input.Password" class="form-control" autocomplete="current-password" aria-required="true" />
<label asp-for="Input.Password" class="form-label"></label>
<span asp-validation-for="Input.Password" class="text-danger"></span>
</div>
<div>
<div class="checkbox form-row">
<label asp-for="Input.RememberMe" class="form-label">
<input class="form-check-input" asp-for="Input.RememberMe" />
#Html.DisplayNameFor(m => m.Input.RememberMe)
</label>
</div>
</div>
<div class="form-row">
<button id="login-submit" type="submit" class="w-100 btn btn-lg btn-primary">Log in</button>
</div>
It uses bootstrap classes. It looks nice with dark theme:
But once the browser (tried Chrome and Edge) pre-filled the form it becomes ugly and I was told its by design.
Is there any way I can change the pre-filled form elements colours?
EDIT: as suggested I looked up for autofill and found only these:
.form-floating > .form-control:-webkit-autofill {
padding-top: 1.625rem;
padding-bottom: .625rem
}
.form-floating > .form-select {
padding-top: 1.625rem;
padding-bottom: .625rem
}
.form-floating > .form-control: not(:-moz-placeholder-shown)~label {
opacity: .65;
transform: scale(.85)translateY(-.5rem)translateX(.15rem)
}
.form-floating > .form-control: focus~label, .form-floating > .form-control: not(:placeholder-shown)~label, .form-floating > .form-select~label {
opacity: .65;
transform: scale(.85)translateY(-.5rem)translateX(.15rem)
}
.form-floating > .form-control:-webkit-autofill~label {
opacity: .65;
transform: scale(.85)translateY(-.5rem)translateX(.15rem)
}
so I added the following css to override it but it has no effect:
.form-floating > .form-control:-webkit-autofill {
background-color: #dedede;
border-color: #2a2a2a;
color: #2a2a2a;
}

Apparently the style I was looking for is this:
.form-control:disabled, .form-control[readonly] {
background-color: #dedede;
border-color: #2a2a2a;
color: #2a2a2a;
}
Thanks to #epascarello and #vee for help

Related

css focus on input field which belongs to a div is not working

I'm trying to customize the style of the login form, but I can't apply the :focus rule. Could someone be so kind as to explain to me why it's not working? I don't understand what I'm doing wrong, sorry, but I'm new to all this.
/*Label Style*/
.login_form_fields input:focus + label {
color: blue;
}
/*Input Style*/
.login_form_fields input {
border-radius: 2px;
background: #fff;
border: 1px solid #E3E3E3;
}
.login_form_fields input:focus {
border: 1px solid blue;
}
<form id="login-form" method="post">
<div class="login_form_fields uname">
<label for="username">Username o Email:</label>
<input type="text" id="username" name="username" required>
</div>
<div class="login_form_fields pswrd">
<label for="password">Password:</label>
<input type="password" id="password" name="password" required>
</div>
<button class="login_button" type="submit">Accedi</button>
</form>
</div>

Aligning form input elements of varying lengths

I need to create a form using only HTML and CSS. The form fields are of different lengths depending on which field it is.
I've tried just about every solution I've found on SO and other sites that use things like display modes but those all seem to be aimed at forms where all the input fields are the same size. I've also tried to use a table to make the form but I don't know enough about them to make that work. I am still open to using a table if anyone is willing to teach me how to have it work.
form {
margin-top: 25px;
}
.form {
margin-left: 10px;
background-color: #fff;
float: left;
width: 620px;
}
.form p {
margin-left: 10px;
}
.form h3,
.summary h3 {
font-size: 36px;
background-color: #fff;
width: auto;
padding-top: 35px;
}
.form label {
font-family: Arial, sans-serif;
font-size: 14px;
display: block;
margin-left: 10px;
color: #8f8f8f;
}
input {
float: left;
margin-left: 10px;
background-color: #f9f9f9;
border: 1px solid #cdcdcd;
height: 36px;
border-radius: 2px;
}
.form span {
color: #861919;
}
.name {
width: 288px;
}
.bigbar {
width: 448px;
}
.smallbar {
width: 128px;
}
<form action="#" method="POST">
<label for="firstname">First Name <span>*</span></label>
<input type="text" id="firstname" required class="name" />
<label for="lastname">Last Name <span>*</span></label>
<input type="text" id="lastname" required class="name" />
<label for="address">Street Address <span>*</span></label>
<input type="text" id="address" required class="bigbar" />
<label for="apt">Apt/Unit/Suite #</label>
<input type="text" id="apt" class="smallbar" />
<label for="city">City <span>*</span></label>
<input type="text" id="city" required class="bigbar" />
<label for="province">Province <span>*</span></label>
<input type="text" id="province" required class="smallbar" />
<label for="code">Postal Code <span>*</span></label>
<input type="text" id="code" required class="smallbar" />
<label for="phone">Phone Number <span>*</span></label>
<input type="tel" id="phone" required class="bigbar" />
<button type="submit" id="submit">Continue Checkout</button>
</form>
At the end it's supposed to look like this: https://puu.sh/DQhyH/2aed3ce204.png
but as far as I'm able to get it with my knowledge it looks like this: https://puu.sh/DQhAs/eb0a1eeb5b.png (can't post images due to just making my account)
so it's almost there but the fields aren't aligned properly.
This could be done fairly easy with a table using colspan attribute on td elements. However, tables are intended for the display of data, and should be not used for layout (this article lists several reasons why).
You need to look at your desired result and form a grid with containing div elements. You have several "rows", so put the content of each row inside a div.row that takes the full width. Your content within the rows is either 1,2, or 3 "columns" wide out of 4 total. So you make div.col1, div.col2, div.col3 with appropriate widths and inline-block display. Then just make sure you put four columns in each row. The containing divs now determine the widths so we set inputs and labels to width:100% to take the full width of their respective parents.
div.field {
display: inline-block;
padding: .5em;
}
div.row,
input,
label,
button {
width: 100%;
}
form {
margin-top: 25px;
width: 620px;
}
form label {
font-family: Arial, sans-serif;
font-size: 14px;
display: block;
margin-left: 10px;
color: #8f8f8f;
}
input {
/*float: left;*/
margin-left: 10px;
background-color: #f9f9f9;
border: 1px solid #cdcdcd;
height: 36px;
border-radius: 2px;
}
form span {
color: #861919;
}
.col2 {
width: 288px;
}
.col3 {
width: 448px;
}
.col1 {
width: 128px;
}
<form action="#" method="POST">
<div class="row">
<div class="col2 field">
<label for="firstname">First Name <span>*</span></label>
<input type="text" id="firstname" required/>
</div>
<div class="col2 field">
<label for="lastname">Last Name <span>*</span></label>
<input type="text" id="lastname" required/>
</div>
</div>
<div class="row">
<div class="col3 field">
<label for="address">Street Address <span>*</span></label>
<input type="text" id="address" required class="bigbar" />
</div>
<div class="col1 field">
<label for="apt">Apt/Unit/Suite #</label>
<input type="text" id="apt" />
</div>
</div>
<div class="row">
<div class="col3 field">
<label for="city">City <span>*</span></label>
<input type="text" id="city" required/>
</div>
<div class="col1 field">
<label for="province">Province <span>*</span></label>
<input type="text" id="province" required/>
</div>
</div>
<div class="row">
<div class="col1 field">
<label for="code">Postal Code <span>*</span></label>
<input type="text" id="code" required/>
</div>
<div class="col3 field">
<label for="phone">Phone Number <span>*</span></label>
<input type="tel" id="phone" required/>
</div>
</div>
<div class="row">
<div class="col1 field"></div>
<div class="col2 field">
<button type="submit" id="submit">Continue Checkout</button>
</div>
<div class="col1 field"></div>
</div>
</form>

How can I get the input label to fade when I focus or select the input & can I also get the label to be inside the input?

I am trying to do 2 things here:
1) I want the label to change to a lighter shade of grey when the input is selected. However, I can't get all the labels to change color.
2) I want to put the label inside the input. However, I am more concerned about the first than the second, as by implementing the second one would cancel out the first one I believe. But it would be nice to know how to position a label inside an input.
I am using the CSS input:focus ~ label method however, this just selects the labels AFTER the input and I have the label before the input therefore the first label is not working for the fade.
How can I get all the labels to change color when an input is selected? I know I need to move the label after the input however, if I do that the label will show up underneath the input which is not where it is supposed to go.
I have tried position: absolute on the label but it just bunches them together, so I am not sure how to move them individually.
Here is the link to my codepen, I have two contact forms, one separated with divs the other is not.
https://codepen.io/RJorns/pen/MdmoRp
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css">
<div class="contact-wrapper">
<div class="contact-title"><h1>Contact</h1></div>
<form class="contact-form ui equal width form">
<div class="form-wrapper">
<label class="form-label">Name</label>
<input class="form-input" id="name" type="text" placeholder="Name" required>
</div>
<div class="form-wrapper">
<label class="form-label">Email</label>
<input class="form-input" id="email" type="text" placeholder="Email" required>
</div>
<label class="form-label">Message</label>
<div class="field">
<textarea rows="4" id="message" type="text" required></textarea>
</div>
<button id="submit" class="ui grey button">Submit</button>
</form>
</div>
<div class="contact-wrapper">
<div class="contact-title"><h1>Contact</h1></div>
<form class="contact-form ui equal width form">
<label class="form-label">Name</label>
<input class="form-input" id="name" type="text" placeholder="Name" required>
<label class="form-label">Email</label>
<input class="form-input" id="email" type="text" placeholder="Email" required>
<label class="form-label">Message</label>
<div class="field">
<textarea rows="4" id="message" type="text" required></textarea>
</div>
<button id="submit" class="ui grey button">Submit</button>
</form>
</div>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
text-align: center;
}
/* Contact */
.contact-wrapper {
margin: 0 auto;
max-width: 75%;
}
.contact-title {
margin-bottom: 2em;
}
.contact-form {
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: center;
align-content: center;
}
input {
box-shadow: inset 0px 0px 13px -1px rgba(77, 77, 77, 1);
}
.form-label {
display: flex;
color: #515151;
}
input:focus ~ label {
color: #a0a0a0;
}
button {
margin-top: 5em;
}
I want the all the labels to show a lighter grey when it is focused.
I want to use CSS only but if I have to use JS that is fine.
I just moved the label after the input for label section from css and used the column reverse to move the label on top. Also added the transition on input focus. I hope this will work. Comment for more info.
* {
padding: 0;
margin: 0;
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
text-align: center;
}
/* Contact */
.contact-wrapper {
margin: 0 auto;
max-width: 75%;
}
.contact-title {
margin-bottom: 2em;
}
.contact-form {
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: center;
align-content: center;
}
input {
box-shadow: inset 0px 0px 13px -1px rgba(77, 77, 77, 1);
}
.form-label {
display: flex;
color: #515151;
transition: all 0.4s ease-in;
}
input:focus ~ label {
color: #a0a0a0;
transition: all 0.4s ease-in;
}
.form-wrapper {
display: flex;
flex-direction: column-reverse;
}
button {
margin-top: 5em;
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.4.1/semantic.min.css">
<div class="contact-wrapper">
<div class="contact-title">
<h1>Contact</h1></div>
<form class="contact-form ui equal width form">
<div class="form-wrapper">
<input class="form-input" id="name" type="text" placeholder="Name" autocomplete="off" required>
<label class="form-label">Name</label>
</div>
<div class="form-wrapper">
<input class="form-input" id="email" type="text" placeholder="Email" autocomplete="off" required>
<label class="form-label">Email</label>
</div>
<label class="form-label">Message</label>
<div class="field">
<textarea rows="4" id="message" type="text" required></textarea>
</div>
<button id="submit" class="ui grey button">Submit</button>
</form>
</div>
<div class="contact-wrapper">
<div class="contact-title">
<h1>Contact</h1></div>
<form class="contact-form ui equal width form">
<div class="form-wrapper">
<input class="form-input" id="name" type="text" placeholder="Name" autocomplete="off" required>
<label class="form-label">Name</label>
</div>
<div class="form-wrapper">
<input class="form-input" id="email" type="text" placeholder="Email" autocomplete="off" required>
<label class="form-label">Email</label>
</div>
<label class="form-label">Message</label>
<div class="field">
<textarea rows="4" id="message" type="text" required></textarea>
</div>
<button id="submit" class="ui grey button">Submit</button>
</form>
</div>

Blocks besides each others

I have a given HTML which I can't touch:
<fieldset>
<label for="username" class="text">
Dein Benutzername<span class="req">*</span>
</label>
<div class="div_text">
<input name="username" type="text" id="username" value="" class="textbox" required="">
</div>
<label for="first_name" class="text">
Vorname<span class="req">*</span>
</label>
<div class="div_text">
<input name="first_name" type="text" id="first_name" value="" class="textbox" required="">
</div>
<label ....
and I need to output that one with 2 same-width columns where the label is above the corresponding div-block and the next input-div-block and their corresponding label is besides it.
I added an image to show exactly what I want it to be. I run into problems because the label-tag sits within the same hierarchy-level as the div-block and I therefore can't use inline-block nor - due to my lack of knwoledge - flexbox as it's a mix between column and row in flex-direction
Could somebody please guide me on that one?
You can still use flexbox and put the elements into a new order. It is not a elegant way but the only solution i can
think of if you cant touch the HTML
.text,
.div_text {
width: 50%;
display: block;
}
fieldset {
display: flex;
flex-wrap: wrap;
}
label {
display: block;
}
label[for=username] {
order: 1;;
}
label[for=first_name] {
order: 2;
}
fieldset div:nth-of-type(1){
order: 3;
}
fieldset div:nth-of-type(2){
order: 4;
}
label[for=sur_name] {
order: 5;
}
label[for=example] {
order: 6;
}
fieldset div:nth-of-type(3){
order: 7;
}
fieldset div:nth-of-type(4){
order: 8;
}
<fieldset>
<label for="username" class="text">Dein Benutzername<span class="req">*</span></label>
<div class="div_text">
<input name="username" type="text" id="username" value="" class="textbox" required="">
</div>
<label for="first_name" class="text">Vorname<span class="req">*</span></label>
<div class="div_text">
<input name="first_name" type="text" id="first_name" value="" class="textbox" required="">
</div>
<label for="sur_name" class="text">Nachname<span class="req">*</span></label>
<div class="div_text">
<input name="sur_name" type="text" id="sur_name" value="" class="textbox" required="">
</div>
<label for="example" class="text">Weiters Beispiel<span class="req">*</span></label>
<div class="div_text">
<input name="example" type="text" id="example" value="" class="textbox" required="">
</div>
</fieldset>
Hoping you are not using any css framework, please have a look at the below-working snippet with custom markup and css, hope it helps :)
body {
font-size: 14px;
font-family: sans-serif;
}
fieldset {
clear: both;
max-width: 500px;
border: 1px dashed deeppink;
}
legend {
color: #fff;
font-size: 14px;
padding: 5px 10px;
background: deeppink;
margin: 0 0 10px 15px;
}
.form-group {
width: 45%;
float: left;
padding: 0 2.5%;
}
label {
display: block;
margin-bottom: 5px;
}
input[type="text"] {
width: 100%;
font-size: 14px;
padding: 5px 3px;
margin-bottom: 10px;
border: 1px solid #000;
box-sizing: border-box;
}
<fieldset>
<legend>Fieldset Container</legend>
<div class="form-group">
<label for="labelX">Label x</label>
<input id="labelX" type="text" placeholder="Label X" />
</div>
<div class="form-group">
<label for="labelY">Label y</label>
<input id="labelY" type="text" placeholder="Label Y" />
</div>
<div class="form-group">
<label for="labelZ">Label z</label>
<input id="labelZ" type="text" placeholder="Label Z" />
</div>
<div class="form-group">
<label for="labelXX">Label xx</label>
<input id="labelXX" type="text" placeholder="Label XX" />
</div>
</fieldset>

Floating label on top inputs

I am trying to float a label on top of the input but it's not working correctly? Maybe I messed up the CSS? Help would be greatly appreciated, thanks!
Heres what it looks like at the moment:
http://prntscr.com/37hw30
HTML:
<form id="loginformitem" name="loginformitem" method="post" style="float:right">
<label for="username">Username</label>
<input type="text" name="log_username" class="logintext" id="username" placeholder="Username" style="margin-left:100px">
<label for="password">Password</label>
<input type="password" name="log_password" class="logintext" id="password" placeholder="Password">
<button type="submit" class="loginbutton" name="login" >Login now</button>
</form>
CSS:
.logintext {
margin-right: 10px;
border: 2px solid rgba(0,0,0,0);
border-radius: 2px;
padding: 2px 2px 3px 2px;
font-size: 13px;
color: #222;
}
form label {
font-size: 13px;
font-weight: bold;
color: #a4c0d7;
}
you can wrap label & input into a <p> or a <span> displayed as inline-block.
input can be displayed as block to break the line.
HTML
<form id="loginformitem" name="loginformitem" method="post" >
<span>
<label for="username">Username</label>
<input type="text" name="log_username" class="logintext" id="username" placeholder="Username" >
</span>
<span>
<label for="password">Password</label>
<input type="password" name="log_password" class="logintext" id="password" placeholder="Password">
</span>
<button type="submit" class="loginbutton" name="login" >Login now</button>
</form>
CSS
form span {
display:inline-block;
margin:0 50px;
vertical-align:bottom;
}
span input {
display:block;/* if you want submit under , do form input {} to include it too */
}
form {
float:right;
/* for the demo */
border:solid;
border-radius:1em;
padding:0.25em;
}
Turns to look like DEMO
try this
#loginformitem input , #loginformitem label {
clear : left ,
float : left
}