I'm making a form for a website and want the border of the fields to change color when the field is filled with text. I was thinking I could simply do it with adding an :active state but that doesn't seem to work...
Here is my code: https://jsfiddle.net/3uczn2bw/1/
HTML
<form>
<label for="fname">First name:</label><br>
<input type="text" placeholder="First name" id="fname" name="fname">
<label for="lname">Last name:</label><br>
<input type="text" placeholder="Last name" id="lname" name="lname">
</form>
CSS
input {
border: none;
outline: none!important;
padding: 10px 20px;
width: 300px;
}
input[type=text] {
color: #1a1a1a;
border-bottom: 4px solid #1a1a1a;
font-size: 24px;
font-weight: 900;
}
input[type=text]:focus {
border-bottom: 4px solid #178926;
}
To achieve this you need to add some scripts. I have given the script below by using this you will achieve your goal.
In script add below code:
document.querySelectorAll('input').forEach((input) => {
input.addEventListener('blur', function() {
this.classList.toggle('green', this.value.length > 0);
});
});
In CSS add below code:
.green {
border: 2px solid blue;
}
by using this CSS property you can manipulate the border properties.
This Code is useful for all input elements.
Related
In the example below, if you keyboard navigate to the checkbox, I get the custom focus state I'm looking for. However, I don't want the focus to show on click. I only want to show the focus state when it's keyboard navigated to.
How do I remove the focus state if clicked on?
input[type=checkbox]:focus, input[type=checkbox]:focus-visible {
outline: none;
}
label:focus-within {
outline: 2px solid #005fec;
outline-offset: 4px;
z-index: 10;
}
<label for="check1">
<input type="checkbox" name="check1" id="check1">
<span>Check Here</span>
</label>
Try this!
input[type=checkbox], input[type=checkbox] {
outline: none;
}
<label for="check1">
<input type="checkbox" name="check1" id="check1">
<span>Check Here</span>
</label>
To avoid showing the outline on focus you have to use: outline: none; (https://developer.mozilla.org/de/docs/Web/CSS/outline). In your case you have to add outline: none; only to the class label:focus-within.
label:focus-within {
outline: 2px solid #005fec;
outline-offset: 4px;
z-index: 10;
outline: none;
}
<label for="check1">
<input type="checkbox" name="check1" id="check1">
<span>Check Here</span>
</label>
I changed it to the css solution, but I need to modify the html structure, take the input out of the lable, and modify the css like this
input[type=checkbox]:focus, input[type=checkbox]:focus-visible {
outline: none;
}
input[type=checkbox]:focus, input[type=checkbox]:focus-visible {
outline: none;
}
/* I just added for here for debugging convenience, and now I have removed it
label[for='check2']
*/
label{
padding-left: 25px;
margin-left: -25px;
}
input[type=checkbox]:focus-visible + label{
outline: 2px solid #005fec;
outline-offset: 4px;
z-index: 10;
}
<input type="checkbox" name="check2" id="check2">
<label for="check2">
<span>Check Here2</span>
</label>
I have the following code:
<div class="form-group">
<div class="check-box">
<input class="form-control input-text checkbox" data-val="true" data-val-required="The Contact Branch Specific Email field is required." id="UseBranchSpecificContact" name="UseBranchSpecificContact" type="checkbox" value="true"><input name="UseBranchSpecificContact" type="hidden" value="false">
<label for="UseBranchSpecificContact">Contact Branch Specific Email</label>
</div>
</div>
Which displays the following checkbox:
The checkbox is displayed using the following CSS:
.check-box label:before {
position: absolute;
left: 0;
top: 2px;
border: 1px solid #dcdde3;
content: '';
font-family: FontAwesome;
line-height: 14px;
color: #3d4964;
font-size: 11px;
text-align: center;
cursor: pointer;
display: block;
width: 16px;
height: 16px;
}
So the actual checkbox (input) is hidden and the above CSS adds a checkbox before the label. This is all working as expected.
I wanted to add an extra CSS class to change the color of displayed checkbox to light grey, if the label has readonly attribute. So I have added the following CSS:
.check-box label:read-only:before {
background-color: #eeeeee;
}
But the problem is the above CSS affects the labels which do not have readonly attribute as well, I am not understanding why is that?
'Label' doesn't have 'readonly' attribute. As well, 'readonly' doesn't work with checkbox input type. You can use 'disabled' for checkbox.
var button = document.getElementById("toggleBtn");
var input = document.getElementById("UseBranchSpecificContact");
button.addEventListener("click", function(){
input.toggleAttribute("disabled");
});
.check-box label:before {
position: absolute;
left: 0;
top: 2px;
border: 1px solid #dcdde3;
content: '';
font-family: FontAwesome;
line-height: 14px;
color: #3d4964;
font-size: 11px;
text-align: center;
cursor: pointer;
display: block;
width: 16px;
height: 16px;
}
.check-box input:disabled ~ label:before {
background-color: red;
}
<div class="form-group">
<div class="check-box">
<input class="form-control input-text checkbox" data-val="true" data-val-required="The Contact Branch Specific Email field is required." id="UseBranchSpecificContact" name="UseBranchSpecificContact" type="checkbox" value="true">
<input name="UseBranchSpecificContact" type="hidden" value="false">
<label for="UseBranchSpecificContact">Contact Branch Specific Email</label>
</div>
</div>
<button id="toggleBtn">Toggle Disable</button>
Ref: Can HTML checkboxes be set to readonly?
Tag 'label' doesn't have 'readonly' attribute. See html spec. If you want to change color of label, when input has 'readonly' attribute, than:
.check-box input[readOnly] + label {
...
}
I want to keep the background color of the input field as 'green', the problem is that when I click on it to write something, the color changes to default white again, here is my CSS code:
input{
width: 100%;
color: $white;
border: none;
border-bottom: 1px solid $grey-light;
outline: none;
font-size: 1rem;
margin-bottom: 1.25rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
background-color: $green;
}
the HTML React part:
<form className="form">
<input type="text" name="name" className="form__name" placeholder="Name*"/>
<input type="email" name="email" className="form__email" placeholder="Email*"/>
</form>
You could use input:focus to keep the background green while typing. Make sure you do not have other css which is overriding this css.
Demo:
input {
width: 100%;
color: white;
border: none;
border-bottom: 1px solid grey-light;
outline: none;
font-size: 1rem;
margin-bottom: 1.25rem;
padding-top: 0.5rem;
padding-bottom: 0.5rem;
background-color: green;
}
input:focus {
background: green;
}
<form class="form">
<input type="text" name="name" class="form__name" placeholder="Name*" />
<input type="email" name="email" class="form__email" placeholder="Email*" />
</form>
I think you have to use the :focus Property.
So like that:
input:focus {
background: #161616;
}
Use input:focus below input to write your css rules for the focus scenario.
I'm trying to make an input autoresize to fit content (not bigger or smaller than needed)
form {
white-space:nowrap;
background: #ddd;
border: 1px solid #ccc;
margin:10px;
padding: 5px;
display:inline-block;
}
input, button {
display:inline-block;
}
input {
max-width: none;
min-width: 0;
width: auto;
-moz-appearance: none;
}
<form>
<button>-</button>
<input type="text" value="0"> minutes
<button>+</button>
</form>
<form>
<button>-</button>
<input type="text" value="500"> minutes
<button>+</button>
</form>
But width auto doesn't do the trick, at least in firefox,
Any idea why?
Well,
As I don't see a css only solution, this a solution involving javascript
(but refering the original question, I would like not to use js if posible..)
$('input').on('change keyup', function() {
var fontsize = parseInt($(this).css('font-size'));
var value = $(this).val();
var length = value.length;
$(this).width(fontsize*length))
});
form {
white-space:nowrap;
background: #ddd;
border: 1px solid #ccc;
margin:10px;
padding: 5px;
display:inline-block;
}
input, button {
display:inline-block;
}
input {
width: 10px;
padding: 0;
font-size: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<button>-</button>
<input type="text" value="0"> minutes
<button>+</button>
</form>
Have you a live example?
This should be able to be done with CSS and not using JS but would need to be able to check the rest of the code to see that this would fit and work correctly
I have an issue on styling different form fields with CSS
I have a CSS code:
#form input {border: 1px solid #FFFFFF;font-size: 16px;padding: 5px;width: 200px;}
Now this code styles all three of the Input fields I have (below) but also styles the image submit button I have
<input type="text" class="text" name="email" id="email">
<input type="password" value="" name="password" id="password">
<input name="button" type="image" src="go.jpg" alt="Submit" align="right">
So then I change the CSS and create:
#form input.text {border: 1px solid #FFFFFF;font-size: 16px;padding: 5px;width: 200px;}
#form input.button {border: 0px)
This prevents the CSS styling of the image submit button but now it is not styling the password field - so I tried:
#form input.text {border: 1px solid #FFFFFF;font-size: 16px;padding: 5px;width: 200px;}
#form input.password {border: 1px solid #FFFFFF;font-size: 16px;padding: 5px;width: 200px;}
#form input.button {border: 0px)
But this had no affect.
So the question is, how can I effectively allow the CSS styling on the input text and input password fields - but not to style the image submit button?
Thanks in advance!
What you need to research is css selectors: CSS selector for text input fields?.
#form input[type="text"] {
border: 1px solid #FFFFFF;
font-size: 16px;
padding: 5px;width: 200px;
}
#form input[type="password"] {
border: 1px solid #FFFFFF;
font-size: 16px;
padding: 5px;
width: 200px;
}
#form input[type="button"] {
border: 0px;
}
There is an option you can add
For your text fields
#form input[type=text] {}
For your password fields
#form input[type=password] {}
For your button fields
#form input[type=button] {}
Or just add a class to your password field, which is password.
Use the type of the input as part of your selector:
#form input[type="text"] {
border: 1px solid #FFFFFF;
font-size: 16px;
padding: 5px;
width: 200px;
}
This is the attribute selector.
You could always just add a class for your input boxes:
<input type="text" class="styled" name="email" id="email">
<input type="password" value="" class="styled" name="password" id="password">
With:
#form input.styled {
border: 1px solid #FFFFFF;
font-size: 16px;
padding: 5px;
width: 200px;
}
You're missing the .password and .button classes on your html.
<input type="text" class="text" name="email" id="email">
<input type="password" class="password" value="" name="password" id="password">
<input name="button" class="button" type="image" src="go.jpg" alt="Submit" align="right">
You didn't put a class on your inputs? .passwords means class="password" in your html
You could do this
Do not style the input element on its own, but instead target only those elements you want.
First, get rid of this css
#form input {/* styles here*/}
Then do this
#form input.text, #form input#password{
border: 1px solid #FFFFFF;
font-size: 16px;
padding: 5px;
width: 200px;
}
The comma separates the two elements but applies the css to each.
Then you don't need to "reset" the border on the image submit button.
By the way, your password input has an id according to your code and not a class, so you need the # instead of the ..
FORM "inputs" in CSS:
Text input:
input[type="text"] {...} /* normal (one row) text input */
textarea {...} /* multiline text input */
input[type="password"] {...} /* password masked input */
Button:
input[type="button"] {...}
input[type="submit"] {...} /* type of button - submitting the form */
input[type="reset"] {...} /* clean (reset) whole form "inputs" */
Checkbox:
input[type="checkbox"] {...}
Radiogroup:
input[type="radio"] {...}
Dropdown list:
select {...}
select optgroup{...} /* group od values in dropdown list */
select option{...} /* value to choose in dropdown list */
just remove second line on your css and add "text" class to your password type input. CORRECT VERSION BELOW:
CSS:
#form input.text {border: 1px solid #FFFFFF;font-size: 16px;padding: 5px;width: 200px;}
#form input.button {border: 0px)
HTML:
<input type="text" class="text" name="email" id="email">
<input type="password" value="" name="password" id="password">
<input name="button" type="image" src="go.jpg" alt="Submit" align="right">