Trying to create a material design like textbox, issue in input validation - css

I am trying to create a material design inspired textbox.
I have read similar questions/answers around. However, my question is little different, so kindly read on.
Here is my stylesheet for the page -
.textBoxContainer{
height:50px;
position:relative;
margin:20px;
}
.textbox{
border-bottom: solid 2px #984343;
border-top: none;
border-left: none;
font-family: Candara;
width: 30%;
}
.textbox + .lbl{
position:absolute;
top:0px;
left:0px;
transition:all .2s ease-out;
}
.textbox:focus{
border-bottom: solid 2px #4CAF50;
outline:none;
}
.textbox:focus + label, input:valid+label{
top:-15px;
font-size:10px;
}
Here is the textbox which I am trying to create -
<div class='textBoxContainer'>
<input class='textbox' type="text" />
<label class='lbl'>
phone no....
</label>
</div>
It works fine like a material design inspired textbox, where the text moves up upon typing.
However, after the typing is done, if I move out of the textbox, then the label collapses back again on the textbox.
I was thinking 'input:valid' will help prevent it from happening, but it did not.
I can solve this with javascript, but is there a way around without using js?
I have also tried this putting a 'required' in the input filed, that did not help either.
Please let me know if I should explain any further.

I believe that would require to use javaScript. If you don't want to use javascript, just keep the label on top of the textField, or just use a placeholder.
The way to implement it using JavaScript that would be as follows:
Add a onfocusout event listener to all the textfields which you want that label to stay up.
<input class='textbox' type="text" onfocusout="leaveFocus(event)" />
Next, you have to create the function which gets called everytime the textField looses focus.
function leaveFocus(event) {
const textField = event.target;
// Check if textField is empty
if (textField.value !== '') {
textField.classList.add('has-text);
} else {
// Otherwise, textFiled has text
textField.classList.remove('has-text);
}
}
That function checks to see if the textField is empty. If it is, it removes the class of has-text, and if it has text, it adds the class of has-text.
Now in your css, you have to add has-text to all the styling you have for the label going up, like this:
.textbox:focus, .has-text{
border-bottom: solid 2px #4CAF50;
outline:none;
}
.textbox:focus + label, .has-text + label, input:valid+label{
top:-15px;
font-size:10px;
}
Let me know if this answers your question, and let me know if you need any more help.

Related

CSS/React JS: I want the input fields to remain focus after inputing the values

I have a confirmation code that consist of 6 digits. When I click on one of the input fields, the field is in focus and is underlined with a blue color.
But when I input in the next input field, the first input field, the underline blue color disappears.
I want the first input field to remain underline when i fill the second input field and same for the other 4 input fields.
I have used the focus selector:
input[type='tel'].InputField:focus {border: 0 none; border-bottom: 2px solid blue; border-bottom-left-radius: 0; border-bottom-right-radius: 0; outline: none;}
The CSS Code snippet you've posted will only highlight the input field with blue border when it is in focus meaning when it is selected as you have stated in the pseudo-class :focus
input[type='tel'].InputField:focus {
border: 0 none;
border-bottom: 2px solid blue;
border-bottom-left-radius: 0;
border-bottom-right-radius: 0; outline: none;
}
A probable solution to your question can be a workaround using the :placeholder-shown pseudo class like this:
input[type="tel"]:not(:placeholder-shown) {
background-color: cyan;
border: 1px solid blue;
}
<input type="tel" placeholder=" ">
<input type="tel" placeholder=" ">
Mine is a CSS-only answer is based on ngryman's answer here. Style it accordingly using the ReactJS syntax for your use.
You can create another css class that contains the same styles, let's call it "afterEdit"(You can name it as you want).
Then, you need to listen to the input changes with the onChange attribute.
For each input, after you catch a change, you need to apply the "afterEdit" css class, so although you move to another input, and you lose the focus on the input element, you still apply the focus styles.
You can do it with className, lets say you save in the state, a boolean indicates whether the input has been changed. So you can modify the className of the input element:
render() {
const isChanged = this.state.inputA.changed;
let className = ['InputField'];
if(isChanged) className.push('afterEdit');
return <input className = {className.join(' ')} />

Changing checkbox layout without using label

Is it possible to change the layout of a checkbox without adding the label tag in CSS?
Things like this do not have any effect:
input[type=checkbox][disabled] {
background-color: green;
border: 10px solid red;
}
The only thing I found so far is how to change the opacity.
I'm not sure if this will be much use to you, but it does allow you to "style up" a checkbox without the need for a label. I've remove the disabled flag so you can swap between the different styles. Shouldn't be difficult to add it back in if this will work for you.
Fiddle is here.
input[type=checkbox]:checked:before {
background-color: green;
border: 10px solid red;
}
input[type=checkbox]:before {
content:'';
display:block;
height:100%;
width:100%;
border: 10px solid green;
background-color: red;
}
The above only works on Chrome, however, it seems like Chrome is in the wrong where the specification is concerned.
A fuller answer here: CSS content generation before or after 'input' elements
As of today there is no solution, if we assume a cross browser functional styling, to style the <input type="checkbox" > alone, other than a few properties like opacity, width, height, outline (and maybe a few more).
Using a label (or other content elements) is what you need to do that and here is a good (which this question is likely a duplicate of) post with lots of options: How to style checkbox using CSS?
Note: If you know more properties, feel free to update this answer.

Mouse hover event not working for the second time

I am using asp.net text box and I am showing the border for the text box on mouse hover event and its pretty well doing it's job for the first time and when I click on the text box and again click outside the textbox and If I mouse hover the same textbox it dosen;t show me the border.
Can anyone say me where I am doing wrong?
Thanks in advance!
Here is my css code:
.onfocus
{
border-width: thin;
border-color: #0033CC;
}
.onblur
{
border:0px;
}
.MyTextbox:hover{border:1px solid red;}
Here is how I am using it:
<script type ="text/javascript">
function Change(obj, evt) {
if (evt.type == "focus")
obj.className = "onfocus";
else if (evt.type == "blur")
obj.className = "onblur";
}
<asp:TextBox ID="txtUsername" runat="server" Style="position: absolute; top: 76px;
left: 24px; width: 189px; height: 24px; outline: none;" onfocus ="Change(this, event)"
onblur ="Change(this, event)" BackColor="#FAFFBD" CssClass="MyUsername" ></asp:TextBox>
" outline: none;"
where is the style attr ?
The hover is not working the second time because your javascript code on focus/blur is changing the class value for the textbox so that it no longer has the "MyTextbox" class. Since your border change on hovering is done based on the MyTextbox class, it no longer works after that.
What you should be doing instead of setting obj.className = "onfocus", is to add the "onfocus" class to the existing value so that it is not lost. Then, during the blur event, you would remove the onfocus class, and add in the onblur class (again, not just totally replacing the className value).
This question has some good answers about how you can properly add/remove the extra classes in either plain javascript or with jQuery (which makes it much easier).

How do I customize my WordPress search form?

Mission
I would like to customize the search field and button on my WordPress blog.
The search textfield and button I have in mind will have three states:
off
:hover
:focus
I will need to have three different DIV ids for each of these states.
Search's Text Field specifics
Additionally, I will need there to be the text "search site" initially loaded into the search's text field. when a user clicks into the search's text field that initial text will disappear and the users cursor will appear blinking. the user can then type in their search keyword. if after typing in their keyword they happen to click off the search's text field, their keyword will remain in the text field intact. if they decide to delete their keyword and click off of the search's text field , the text "search site" will reappear.
Search's Button specifics
The search button will have to have the text "search" that is centered vertically and horizontally.
Current State of my Search Text Field and Button
my current search form and site can be seen here at criticear
I have been able to make my comment form text fields have the 3 states I mentioned above since I got the code from ottodestruct's WordPress threaded comments tutorial.
the thing is that I do not quite understand how to take this comment form css and apply it to my search's textfield and button. you can check out my comment form on my blog criticear's single post page
Here is my search form CSS:
/*
SEARCH FORM
*/
form#searchform
{
display:block;
width:255px;
height:20px;
position:absolute;
top:56px;
left:753px;
}
.searchbutton
{
color: #0066ff;
border: 0px solid;
display:block;
width:45px;
height:20px;
background: #d2e4ff;
position:absolute;
top:0px;
left:202px;
-moz-border-radius-bottomright: 4px;
-moz-border-radius-topright: 4px;
-webkit-border-bottom-right-radius: 4px;
-webkit-border-top-right-radius: 4px;
font-size: 12px;
}
.searchbutton:hover
{
background-color: #0066ff;
color: #ffffff;
font-size: 12px;
}
.searchfield
{
background:url(/images/search-field-shadow.png) top left repeat-x #666666;
color: #eeeeee;
border: 0px solid;
position: absolute;
top:0px;
left:0px;
display:block;
width:200px;
height:20px;
-moz-border-radius-bottomleft: 4px;
-moz-border-radius-topleft: 4px;
-webkit-border-bottom-left-radius: 4px;
-webkit-border-top-left-radius: 4px;
font-size: 12px;
}
here is my searchform.php code:
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" class="searchfield" />
<input type="submit" id="searchsubmit" value="search" class="searchbutton"/>
</form>
Here is the searchform php call I have in my header.php:
<?php get_search_form(); ?>
If you have any questions or need more info than let me know. I hope you can help. Thanks.
You need to use CSS to style the form as you want to, and JavaScript or a JavaScript library (like jQuery) to create the focus/blur effect you're looking for. I'm not going to do the whole thing for you, but I'll just point you in the right direction.
First of all, you don't need "3 different DIV ids for each of these 3 states." CSS has pseudo-selectors for :hover and :focus, so that's all you need. If you ever get caught up on that or run into browser problems, you can always use JavaScript (again, I recommend jQuery).
For the "Search site" effect in the input box, you'll be doing something like this (this is jQuery):
$('input[name=s]').focus(function(){
if ($(this).val() == 'Search site')
$(this).val('');
});
$('input[name=s]').blur(function(){
if ($(this).val() == '')
$(this).val('Search site');
});
The last thing I'll mention is that your CSS classes searchfield and searchbutton make your HTML more verbose than it needs to be. You can easily access these fields in your CSS without having to individually declare them in your HTML:
#searchform input[name=s] { } /* use this instead of .searchfield */
#searchform input[type=submit] { } /* use this instead of .searchbutton */

HTML input type submit: problem with width on IE

this will be quite difficult to explain. I hope I'm able to.
I recently created a custom ASP.net server control, representing a toolbar. The toolbar contains buttons, so HTML elements. To allow me to add an image I use CSS which add it to the background. The CSS which I apply on the input element looks like this:
.button{
padding: 2px 2px 2px 2px;
margin-right: 2px;
border: 1px solid #999;
cursor: pointer;
text-decoration: none;
color: #606060;
}
Moreover on the button itself (through the style tag; this is because these kind of buttons are rendered automatically and shouldn't be changed by the end-programmer) I have styles which define the background images and some additional settings
background-attachment:scroll;
background-image:url(images/select.png);
background-position:left center;
background-repeat:no-repeat;
padding-left:15px;
The padding-left is needed s.t. the text doesn't go behind the background image. So at the end you would have something like
<input type="submit" style="background-image: url(images/select.png); background-attachment: scroll; background-repeat: no-repeat; background-position: left center; padding-left: 15px;" class="button" id="someId" value="Save" name="someName"/>
On Firefox (as usual) everything works perfectly. My problem is that on IE (tested on IE 7 but I need to be compatible from IE 6+) it happens that if you enter a quite long text as the button text, the button will enlarge, basically the space before and after the button text increases with the size of the text. To have the button still immediately after the image I added the line text-align:right to the button class.
To illustrate it better...
On Firefox:
alt text http://img268.imageshack.us/img268/311/buttonfirefox.jpg
On IE:
alt text http://img23.imageshack.us/img23/2373/buttonie.jpg
Does anyone have any suggestion on how I could fix this??
//Edit:
What I could do of course is to specify a fixed width on the button, till it looks nicely. I would like to avoid this however, if possible.
This is an old bug. You need to add overflow:visible to the button. There is more here:
http://jehiah.cz/archive/button-width-in-ie
and here:
http://www.brandnewbox.co.uk/articles/details/removing_padding_from_ie_buttons/
Just try a css reset of submit button first (at the beginning of css file). For example margin, padding etc set to zero.
I am not quite sure how apply reset for submit buttons ..
but you can try following and test
/**
* Reset browser defaults
* Author: Tim Wright - csskarma.com
* Last updated: 04.19.2009
----------------------------------*/
body,div,dl,dt,dd,ul,ol,
li,h1,h2,h3,h4,h5,h6,
pre,form,fieldset,p,
blockquote,th,td { margin:0;padding:0; }
body { line-height:1;color:#121212;background:#fff; }
h1,h2,h3,h4,h5,h6,p { font-size:100%;font-weight:400; }
ol,ul { list-style:none; }
caption,cite,code,th { font-style:normal;font-weight:400; }
fieldset,img { border:0; }
caption,th { text-align:left; }
:focus { outline:1px dotted #eee; }
table { border-collapse:collapse;border-spacing:0; }
hr { border:0;border-top:1px solid #555;margin:0;height:1px; }
label,button { cursor:pointer; }
As per #Andrew's answer you can try * html input { overflow: visible; } also.

Resources