CSS on input boxes - css

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">

Related

How to remove focus outline on clicked checkbox, but not keyboard focus?

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>

Input Styling on Active

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.

Input field background color changing

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.

CSS solution to make drop-down element visible while focused

I'm trying to figure out if there is any pure CSS solution to keep a drop-down element open while the input field of that element is focused? Here is an example:
div {
width: 300px;
line-height: 50px;
text-align: center;
background: #e8e8e8;
border: 1px solid #666;
}
div:hover form {
display: block;
}
form {
display: none;
padding: 0 15px;
}
<div>Hover Me
<form class="search">
<input type="search" placeholder="What are you looking for?" autofocus>
<input type="button" value="Search!">
</form>
</div>
The idea is to keep the form visible when the search field is focused. Because when a user starts typing the search inquiry and the mouse move out of the hover zone, the form hides, and that's very annoying.
Side-question: Is it possible to focus via CSS search input element each time a <div> is hovered?
The solution has been already proposed, but lacks browser support:
9.4. The Generalized Input Focus Pseudo-class: :focus-within
The :focus-within pseudo-class applies to elements for which the
:focus pseudo class applies.
An element also matches :focus-within if one of its
shadow-including descendants matches :focus.
div {
width: 300px;
line-height: 50px;
text-align: center;
background: #e8e8e8;
border: 1px solid #666;
}
div:hover form, div:focus-within form {
display: block;
}
form {
display: none;
padding: 0 15px;
}
<div tabindex="-1">Hover Me
<form class="search">
<input type="search" placeholder="What are you looking for?" autofocus>
<input type="button" value="Search!">
</form>
</div>
Meanwhile, you can use a polyfill:
div {
width: 300px;
line-height: 50px;
text-align: center;
background: #e8e8e8;
border: 1px solid #666;
}
div:hover form, div.focus-within form {
display: block;
}
form {
display: none;
padding: 0 15px;
}
<script src="https://gist.githubusercontent.com/aFarkas/a7e0d85450f323d5e164/raw/"></script>
<div tabindex="-1">Hover Me
<form class="search">
<input type="search" placeholder="What are you looking for?" autofocus>
<input type="button" value="Search!">
</form>
</div>
Wait. There's actually a pure CSS solution. But there is a drawback — it only works with just one <input> tag, and no <form> tag, like this:
div {
width: 300px;
line-height: 50px;
text-align: center;
background: #e8e8e8;
border: 1px solid #666;
padding: 0.5%;
}
input {
display: none;
margin: auto;
}
div:hover input {
display: block;
}
input:focus {
display: block !important
}
<div>Hover Me
<input type="search" placeholder="What are you looking for?">
</form>
</div>
However, you can just make the user search the form by pressing Enter on the keyboard. Unfortunately, this requires JavaScript, which defeats the whole purpose of this post.
I've also noticed that your placeholder text doesn't really work properly, since the text "Search" is still there. There are two solutions to this — use JavaScript to fix it, or change the 'type' of the input tag to "text".

Add padding arround input

I'm working on this code. I would like to add some space around this input field:
<span class="theSpan">
<h:inputText id="search" class="input_style" value="#{accounts.searchString}"></h:inputText>
<h:commandButton value="Search by title" action="#{bookBean.searchByTitle(accounts.searchString)}">
<f:ajax execute="search" render="output"></f:ajax>
</h:commandButton>
</span>
CSS:
.input_style {
width: 260px;
padding: 5px;
padding-right: 40px;
outline: none;
border: 2px solid #999999;
border-radius: 5px;
background-color: #FBFBFB;
/* font-family: Cambria, Cochin, Georgia, serif;*/
/* font-size: 16px;*/
/*background-position: 270px -10px;*/
/* background-image: url('http://www.kirupa.com/images/search.png');*/
background-repeat: no-repeat;
margin-bottom: 30px;
}
.theSpan {
vertical-align: baseline;
}
Visual result:
How can I add some space between the input filed and the table? Also how can I place the text "Search by title" at line line with input field?
Add margin after the input-style
.input_style{
margin-bottom:10px;
}
padding creates space inside the element and margin creates outside the element so you need to use margin.
Coming to "Search by title" use line-height css in this and line-height should be equal to height of input
To put space between two elements use margin (which adds space outside of the borders of your element), not padding (which adds space inside the border of your element).
To vertically align the text next to the input, place it inside of a span element and apply the following CSS to that span.
Since a text field and a span are both inline elements, they should semantically both be placed into a container (block level) element. That is the element that should get the margin applied to it.
.input_style {
width: 260px;
padding: 5px;
padding-right: 40px;
outline: none;
border: 2px solid #999999;
border-radius: 5px;
background-color: #FBFBFB;
background-repeat: no-repeat;
}
.theButton {
vertical-align: baseline;
}
div { margin-bottom: 30px;
border:1px solid red; /* only here to show the layout */
}
<div>
<input type="text" id="search" class="input_style" >
<button class="theButton">Search By Title</button>
</div>
<div>
This is the next item in the flow of the document.
</div>
Another way to solve this issue is to simply make the button the same
height as the input field, then alignment isn't necessary since both elements are the same height:
/* Note that this rule now applies to the input field and the button */
.input_style, .theButton {
width: 260px;
padding: 5px;
padding-right: 40px;
outline: none;
border: 2px solid #999999;
border-radius: 5px;
background-color: #FBFBFB;
background-repeat: no-repeat;
}
/* Now we just reset aspects of the button that are different
from the input field */
.theButton {
width:auto;
border:0;
}
div { margin-bottom: 30px;
border:1px solid red; /* only here to show the layout */
}
<div>
<input type="text" id="search" class="input_style" >
<button class="theButton">Search By Title</button>
</div>
<div>
This is the next item in the flow of the document.
</div>
I would wrap your entire block in a div tag. That way you can easily control both the spage around the form and the line height alignment.
Add CSS
.my-form-wraper {
/*set margins, padding, line-height, etc. here ^/
}
And HTML:
<div class='my-form-wraper'>
<h:inputText id="search" class="input_style" value="#{accounts.searchString}"></h:inputText>
<h:commandButton value="Search by title" action="#{bookBean.searchByTitle(accounts.searchString)}">
<f:ajax execute="search" render="output"></f:ajax>
</h:commandButton>
</div>

Resources