Cannot able to float label to top when focus in textbox - html

I want to float the label when users focus on textboxes as in this example, but on focus the label does not move upwards. Below is my code.
.input-base-input {
width: 100%;
font-size: 15px;
padding-top: 8px;
border: none;
border-bottom: .5px solid #a9abb3;
background: transparent;
font-weight: 600;
}
.input-base-input:focus ~ .input-base-label,
.input-base-input:not(:focus):valid ~ .input-base-label{
display: block;
position: absolute;
top: 10px;
transition-property: top;
transition-duration: .1s;
}
.input-base-label {
position: absolute;
pointer-events: none;
top:-10;
transition-property: top;
transition-duration: .1s;
}
<label for="pincode" class="input-base-label">Pin Code</label>
<input class="input-base-input" maxlength="6">

First, this CSS:
.input-base-input:focus ~ .input-base-label
will select the label which comes after the input (and not before as shown in your code), so first change the order of input and label.
Second, you have not specified the correct value for the top property of .input-base-label:
.input-base-label {
...
/* Not correct */
top: -10;
/* Correct */
top: -10px;
/* or */
top: 0;
...
}
Third, :valid selector will make your input valid even if it's empty (so your label will be floated on the page load). To resolve this with CSS-only approach, add required attribute to the input.
So, your final result might look like this:
<input class="input-base-input" id="pincode" name="pincode" maxlength="6" required>
<label class="input-base-label" for="pincode">Pin code</label>
.input-base-input {
width: 100%;
font-size: 15px;
padding-top: 8px;
border: none;
border-bottom: .5px solid #a9abb3;
background: transparent;
font-weight: 600;
}
.input-base-label {
position: absolute;
pointer-events: none;
top: 0;
transition-property: top;
transition-duration: .1s;
}
.input-base-input:focus ~ .input-base-label,
.input-base-input:not(:focus):valid ~ .input-base-label {
display: block;
top: -10px;
/* The following properties are not needed as they are specified previously */
/*
position: absolute;
transition-property: top;
transition-duration: .1s;
*/
}
<input class="input-base-input" id="pincode" name="pincode" maxlength="6" required>
<label class="input-base-label" for="pincode">Pin code</label>

Related

label to stay on top after input field is filled

I want to keep my label to stay on top of the input field after we fill the input data in html
I have tried the valid function in css but couldn't achieve the functionality.
.txt_field input {
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
}
.txt_field label {
position: absolute;
top: 65%;
left: 5px;
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
}
.txt_field input:focus~label {
top: 0px;
color: #0170C1;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<input type="text" class="textbox" id="username" placeholder="">
<label>PhoneNumber/Email</label>
</div>
I have tried .txt_field input:focus ~valid but the label keeps overlapping with the input data.
What can I do to make the label stay on top after the input field is filled.
Use the :placeholder-shown pseudo-class to detect does input is filled and use ::placeholder pseudo-elements selectors to hide unwanted placeholder.
Also you need position:relative; for the .txt_field for better view.
And it's better to use + except ~.
Important: Input tag must have a non-empty placeholder attribute.
.txt_field{
position:relative;
}
.txt_field input {
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
}
.txt_field label {
position: absolute;
top: 65%;
left: 5px;
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
}
.txt_field input:focus+label, .txt_field input:not(:placeholder-shown)+label {
top: 0px;
color: #0170C1;
}
.txt_field input::placeholder {
color: transparent;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<input type="text" class="textbox" id="username" placeholder="if empty will not work">
<label>PhoneNumber/Email</label>
</div>
.txt_field input {
width: 100%;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
background-color: rgba(0,0,0,.1);
}
.txt_field label {
position: absolute;
top: 65%;
left: 5px;
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
}
.txt_field input:focus~label,
.txt_field input:not(:placeholder-shown)~label{
top: 0px;
color: #0170C1;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<input type="text" class="textbox" id="username" placeholder="">
<label>PhoneNumber/Email</label>
</div>
Two important points:
1- you must learn about HTML structure and understand its importance.
2- you must learn about display and positioning element. learning this will help you to put elements properly where you want them.
don't use position: absolute; for this case. and why is your input 100% width? that's not proper.
.txt_field input {
display: flex;
flex-direction: column;
position: relative;
width: 300px;
padding: 0 5px;
height: 40px;
font-size: 16px;
border: none;
background: none;
outline: none;
border: 1px solid red;
}
.txt_field label {
/* position: absolute; */
color: #adadad;
transform: translateY(-50%);
font-size: 16px;
pointer-events: none;
transition: .3s;
border: 1px solid orange;
}
.txt_field input:focus~label {
top: 0px;
color: #0170C1;
}
<div class="txt_field">
<i class="fa fa-user"></i>
<label>PhoneNumber/Email</label>
<input type="text" class="textbox" id="username" placeholder="">
</div>
You can do it with the translate effect like this:
.input:focus ~ .placeholder,
.input:not(:placeholder-shown) ~ .placeholder {
transform: translateY(-30px) translateX(10px) scale(0.75);
}
and of course you better give it a transition to make it cool
// transition: transform 200ms

CSS Custom RadioButton not working [duplicate]

This question already has an answer here:
Why are my show/hide styles not working when I click the radio button?
(1 answer)
Closed 4 years ago.
I'm making a simple custom radio button using CSS and I don't know why is not working as like a normal radio button. When I choose one, the other selected itself too (?).
/* Radio Button */
.radioBtn{
float: right;
margin-top: 30px;
height: 35px;
width: 35px;
border: solid 3px #d8aa00;
border-radius: 50%;
background: #fffbd8;
position: relative;
transition: .3s;
}
.radioBtn::after{
position: absolute;
width: 20px;
height: 20px;
background: #d8aa00;
border-radius: 50%;
content: '';
top: 7px;
left: 7px;
opacity: 0;
transition: .3s;
}
input[type=radio]:checked ~ .radioBtn::after {
opacity: 1;
}
input[type=radio]{
display: none;
}
<input id="radioBtn" type="radio" name="test">
<label class="radioBtn" for="radioBtn"></label>
<input id="radioBtn2" type="radio" name="test">
<label class="radioBtn" for="radioBtn2"></label>
Use + instead of ~ to target only the immediate sibling placed after and not all of them.
The + combinator selects adjacent siblings. This means that the second
element directly follows the first, and both share the same parent.ref
The ~ combinator selects siblings. This means that the second element
follows the first (though not necessarily immediately), and both share
the same parentref
/* Radio Button */
.radioBtn{
float: right;
margin-top: 30px;
height: 35px;
width: 35px;
border: solid 3px #d8aa00;
border-radius: 50%;
background: #fffbd8;
position: relative;
transition: .3s;
}
.radioBtn::after{
position: absolute;
width: 20px;
height: 20px;
background: #d8aa00;
border-radius: 50%;
content: '';
top: 7px;
left: 7px;
opacity: 0;
transition: .3s;
}
input[type=radio]:checked + .radioBtn::after {
opacity: 1;
}
input[type=radio]{
display: none;
}
<input id="radioBtn" type="radio" name="test">
<label class="radioBtn" for="radioBtn"></label>
<input id="radioBtn2" type="radio" name="test">
<label class="radioBtn" for="radioBtn2"></label>

Align label with css 3 fancy radio button

I have the following radio button, I need the bigger circle to be 38px
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked+label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Here is a fiddle, how can I align the label so it is aligned to the centered and pushed to the right of the circle?
Add .container{ line-height:38px} to have it centered (it seems that it was to the right already)
https://jsfiddle.net/8gubpzhq/
to move it to the right add this to the
.label {
font-weight: normal;
color: #333;
padding-left:5px;//add this line
}
https://jsfiddle.net/vszuu535/
You can add line-height:40px; to your .label to center it vertically. To move it over to the right more you can add padding-left:20px; (You can change the line-height and padding-left to fit your needs).
input[type=radio] {
visibility: hidden;
}
.label {
font-weight: normal;
color: #333;
line-height:40px;
padding-left:20px;
}
.label::before {
content: '';
position: absolute;
left: 0;
width: 38px;
height: 38px;
border: 1px solid #727272;
border-radius: 50%;
}
input[type=radio]:checked + label:after {
content: '';
position: absolute;
width: 38px;
height: 38px;
left: 0;
background: #0065bd;
border: none;
transform: scale(0.5);
border-radius: 50%;
}
<div class="container">
<input type="radio" id="radio1" value="on">
<label for="radio1" class="label">Yes</label>
</div>
Perhaps your code is over-complicating matters; as you want the input to be bigger, maybe you should focus the sizing etc on the input rather than the label?
See the snippet (the radio turns blue now since edit, adapted from this codepen. It's grey before click, blue after, centered, and indented from the edge).
Just a note: If you are going to use a default value (and only have one option) maybe a custom checkbox would be a more suitable choice? (Radios button are usually used in instances where the user would have 2 or more choices, but can only select one).. just a thought.
input[type="radio"] {
display: none;
width: 38px;
height: 38px;
border: 1px solid #727272;
}
input[type="radio"]+label span {
display: inline-block;
width: 38px;
height: 38px;
margin: 9px;
vertical-align: middle;
cursor: pointer;
border-radius: 50%;
background-color: grey;
}
input[type="radio"]:checked+label span {
content="";
background: #0065bd;
}
input[type="radio"]+label span,
input[type="radio"]:checked+label span {
-webkit-transition: background-color 0.4s linear;
-o-transition: background-color 0.4s linear;
-moz-transition: background-color 0.4s linear;
transition: background-color 0.4s linear;
}
<div class="container">
<input type="radio" id="radio1" name="radio">
<label for="radio1" class="label"><span></span>Yes</label>
<input type="radio" id="radio2" name="radio">
<label for="radio2" class="label"><span></span>No</label>
</div>

How to add label to text field in css AND to give help text also [duplicate]

I am looking to find an example of how to make a label / placeholder transition move up and out of placeholder position into a label position and vice versa..
Example: https://www.xero.com/us/signup/
General sibling selectors & :focus does the trick in a very simple way ;)
input{
position:absolute;
top:20px;
}
input ~ span{
transition:top .7s ease;
position:absolute;
top:20px;
}
input:focus ~ span{
top:0px;
}
<label>
<input>
<span>Text</span>
</label>
here is an example with multiple fields
https://jsfiddle.net/shLe3107/1/
hope this is enough else just ask
I found a good Codepen showing an example of how to do it in CSS.
HTML:
<div class="row">
<input id="name" type="text" name="name">
<label for="name">Full Name</label>
</div>
CSS:
.row {
position: relative;
margin-top: 35px;
}
input {
display: block;
padding: 8px 12px;
width: 100%;
max-width: 300px;
border-radius: 5px;
border: 0;
}
label {
position: absolute;
font-weight: 600;
color: #777777;
top: 50%;
left: 12px;
transform: translateY(-50%);
-ms-transform: translateY(-50%);
-webkit-transform: translateY(-50%);
cursor: text;
user-select: none;
transition: 0.15s ease-in-out;
}
input[data-empty="false"] + label,
input:valid + label,
input:focus + label {
top: -10px;
left: 0px;
font-size: 10px;
color: #ffffff;
}
Example:
https://codepen.io/sivan/pen/alKwf

How to make checkboxes rounded?

Is there any way to make checkboxes with rounded corners using bootstrap or some css property?
Just using css, however, you lose the checkbox tick.
.checkbox-round {
width: 1.3em;
height: 1.3em;
background-color: white;
border-radius: 50%;
vertical-align: middle;
border: 1px solid #ddd;
appearance: none;
-webkit-appearance: none;
outline: none;
cursor: pointer;
}
.checkbox-round:checked {
background-color: gray;
}
<input type="checkbox" class="checkbox-round" />
.container {
display: block;
position: relative;
padding-left: 35px;
margin-bottom: 12px;
cursor: pointer;
font-size: 22px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
/* Hide the browser's default checkbox */
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
height: 0;
width: 0;
}
/* Create a custom checkbox */
.checkmark {
position: absolute;
top: 0;
left: 0;
height: 25px;
width: 25px;
background-color: #eee;
border-radius: 15px;
}
/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
background-color: #ccc;
}
/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
background-color: #2196F3;
}
/* Create the checkmark/indicator (hidden when not checked) */
.checkmark:after {
content: "";
position: absolute;
display: none;
}
/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
display: block;
}
/* Style the checkmark/indicator */
.container .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
height: 10px;
border: solid white;
border-width: 0 3px 3px 0;
-webkit-transform: rotate(45deg);
-ms-transform: rotate(45deg);
transform: rotate(45deg);
}
<label class="container">One
<input type="checkbox" checked="checked">
<span class="checkmark"></span>
</label>
<label class="container">Two
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">Three
<input type="checkbox">
<span class="checkmark"></span>
</label>
<label class="container">Four
<input type="checkbox">
<span class="checkmark"></span>
</label>
Try to do
body {
background-color: #f1f2f3;
-webkit-box-align: center;
-ms-flex-align: center;
align-items: center;
display: -webkit-box;
display: -ms-flexbox;
display: flex;
}
.container {
margin: 0 auto;
}
.round {
position: relative;
}
.round label {
background-color: #fff;
border: 1px solid #ccc;
border-radius: 50%;
cursor: pointer;
height: 28px;
left: 0;
position: absolute;
top: 0;
width: 28px;
}
.round label:after {
border: 2px solid #fff;
border-top: none;
border-right: none;
content: "";
height: 6px;
left: 7px;
opacity: 0;
position: absolute;
top: 8px;
transform: rotate(-45deg);
width: 12px;
}
.round input[type="checkbox"] {
visibility: hidden;
}
.round input[type="checkbox"]:checked + label {
background-color: #66bb6a;
border-color: #66bb6a;
}
.round input[type="checkbox"]:checked + label:after {
opacity: 1;
}
<div class="container">
<div class="round">
<input type="checkbox" id="checkbox" />
<label for="checkbox"></label>
</div>
</div>
One of the Best And Easiest Method is to use CSS clip path property:
<input type="checkbox" id="checkbox" />
<label for="checkbox" >Option</label>
input[type="checkbox"] {
width: 45px; /* Set width */
height: 45px; /* Set height */
clip-path: circle(46% at 50% 50%); /* Set the clip path of circle*/
}
if you still see some pointed corners, try reducing the first percentage values, (where I have used 46%), play with it a little bit and it will definitely work.
Well, this is the simplest and optimal solution. You set appearance to none and then use clip-path when its checked.
.rounded-checkbox {
width:35px;
height: 35px;
border-radius: 50%;
vertical-align: middle;
border: 1px solid black;
appearance: none;
-webkit-appearance: none;
outline: none;
cursor: pointer;
}
.rounded-checkbox:checked {
appearance: auto;
clip-path: circle(50% at 50% 50%);
background-color: blue;
}
<input
type="checkbox"
class="rounded-checkbox"
id="checkbox"
/> <label for="checkbox">Checkbox</label>
in css you may play with height, width, border-radius. basically height and width should be equal and border-radius should be half of them.
if you are using bootstrap you can use this class and add it where your shape or in this case your checkbox class is: class="rounded-circle"
look: borders bootstrap 5
for example to make a checkbox rounded in bootstrap 5 (also working for v4):
<div class="form-check">
<input
class="form-check-input rounded-circle"
type="checkbox"
/>
<label class="form-check-label" for="flexCheck1">
rounded checkbox
</label>
</div>
CSS
.checkbox {
clip-path: circle(46% at 50% 50%);
}
HTML
<input type="checkbox" class="checkbox" />
No need to hide the default checkbox and create a custom just set the clip-path and you can easily achieve a circular checkbox.
I thing the best way to make a rounded corners is by using the border-radius property. This site has a nice collection of checkboxes.
For example:
cursor: pointer;
position: absolute;
width: 20px;
height: 20px;
top: 0;
border-radius: 10px;
The last line(border-radius: 10px) will give you a checkbox with rounded corners.