How to style required field (only when it is clicked) [closed] - css

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need style required fields, but I need show style only when user click first time, and then it does not click, I would like to keep with the default color.
With effect of click first form, but with css of second form, result of box 3:
http://jsfiddle.net/rflfn/m0f6xuxd/
Note: the red box-shadow is default color of browser for tag required of HTML5 (I am not using any script).
<div class="test1">box 1<br>Appear box-shadow only on click
<br>(default browser color)
<br>show on 3 input at same time (all required fields).
<form action="#">
<input type="text">
<input type="text" required>
<input type="text" required>
<input type="text">
<input type="text" required>
<input type="text">
<input type="text">
<input type="submit" text="Send">
</form>
</div>
<div class="test2">box 2<br>Have CSS, but I need show color only when click (use standard formatting until it is clicked)
<form action="#">
<input type="text">
<input type="text" required>
<input type="text" required>
<input type="text">
<input type="text" required>
<input type="text">
<input type="text">
<input type="submit" text="Send">
</form>
</div>
<div class="test3">box 3<br>I need style all required fields with my own css (show on 3 input at same time, not only on focus input)
<form action="#">
<input type="text">
<input type="text" required>
<input type="text" required>
<input type="text">
<input type="text" required>
<input type="text">
<input type="text">
<input type="submit" text="Send">
</form>
</div>
.test1, .test2, .test3 {
width: 230px;
padding: 5px;
margin: 5px;
border: 1px solid red;
display: inline-block;
vertical-align: top;
}
form {
margin: 20px;
}
form * {
display: block;
clear: both;
margin: 10px;
}
.test2 :required {
border: 1px solid #FFF;
border-radius: 3px;
}
.test2 :required:valid {
background-color: #E8FFED;
box-shadow: 0 0 3px 1px #00EA33;
}
.test2 :required:invalid {
background-color: #FFEEEE;
box-shadow: 0 0 3px 1px #EA2034;
}
.test3 input[type="text"] {
border: 1px solid #D1D1D1;
border-radius: 3px;
}
.test3 :required {
box-shadow: none;
}
.test3 :required:focus {
border: 1px solid #FFF;
background-color: #FFEEEE;
box-shadow: 0 0 3px 1px #EA2034;
}
.test3 :required:valid {
background-color: #E8FFED;
box-shadow: 0 0 3px 1px #00EA33;
}

EDIT: No classes needed.
You can use general sibling combinator. The only problem is it will not work for elements above the one you click on. To go further you will need Javascript.
jsfiddle
Key part:
input:required:focus~input:required, input:required:focus {
border: 1px solid #FFF;
background-color: #FFEEEE;
box-shadow: 0 0 3px 1px #EA2034;
}

Related

Display HTML5 error message/validation on hidden radio/checkbox

I have CSS-customized radio buttons, which have required validation. The default radio buttons are hidden by CSS rule.
When I submit the form, the default validation message is also hidden.
Form looks like this
How can I display default error message (like: Please select one of the options) while disabling radio buttons? (Pure HTML/CSS without using Js)
input[type="radio"] {
-webkit-appearance: none;
-moz-appearance: none;
appearance: none;
visibility: hidden;
}
input[type="radio"]:checked +label {
border: 1px solid blue;
}
<h3>Gender</h3>
<input id="male" type="radio" name="gender" value="male" required>
<label for="male">Male</label>
<input id="female" type="radio" name="gender" value="female" required>
<label for="female">Female</label>
<input id="other" type="radio" name="gender" value="other" required>
<label for="other">Rather not to say</label>
as #joostS said, hidden radio button will not trigger native error message. for that we need to hide it using opacity. I have created sample example.
Also it will not trigger validation until we submit the form by clicking on submit button. If you need validation on "onChange" event of any form elements, then we need to use jQuery or Javascript solution to achieve that.
I hope it will be helpful.
label {
display: inline-block;
border: 2px solid #83d0f2;
padding: 10px;
border-radius: 3px;
position: relative;
}
input[type="radio"] {
opacity: 0;
position: absolute;
z-index: -1;
}
input[type="radio"]:checked +label {
border: 1px solid #4CAF50;
}
input[type="radio"]:invalid +label {
}
<h3>Gender</h3>
<form>
<input id="male" type="radio" name="gender" value="male" required>
<label for="male">Male</label>
<input id="female" type="radio" name="gender" value="female" required>
<label for="female">Female</label>
<input id="other" type="radio" name="gender" value="other" required>
<label for="other">Child</label>
<input type="submit" />
</form>
You could try using the CSS :invalid pseudo-class. Use this HTML:
<label for="male">Male</label>
<input id="male" type="radio" name="gender" value="male" required>
<span class="error-message">Please select on of the options</span>
And the following CSS:
input[name=gender] + .error-message { display: none; } // or whatever you wanna do with it
input[name=gender]:invalid + .error-message { display: block; }
MDN documentation about :invalid
Here you go...
body {font-size: 15px; font-family: serif;}
input {
background: transparent;
border-radius: 0px;
border: 1px solid black;
padding: 5px;
box-shadow: none!important;
font-size: 15px; font-family: serif;
}
input[type="submit"] {padding: 5px 10px; margin-top: 5px;}
label {display: block; padding: 0 0 5px 0;}
form > div {margin-bottom: 1em; overflow: auto;}
.hidden {
opacity: 0;
position: absolute;
pointer-events: none;
}
.checkboxes label {display: block; float: left;}
input[type="radio"] + span {
display: block;
border: 1px solid black;
border-left: 0;
padding: 5px 10px;
}
label:first-child input[type="radio"] + span {border-left: 1px solid black;}
input[type="radio"]:checked + span {background: silver;}
<form>
<div>
<label for="name">Name (optional)</label>
<input id="name" type="text" name="name">
</div>
<label>Gender</label>
<div class="checkboxes">
<label><input id="male" type="radio" name="gender" value="male" class="hidden" required><span>Male</span></label>
<label><input id="female" type="radio" name="gender" value="male" class="hidden" required><span>Female </span></label>
<label><input id="other" type="radio" name="gender" value="male" class="hidden" required><span>Other</span></label>
</div>
<input type="submit" value="Send" />
</form>
Although I like the minimalistic approach of using native HTML5 validation, you might want to replace it with Javascript validation on the long run. Javascript validation gives you far more control over the validation process and it allows you to set real classes (instead of pseudo classes) to improve the styling of the (in)valid fields. This native HTML5 validation can be your fall-back in case of broken (or lack of) Javascript. You can find an example of that here, along with some other suggestions on how to make Better forms, inspired by Andrew Cole.

CSS: Can't get submit button to center [duplicate]

This question already has answers here:
Center form submit buttons HTML / CSS
(11 answers)
Closed 5 years ago.
Very odd and I spent an hour today trying to figure out what I'm doing wrong. Have a email signup form. Four input fields and a submit button. In my design, the submit button should be centered under the four fields. However, instead the button is flush left aligned no matter whether I use or don't use float:left; or clear:both; or margin:0 auto; In other words, the usual suspects.
Here's the site. The form is on the bottom: http://ellismarsalis2017.jasonmarsalis.com/
Here's the code:
#footerForm {
position: relative;
float: none;
width: 728px;
height: auto;
margin: 0 auto;
padding: 18px auto 0;
}
footer form input {
float: left;
color: #2a358f;
width: 44%;
background: #edc53e;
border-radius: 8px;
margin: 0 2% 14px;
font-size: 18px;
padding: 0 .5%;
border: none;
}
footer form input.signUp {
font-family: "clarendon-urw", serif;
float: none!important;
clear: both;
background: #2a358f;
color: #edc53e;
margin: 0 auto;
font-size: 18px;
padding: 8px 24px;
border: none;
text-align: center;
}
footer p {
padding: 28px 0;
}
<div id="footerForm">
<form name="" method="post" action="http://www.yoursite.com/box.php">
<input name="name" type="text" id="name" value="Your Name">
<input name="field1" type="text" id="field1" value="Your City">
<input name="email" type="text" id="email" value="Your Email Address">
<input name="field2" type="text" id="field2" value="Your State">
<input name="p" type="hidden" id="p" value="7">
<input type="hidden" name="nlbox[1]" value="1">
<input type="submit" name="Submit" class="signUp" value="Sign me up for the Email List!">
</form>
</div>
You should put it on DIV section and make it's style text-align:center like that:
<div style="text-align:center;">
<input type="submit" name="Submit" class="signUp" value="Sign me up for the Email List!">
</div>
or with a class and css code :
HTML
<div class="submitsection">
<input type="submit" name="Submit" class="signUp" value="Sign me up for the Email List!">
</div>
CSS
.submitsection {
text-align:center;
}

applying css to <input type="range">

I am trying to style the input slider in my application. I have the <input type="text"> and <input type="range"> tags dependent on each other. I am facing two problems.
1.When I refresh the document the color on the left of the slider has a default width independent of the <input type="text">
2.When an input in given in <input type="text"> the color doesn't properly vary in the slider (The slider thumb moves though).
input[type="range"]{
-webkit-appearance: none;
-moz-apperance: none;
border-radius: 6px;
height: 8px;
background-image: -webkit-gradient(
linear,
left top,
right top,
color-stop(0.15, #94A14E),
color-stop(0.15, #C5C5C5)
);
}
input[type='range']::-webkit-slider-thumb {
-webkit-appearance: none !important;
background-color: #ffffff;
border: 1px solid #CECECE;
height: 18px;
width: 18px;
border-radius: 50%;
}
<form>
<div id="loanAmount">
<div id="loanAmountText" class="input-group">
<span class="input-group-addon">Loan Amount</span>
<input type="number" step="any" oninput="rangeInput.value=loanAmount.value" id="amountBox" placeholder="eg.1100000" value="1100000" name="loanAmount" for="rangeInput" oninput="rangeInput.value=loanAmount.value" class="form-control" required/>
<span class="input-group-addon input-group-addon-right"><i class="fa fa-rupee" style="font-size:20px"></i></span>
</div>
<div id="amountSlide">
<input for="loanAmount" oninput="loanAmount.value=rangeInput.value" id="amountSlider" type="range" min="100000" max="10000000" name="rangeInput" required value="5000000" />
</div>
</div>
</form>
Could you please let me know what am I missing?

CSS- Select two input elements

Demo of My Problem
I want to select first two input elements.
.choicesDiv input[type="radio"]:nth-child(-n+2)
{
border: 1px solid yellow;
}
You need to wrap your radio buttons in another element like a label, you can't apply a border directly to a radio button.
See below.
Also, if your intent is to actually set the border of a radio button then you'll need to look into creating custom radio buttons with something like this tutorial: http://webdesign.tutsplus.com/articles/quick-tip-easy-css3-checkboxes-and-radio-buttons--webdesign-895. It's relatively easy, all you're doing is hiding the radio button and setting the background image of a label to your custom radio button style.
.qaDiv
{
width: 100%;
margin: 10px 0;
border: 1px solid red;
}
.choicesDiv
{
margin: 5px;
border: 1px solid green;
}
.choicesDiv label:nth-child(-n+2)
{
border: 1px solid yellow;
}
<div class="qaDiv" index="">
<div class="questionDiv">What is your name?</div>
<div class="choicesDiv">
<label><input type="radio" name="choices0" value="Gopi">Gopi</label>
<label><input type="radio" name="choices0" value="Gops">Gops</label>
<label><input type="radio" name="choices0" value="GopiNath">GopiNath</label>
<label><input type="radio" name="choices0" value="GopsAB">GopsAB</label>
</div>
<div class="answerDiv"></div>
<div class="explanationToggle"></div>
<div class="qButtons"></div>
</div>
Try changing the radio type to checkbox. You can only select one radio button, whereas you can select multiple checkboxes
.qaDiv
{
width: 100%;
margin: 10px 0;
border: 1px solid red;
}
.choicesDiv
{
margin: 5px;
border: 1px solid green;
}
.choicesDiv input[type="checkbox"]:nth-child(-n+2)
{
border: 1px solid yellow;
}
<div class="qaDiv" index="">
<div class="questionDiv">What is your name?</div>
<div class="choicesDiv">
<input type="checkbox" name="choices0" value="Gopi">Gopi
<input type="checkbox" name="choices0" value="Gops">Gops
<input type="checkbox" name="choices0" value="GopiNath">GopiNath
<input type="checkbox" name="choices0" value="GopsAB">GopsAB
</div>
<div class="answerDiv"></div>
<div class="explanationToggle"></div>
<div class="qButtons"></div>
</div>
Let me know if it helps :)
I don't know why border is not working, it's working with shadow
.choicesDiv input[type="radio"]:nth-child(-n+2)
{
box-shadow:0px 0px 0px 1px yellow;
}

open my lightbox on pageload

If you visit my page (http://www.dentalfixrx.com/local-equipment-repair/) and click the "get started" button at the top right you will open a lightbox form.
I would like to create code so the lightbox appears on page load automatically.
Here is the code to open the lightbox currently: <img src="images/mobile-dental-repairs.gif" width="184" height="36"border="0" class="getstarted" />
simply visiting http://www.dentalfixrx.com/local-equipment-repair/download-kit.html does not work
Wrap the lightbox code into a parent div (.parent) and hide that whole div initially
HTML:
<div class="parent">
<div class="downloadkit"><form action="formmail.php" method="post" >
<input type="hidden" name="recipients" value="brian#dentalfixrx.com,billdonatojr#gmail.com,andy#dentalfixrx.com" />
<input type="hidden" name="subject"value="New DentalFixRx Lead!" />
<input type="hidden" name="good_url" value="thanks-downloadkit.php" />
<div style=" width:300px; position:absolute; right:10px; top:15px;">
<h1 style="font-size:20px; margin:5px auto;">Emergency Fix Needed?</h1>
To receive a fast response, please complete the form below and click the "submit" button.
</div>
<p><label>Name/Business:<span class="red">*</span></label><input type="text" name="name" id="name" /></p>
<p> <label> Phone:<span class="red">*</span></label> <input type="text" name="phone" id="phone" / > </p>
<p> <label>Email:<span class="red">*</span></label> <input type="text" name="email" id="email" / ></p>
<p> <label>State:</label> <input type="text" name="st" id="st" / ></p>
<p> <label>Zip Code:</label> <input type="text" name="zip" id="zip" / ></p>
<p> <label>Service Needed:</label><select name="">
<option>Select one</option>
<option>Handpiece Repair</option>
<option> -Low Speed</option>
<option> -High Speed</option>
<option> -Electric High Speed</option>
<option>Equipment Repair</option>
<option> -Autoclaves</option>
<option> -Chairs & Delivery Units</option>
<option> -Compressors</option>
<option> -Vacuum Pumps</option>
<option> -Ultrasonic Scalers</option>
<option> -Instrument Sharpening</option>
<option> -Upholstery</option>
<option> -Curing Lights</option>
<option> -Film Processors</option>
<option>Other Service</option>
</select></p>
<p> <label>Select type of request:</label><select name="">
<option>Select one</option>
<option>Just a casual question</option>
<option>I need some help but it's not super time-sensitive</option>
<option>Things are broken and I'd like them not to be!</option>
<option>I can't get things done, please reply ASAP</option>
</select></p>
<div class="clear"></div>
<input value=" " type="submit" class="download-btn" width="231px" height="36px" />
<span>Exit</span>
</form></div>
</div>
CSS:
div.parent
{
display: none;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
text-align: center;
font-family: 'Myriad Pro', Arial, sans-serif !important;
font-size: 22px !important;
border: 2px solid #aaa;
z-index: 1040;
-moz-box-shadow: 0px 0px 20px 2px #ccc;
-webkit-box-shadow: 0px 0px 20px 2px #ccc;
box-shadow: 0px 0px 20px 2px #ccc;
background: rgb(54, 25, 25); /* Fall-back for browsers that don't
support rgba */
background: rgba(255, 255, 255, .7);
}
div.downloadkit
{
position: fixed;
width: 200px;
height: 100px;
top: 50%;
left: 50%;
margin-left: -100px;
margin-top: -50px;
}
Then, on your home page of your site, add the following code: Note, this is jQuery, so just add the jquery library to your site, either in the header or just above the closing body tag -
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
Now add this code right below where you added jQuery:
<script type="text/javascript">
$(window).load(function(){ //this is pageload
$('div.parent').show(500); //500 is the animation speed
})
</script>
NOTE: If
$('div.parent').show(500);
does not work, try:
$('div.parent').css('display','block');

Resources