Input field animation problem after filling the details - html

Hey everyone hope you all doing great, I am looking for some help with css part for my reactjs application input fields. Can someone solve it for me.
My Problem is that I want to make an animated input field in a way that the placeholder will indicate the input details, so on focusing to the input field the placeholder will move to the top of input field and I will be able to fill the details. But after focusing the pointer outside the input field after filling the details the placeholder gets overlapped. How can I solve the problem of removing it from overlapping.
Here are the images for reference
This is App.js:
<input className='input_field' type="number" name="number" placeholder=''
autoComplete='off'
value={formValues.number}
onChange={handleChange}
style={{width:"180px",height:"35px"}}
/>
<label className='input_label'>Mobile Number</label>
This is the corresponding App.css:
.input_field{
width: 100%;
height: 40px;
border: none;
border-bottom: 2px solid black;
outline: none;
font-size: 16px;
}
.input_label{
position: absolute;
left: 605px;
top: 296px;
pointer-events: none;
font-size: 16px;
}
.input_field:focus{
border-color: crimson;
}
.input_field:focus + .input_label{
top: 275px;
transition: 0.3s;
color: crimson;
}
I have tried adding this
.input_field:valid + .input_label
along with
.input_field:focus + .input_label{
top: 275px;
transition: 0.3s;
color: crimson;
}
but nothing is changed
Can someone please help me solving the problem I faced?

.input_field{
width: 100%;
height: 40px;
border: none;
border-bottom: 2px solid black;
outline: none;
font-size: 16px;
}
.input_label{
position: absolute;
left: 20px;
top: 20px;
pointer-events: none;
font-size: 16px;
}
.input_field:focus{
border-color: crimson;
}
.input_field::placeholder {
color: #fff;
opacity: 1; /* Firefox */
}
.input_field:-ms-input-placeholder {
color: #fff;
}
.input_field::-ms-input-placeholder {
color: #fff;
}
.input_field:not(:placeholder-shown) ~ label {
top: 275px;
transition: 0.3s;
color: crimson;
}
<input className='input_field' type="number" name="number" placeholder='enter number'
autoComplete='off'
value={formValues.number}
onChange={handleChange}
style={{width:"180px",height:"35px"}}
/>
<label className='input_label'>Mobile Number</label>
//You have to add placeholder text and try below css

Related

How to create an animated email input when user clicks on it using CSS

I want to create a cool animation that when the user clicks on the email input or the password the label of the input will go up and have a nice transition in the bottom border.
This is what I have:
And this is what I want to create:
My code:
.form {
position: relative;
display: flex;
flex-direction: column;
margin-bottom: 1rem;
}
.input {
background: none;
color: #c6c6c6;
font-size: 1.8rem;
padding: 1.6rem;
display: block;
border: none;
border-bottom: 1px solid #c6c6c6;
width: 100%;
}
.label {
position: absolute;
color: #c6c6c6;
font-size: 1.6rem;
left: 0.5rem;
top: 1rem;
}
<div class="form">
<input class="email input" type="email" name="email" />
<label class="label" for="email">Email Address</label>
</div>
<div class="form">
<input class="password input" type="password" name="password" />
<label class="label" for="password">Password</label>
</div>
I've searched a lot but every code example I found was with SCCS, SASS and I don't understand it. So please help me with plain CSS. Any help would be greatly appreciated!
I created an animated demo using HTML and CSS.
.main {
width: 500px;
margin: 50px 100px;
;
}
.form-group {
position: relative;
margin-bottom: 45px;
}
input {
display: block;
width: 300px;
font-size: 14pt;
padding: 10px;
border: none;
border-bottom: 1px solid #ccc;
}
input:focus {
outline: none;
}
label {
position: absolute;
top: 10px;
left: 5px;
color: #999;
font-size: 14pt;
font-weight: normal;
pointer-events: none;
transition: all 0.2s ease;
}
input:focus ~ label,
input:valid ~ label {
top: -20px;
font-size: 10pt;
color: #5264AE;
}
.bar {
display: block;
position: relative;
width: 320px;
}
.bar:before,
.bar:after {
content: "";
height: 2px;
width: 0;
bottom: 1px;
position: absolute;
background: #5264AE;
transition: all 0.2s ease;
}
.bar:before {
left: 50%;
}
.bar:after {
right: 50%;
}
input:focus ~ .bar:before,
input:focus ~ .bar:after {
width: 50%;
}
<div class="main">
<div class="form-group">
<input type="text" name="email" required />
<span class="highlight"></span>
<span class="bar"></span>
<label for="email">Email</label>
</div>
<div class="form-group">
<input type="password" name="password" required />
<span class="highlight"></span>
<span class="bar"></span>
<label for="password">Password</label>
</div>
</div>
I attached your code modified to work as intended, you can change any value to customize as you want but here's an explanation of how it works:
Input element
I added a z-index so that the user can click where the label is positioned and click the input instead of the label itself, also added a transition to make the animation smooth.
input:focus refers when input is active (user click or selected by pressing tab key).
And here's where the magic happens and the explanations of complex selectors:
.input:focus~.label,
.input:not([value=""]):not(:focus):invalid~.label,
.input.has-content~.label {
font-size: .7rem;
top: -.4rem;
color: blue;
}
.input:focus ~ .label selects all input's element siblings with the class .label when input is focus so that when user focus in the input the label'll be above.
.input:not([value=""]):not(:focus):invalid~.label this selector'll catch when user unfocus the input but it got content, so the label don't goes down and overlap with the username. (as the password type input doesn't have value attribute I attach you a Js snippet that do the same trick for password element, simply adding the class has-content to the element)
Hope it helps you!
document.querySelector('.password').oninput = (e) => {
e.target.value.length > 0 ?
e.target.classList.add('has-content') :
e.target.classList.remove('has-content')
}
.form {
position: relative;
display: flex;
flex-direction: column;
margin: 1rem;
}
.input {
height: 20px;
background: none;
color: #c6c6c6;
font-size: 1rem;
padding: .5rem .7rem;
display: block;
border: none;
border-bottom: 2px solid #c6c6c6;
width: 100%;
z-index: 2;
transition: all .3s ease;
}
.input:focus {
outline: transparent;
border-color: blue;
}
.input:focus~.label,
.input:not([value=""]):not(:focus):invalid~.label,
.input.has-content~.label {
font-size: .7rem;
top: -.4rem;
color: blue;
}
.label {
position: absolute;
color: #c6c6c6;
font-size: 1rem;
left: 0.5rem;
top: .6rem;
transition: all .3s ease;
}
<div class="form">
<input class="email input" type="email" name="email" />
<label class="label" for="email">Email Address</label>
</div>
<div class="form">
<input class="password input" type="password" name="password" />
<label class="label" for="password">Password</label>
</div>

css can't change checkbox background color

I'm learning css and html, I'm validating a registration form, I have the user terms checkbox but I can't change the background color:
This is the html code where I create the checkbox and the label:
<div class="checkbox-wrap checkbox-primary" id="checkboxdiv" name="checkboxdiv">
<input class="checkbox" type="checkbox" value="0" name="termsNewUser" id="termsNewUser">
<label class="checkbox-wrap checkbox-primary" for="termsNewUser" id="termsNewUserL" name="termsNewUserL">I do accept the <u>Terms and Conditions</u> of your site.
</label>
</div>
I tried some solutions like:
input[type="checkbox"] {
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 0;
accent-color: #9d3039;
}
And:
input[type=checkbox] {
transform: scale(1.5);
}
input[type=checkbox] {
width: 30px;
height: 30px;
margin-right: 8px;
cursor: pointer;
font-size: 17px;
visibility: hidden;
}
input[type=checkbox]:after,
input[type=checkbox]::after {
content: " ";
background-color: #fff;
display: inline-block;
margin-left: 10px;
padding-bottom: 5px;
color: #00BFF0;
width: 22px;
height: 25px;
visibility: visible;
border: 1px solid #00BFF0;
padding-left: 3px;
border-radius: 5px;
}
input[type=checkbox]:checked:after,
input[type=checkbox]:checked::after {
content: "\2714";
padding: -5px;
font-weight: bold;
}
I tried also to create a custom class and in the style.css set the accent there but nothing.
When you simply make a global definition like the one below, this color should change,
:root {
accent-color: red;
}
In your case, this change stays in the background since you have given the checkbox element visible hidden.
input[type=checkbox]:checked:after,
input[type=checkbox]:checked::after {
content: "\2714";
padding: -5px;
font-weight: bold;
background-color: red;
}
so in the checked state you can change the background color directly to get the same look.
demo https://jsfiddle.net/rjzw10cv/1/
ahh I remember having this issue. Here it is. Just change the background color and color to anything you'd like and you should be set. This can be done with any input type.
input[type="checkbox"]:checked+label:before {
background: #3d404e;
color: #F00;
content: "\2713";
text-align: center;
}
so your html code would be
<div class="checkbox-wrap checkbox-primary" id="checkboxdiv" name="checkboxdiv">
<input class="checkbox" type="checkbox" value="0" name="termsNewUser" id="termsNewUser">
<label class="checkbox-wrap checkbox-primary" for="termsNewUser" id="termsNewUserL" name="termsNewUserL">I do accept the <u>Terms and Conditions</u> of your site.
</label>
</div>
and your full css would be below, I added a margin-right:20px as to hide the large space behind the custom checkbox elements.
p {
margin: 5px 0;
padding: 0;
}
input[type="checkbox"] {
margin-right: -20px;
cursor: pointer;
font-size: 17px;
visibility: hidden;
}
label {
cursor: pointer;
}
/* EDIT THE BACKGROUND VALUE FOR CUSTOM CHECKBOX bg COLOR FOR NOT CHECKED */
input[type="checkbox"]+label:before {
border: 1px solid #7f83a2;
content: "\00a0";
display: inline-block;
background: #000;
font: 16px/1em sans-serif;
height: 16px;
margin: 0 .25em 0 0;
padding: 0;
vertical-align: top;
width: 16px;
}
/* EDIT THE BACKGROUND VALUE FOR CUSTOM CHECKBOX bg COLOR FOR CHECKED, CHANGE COLOR TO CHANGE CHECK MARK COLOR */
input[type="checkbox"]:checked+label:before {
background: #3d404e;
color: #ff0000;
content: "\2713";
text-align: center;
}
input[type="checkbox"]:checked+label:after {
font-weight: bold;
}
Here is a snippet:
p {
margin: 5px 0;
padding: 0;
}
input[type="checkbox"] {
margin-right: -20px;
cursor: pointer;
font-size: 17px;
visibility: hidden;
}
label {
cursor: pointer;
}
/* EDIT THE BACKGROUND VALUE FOR CUSTOM CHECKBOX bg COLOR FOR NOT CHECKED */
input[type="checkbox"]+label:before {
border: 1px solid #7f83a2;
content: "\00a0";
display: inline-block;
background: #000;
font: 16px/1em sans-serif;
height: 16px;
margin: 0 .25em 0 0;
padding: 0;
vertical-align: top;
width: 16px;
}
/* EDIT THE BACKGROUND VALUE FOR CUSTOM CHECKBOX bg COLOR FOR CHECKED, CHANGE COLOR TO CHANGE CHECK MARK COLOR */
input[type="checkbox"]:checked+label:before {
background: #3d404e;
color: #ff0000;
content: "\2713";
text-align: center;
}
input[type="checkbox"]:checked+label:after {
font-weight: bold;
}
<div class="checkbox-wrap checkbox-primary" id="checkboxdiv" name="checkboxdiv">
<input class="checkbox" type="checkbox" value="0" name="termsNewUser" id="termsNewUser">
<label class="checkbox-wrap checkbox-primary" for="termsNewUser" id="termsNewUserL" name="termsNewUserL">I do accept the <u>Terms and Conditions</u> of your site.
</label>
</div>

How do I create permanent placeholders? CSS / HTML

I am working on a web form for my first official development job.
I am currently trying to make it so that my input fields placeholders don't disappear but instead i need them to shrink and move to the top of the input field.
I was able to find some code that should be getting the job done but is not.
Can anyone guide me on how to make this work?
Code I found: https://jsfiddle.net/p19j2nm5/
I've worked on this for hours trying all sorts of variations with no luck.
.wrapper /*wpcf7*/{
position:relative;
}
input {
font-size: 14px;
height: 40px;
}
.placeholder{
position:absolute;
font-size: 25px;
pointer-events:none;
left: 1px;
top: 1px;
transition: 0.1s ease all;
}
input:focus ~ .placeholder{
top: 1px;
font-size: 11px;
}
HTML from contact form 7:
<label> First Name:
[text first-name placeholder "First Name"] </label>
<label> Last Name:
[text* last-name placeholder "Last Name"] </label>
<label> Your Email:
[email* your-email placeholder "Example#Example.com"] </label>
Currently, none on my fields placeholders move when focused, but removing "~.placeholder" does make the field shrink when focused.
Example of what I am trying to get my fields to do:
input:focus ~ .lastname_floating-label,
input:focus ~ .firstname_floating-label,
input:not(:focus):valid ~ .firstname_floating-label,
input:not(:focus):valid ~ .lastname_floating-label{
top: 2px;
left: 2px;
bottom: 2px;
font-size: 11px;
opacity: 1;
}
.inputText {
font-size: 14px;
width: 200px;
height: 35px;
}
.firstname_floating-label, .lastname_floating-label {
position: absolute;
pointer-events: none;
left: 10px;
top: 10px;
transition: 0.2s ease all;
}
div{position:relative;}
<div>
<input type="text" class="inputText" required/>
<span class="firstname_floating-label">Firstname</span>
</div>
<div>
<input type="text" class="inputText" required/>
<span class="lastname_floating-label">Lastname</span>
</div>
Ok I changed according your request.
Other examples are wrong. Try to add more input and you can check that the place holder will be positioned in the first input. And than if you focus out the placeholder will cover the typed value.
Check this example if is ok using only HTML and CSS.
firstly to edit placeholders the selector you need to use is :placeholder. The effect you are trying to recreate is really just a trick with floating the form labels, instead of the placeholder text. There is a thorough explanation available here: https://css-tricks.com/float-labels-css/
If you look closely the jsfiddle example actually is not using a placeholder but a span tag with a class of .placeholder
Your own code has input placeholder attribute and you can target it and input :focus like this:
input::-webkit-input-placeholder {
color: #999;
font-size: 15px;
}
input:focus::-webkit-input-placeholder {
color: red;
font-size: 5px;
}
/* Firefox < 19 */
input:-moz-placeholder {
color: #999;
}
input:focus:-moz-placeholder {
color: red;
font-size: 5px;
}
/* Firefox > 19 */
input::-moz-placeholder {
color: #999;
}
input:focus::-moz-placeholder {
color: red;
font-size: 5px;
}
/* Internet Explorer 10 */
input:-ms-input-placeholder {
color: #999;
}
input:focus:-ms-input-placeholder {
color: red;
font-size: 5px;
}
This will make placeholder text go smaller on input focus.
Updated Daebak Do's answer after reading your comment.
I added some padding to the input, and moved the "placeholder" content inside the input (changing top value from -12px to 2px and adding a left: 2px). I also added margin-top property to make the transition smooth and not disturb the rest of the content on your page.
Now the placeholder content is located inside the input box, like on the screenshot you shared.
.wrapper{
position: relative;
}
input {
font-size: 14px;
height: 40px;
transition: padding-top 0.1s, margin-top 0.1s;
margin-top: 15px;
}
.placeholder {
position: absolute;
font-size:25px;
pointer-events: none;
left: 3px;
top: 18px;
transition: 0.1s ease all;
}
input:focus ~ .placeholder{
top: 2px;
left: 2px;
font-size: 11px;
}
input:focus{
margin-top: 1px;
padding-top: 15px;
transition: padding-top 0.1s, margin-top 0.1s;
}
<div class="wrapper">
<input type="text">
<span class="placeholder">Placeholder</span>
</div>
<p>Some content.</p>

Styling the input type="file". How do I display the file path?

I have been trying to style an input type="file" field.
My button is styled but I can't seem to figure out how to get the filepath/file to show when the user selects the file to upload.
Can anyone out there help?
.file-upload {
overflow: hidden;
display: inline-block;
position: relative;
vertical-align: middle;
text-align: center;
color: #fff;
border: 2px solid #707070;
background: #A0A0A0;
-moz-border-radius: 8px;
-webkit-border-radius: 8px;
border-radius: 8px;
text-shadow: #000 1px 1px 4px;
cursor: pointer;
}
.file-upload:hover {
background: #2FA2FF;
}
.file-upload.focus {
outline: 2px solid yellow;
}
.file-upload input {
position: absolute;
top: 0;
left: 0;
margin: 0;
font-size: 12px;
opacity: 0;
filter: alpha(opacity=0);
z-index: -1;
}
.file-upload span {
position: absolute;
top: 0;
left: 0;
display: inline-block;
padding-top: .45em;
}
.file-upload {
height: 38px;
}
.file-upload,
.file-upload span {
width: 160px;
}
.file-upload-status {
margin-left: 10px;
vertical-align: middle;
padding: 7px 11px;
font-weight: bold;
font-size: 16px;
color: #888;
background: #f8f8f8;
border: 3px solid #ddd;
}
<form action="" method="POST" enctype="multipart/form-data">
<div class="fileupload-buttonbar">
<label class="file-upload"><span>Upload....</span><input name="uploadfile" type="file"> </label>
</div>
</form>
Add a change event to the input field, and then just get it's .value.
Example (using jQuery):
$('input[name="uploadfile"]').change(function(){
var fileName = $(this).val();
alert(fileName);
});
DEMO: http://jsfiddle.net/hjNEC/2/
EDIT: Since the input field is hidden, and the file name is part of that, you're gonna have to display fileName on the page yourself.
You can create a new div to show the file path, and use Javascript in the onchange event to update the path displayed in the div whenever the user selects a file.
<form action="" method="POST" enctype="multipart/form-data">
<div class="fileupload-buttonbar">
<input type="file" name="uploadfile" id="uploadfile" style="display:none" onchange="file_path_display.innerHTML=uploadfile.value"/>
<span class="file-upload" onclick="uploadfile.click()" ondragdrop="uploadfile.dragdrop()">Upload....</span>
<div id="file_path_display"></div>
</div>
</form>

CSS ''background-color" attribute not working on checkbox inside <div>

The heading pretty much explains it. I have a couple of checkboxes inside a scrollable div. But for some reasons the 'background-color' attribute doesn't work. Although the 'margin-top' does seem to work...
Just puzzling me how one attribute can work and another not. It's also not like the div has it's own set of background color attributes that could potentially over ride the checkboxes attributes.
Anyways, below is my HTML (which is generated by JSP):
<div class="listContainer">
<input type="checkbox" class="oddRow">item1<br/>
<input type="checkbox" class="evenRow">item2<br/>
<input type="checkbox" class="oddRow">item3<br/>
<input type="checkbox" class="evenRow">item4<br/>
...
</div>
And here is my CSS:
.listContainer {
border:2px solid #ccc;
width:340px;
height: 225px;
overflow-y: scroll;
margin-top: 20px;
padding-left: 10px;
}
.oddRow {
margin-top: 5px;
background-color: #ffffff;
}
.evenRow{
margin-top: 5px;
background-color: #9FFF9D;
}
A checkbox does not have background color.
But to add the effect, you may wrap each checkbox with a div that has color:
<div class="evenRow">
<input type="checkbox" />
</div>
<div class="oddRow">
<input type="checkbox" />
</div>
<div class="evenRow">
<input type="checkbox" />
</div>
<div class="oddRow">
<input type="checkbox" />
</div>
In addition to the currently accepted answer: You can set border and background of a checkbox/radiobutton, but how it is rendered in the end depends on the browser. For example, if you set a red background on a checkbox
IE will show a red border instead
Opera will show a red background as intended
Firefox, Safari and Chrome will do nothing
This German language article compares a few browsers and explains at least the IE behavior. It maybe bit older (still including Netscape), but when you test around you'll notice that not much has changed. Another comparison can be found here.
You can use peseudo elements like this:
input[type=checkbox] {
width: 30px;
height: 30px;
margin-right: 8px;
cursor: pointer;
font-size: 27px;
}
input[type=checkbox]:after {
content: " ";
background-color: #9FFF9D;
display: inline-block;
visibility: visible;
}
input[type=checkbox]:checked:after {
content: "\2714";
}
<label>Checkbox label
<input type="checkbox">
</label>
After so much trouble i got it.
.purple_checkbox:after {
content: " ";
background-color: #5C2799;
display: inline-block;
visibility: visible;
}
.purple_checkbox:checked:after {
content: "\2714";
box-shadow: 0px 2px 4px rgba(155, 155, 155, 0.15);
border-radius: 3px;
height: 12px;
display: block;
width: 12px;
text-align: center;
font-size: 9px;
color: white;
}
<input type="checkbox" class="purple_checkbox">
It will be like this when checked with this code.
My solution
Initially posted here.
input[type="checkbox"] {
cursor: pointer;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
outline: 0;
background: lightgray;
height: 16px;
width: 16px;
border: 1px solid white;
}
input[type="checkbox"]:checked {
background: #2aa1c0;
}
input[type="checkbox"]:hover {
filter: brightness(90%);
}
input[type="checkbox"]:disabled {
background: #e6e6e6;
opacity: 0.6;
pointer-events: none;
}
input[type="checkbox"]:after {
content: '';
position: relative;
left: 40%;
top: 20%;
width: 15%;
height: 40%;
border: solid #fff;
border-width: 0 2px 2px 0;
transform: rotate(45deg);
display: none;
}
input[type="checkbox"]:checked:after {
display: block;
}
input[type="checkbox"]:disabled:after {
border-color: #7b7b7b;
}
<input type="checkbox"><br>
<input type="checkbox" checked><br>
<input type="checkbox" disabled><br>
<input type="checkbox" disabled checked><br>
2022 - there is a much better solution to this problem now
Just use the accent-color property and make sure you achieve proper contrast ratios for accessibility:
.blue-checkbox {
accent-color: #00eaff;
height: 30px; /* not needed */
width: 30px; /* not needed */
}
<input class="blue-checkbox" type="checkbox" />
We can provide background color from the css file. Try this one,
<!DOCTYPE html>
<html>
<head>
<style>
input[type="checkbox"] {
width: 25px;
height: 25px;
background: gray;
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
border: none;
outline: none;
position: relative;
left: -5px;
top: -5px;
cursor: pointer;
}
input[type="checkbox"]:checked {
background: blue;
}
.checkbox-container {
position: absolute;
display: inline-block;
margin: 20px;
width: 25px;
height: 25px;
overflow: hidden;
}
</style>
</head>
<body>
<div class="checkbox-container">
<input type="checkbox" />
</div>
</body>
</html>
The Best solution to change background checkbox color
input[type=checkbox] {
margin-right: 5px;
cursor: pointer;
font-size: 14px;
width: 15px;
height: 12px;
position: relative;
}
input[type=checkbox]:after {
position: absolute;
width: 10px;
height: 15px;
top: 0;
content: " ";
background-color: #ff0000;
color: #fff;
display: inline-block;
visibility: visible;
padding: 0px 3px;
border-radius: 3px;
}
input[type=checkbox]:checked:after {
content: "✓";
font-size: 12px;
}
<input type="checkbox" name="vehicle" value="Bike"> I have a bike<br>
<input type="checkbox" name="vehicle" value="Car" checked> I have a car<br>
<input type="checkbox" name="vehicle" value="Car" checked> I have a bus<br>
Improving another answer here
input[type=checkbox] {
cursor: pointer;
margin-right: 10px;
}
input[type=checkbox]:after {
content: " ";
background-color: lightgray;
display: inline-block;
position: relative;
top: -4px;
width: 24px;
height: 24px;
margin-right: 10px;
}
input[type=checkbox]:checked:after {
content: "\00a0\2714";
}
When you input the body tag, press space just one time without closing the tag and input bgcolor="red", just for instance. Then choose a diff color for your font.