set text inside a check box - css

Hi is it possible to add text inside check box replacing the tick icon.
I am could not achieve it with this code could someone suggest me how to make a size selection box with a text inside it
<ion-col>
<p>Choose the size</p>
<ion-item>
<ion-checkbox >S</ion-checkbox>
</ion-item>
</ion-col>
Help me to bring out this type of UI

You can hide the input and use the label to select the checkbox. Then style your label like in the example below, using :not(:checked) and :checked selectors. Same logic can be applied to radio buttons.
ul {
padding: 0;
margin: 0;
clear: both;
}
li{
list-style-type: none;
list-style-position: outside;
padding: 10px;
float: left;
}
input[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999%;
}
input[type="checkbox"] + label {
display: inline-block;
padding: 10px;
cursor: pointer;
border: 1px solid black;
color: black;
background-color: white;
margin-bottom: 10px;
}
input[type="checkbox"]:checked + label {
border: 1px solid white;
color: white;
background-color: black;
}
<ul>
<li>
<input type="checkbox" id="check_1" name="check_1" value="check_1">
<label for="check_1">S</label>
</li>
<li>
<input type="checkbox" id="check_2" name="check_2" value="check_2">
<label for="check_2">M</label>
</li>
<li>
<input type="checkbox" id="check_3" name="check_3" value="check_3">
<label for="check_3">L</label>
</li>
<li>
<input type="checkbox" id="check_4" name="check_4" value="check_4">
<label for="check_4">XL</label>
</li>
</ul>

Updated
check updated demo here
Add the following Js to get the relevant radio value
JS:
$("body").on("click", "label", function(e) {
var getValue = $(this).attr("for");
var goToParent = $(this).parents(".select-size");
var getInputRadio = goToParent.find("input[id = " + getValue + "]");
console.log(getInputRadio.attr("id"));
});
I assume that the user select only one size at a time. if user can select the multiple size modify the example with checkbox.
Try this
Check Demo here
HTML:
<div class="select-size">
<input type="radio" name="s-size" id="small" checked/>
<input type="radio" name="s-size" id="medium" />
<input type="radio" name="s-size" id="large" />
<input type="radio" name="s-size" id="x-large" />
<input type="radio" name="s-size" id="xx-large" />
<label for="small">S</label>
<label for="medium">M</label>
<label for="large">L</label>
<label for="x-large">XL</label>
<label for="xx-large">XXL</label>
</div>
CSS:
.select-size input{
display: none;
}
label {
display: inline-block;
width: 50px;
height: 50px;
text-align: center;
border: 1px solid #ddd;
line-height: 50px;
cursor: pointer
}
#small:checked ~ label[for="small"],
#medium:checked ~ label[for="medium"],
#large:checked ~ label[for="large"],
#x-large:checked ~ label[for="x-large"],
#xx-large:checked ~ label[for="xx-large"] {
background: #999;
color: #ffffff;
}

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.

Style radio inputs as button when input is within label tag

My html:
<div class="product-addon product-addon-extra-tip">
<p class="form-row form-row-wide addon-wrap-2004-extra-tip-0-0">
<label><input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2">2</label>
</p>
<p class="form-row form-row-wide addon-wrap-2004-extra-donation-to-trafficking-survivors-0-1">
<label><input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="5">5</label>
</p>
</div>
I'm trying to style these radio inputs to look like buttons, and I'm almost there. The problem is that given the current construct (which I can't directly change), I can't figure out how to make the :checked option look different than the rest.
You can see in the jsfiddle where I'm falling short. Is this possible?
http://jsfiddle.net/2gdotu21/1/
Via CSS, input set in front of label and correct attribute used, you can apply a different style if input is :checked or not.
See: https://www.w3.org/wiki/HTML/Elements/label & further more https://www.w3.org/TR/WCAG20-TECHS/H44.html
label {/* button unchecked add your style*/
color:red
}
label:before {/* button checked add your style*/
content:'$';
font-size:1rem;
}
input:checked + label {
color:green;
}
[type=radio]{ /* hide it ? use any methode but display:none; */
position:absolute;
right:200%;
}
<input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2" id="addon-2004-extra-tip-0[]" />
<label for="addon-2004-extra-tip-0[]">2</label>
<input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2" id="addon-2004-extra-tip-0[1]" />
<label for="addon-2004-extra-tip-0[1]">300</label>
<input type="radio" class="addon-radio" name="addon-2004-extra-tip-0[]" value="2" id="addon-2004-extra-tip-0[2]" />
<label for="addon-2004-extra-tip-0[2]">14</label>
<!-- same name to allow only one checked in this demo -->
else with your structure, integrate the radio within the design of the button http://codepen.io/gc-nomade/pen/LketK (oldish glassy button)
example of your code to change bg color
.product-addon-extra-tip label {
float: left;
width: auto;
min-width: 60px;
margin: 3px;
border-radius: 4px;
border: 1px solid #D0D0D0;
overflow: auto;
color: black;
font-size: 1.2rem;
text-align: center;
padding: 5px 0;
display: block;
line-height: 1.3rem;
}
.product-addon-extra-tip label input {}
.product-addon-extra-tip label:before {
content: '$';
}
label {
position: relative;
}
input {
position: absolute;
top: -15px;
z-index: -1;
box-shadow: 0 0 0 200px tomato;
}
input:checked {
box-shadow: 0 0 0 200px green;
}
<div class="product-addon product-addon-extra-tip">
<p class="form-row form-row-wide addon-wrap-2004-extra-tip-0-0">
<label><input type="radio" class="addon addon-radio" name="addon-2004-extra-tip-0[]" data-raw-price="0" data-price="" value="2"> 2 </label>
</p>
<p class="form-row form-row-wide addon-wrap-2004-extra-tip-0-1">
<label><input type="radio" class="addon addon-radio" name="addon-2004-extra-tip-0[]" data-raw-price="0" data-price="" value="5"> 5 </label>
</p>
</div>

Grouping form elements together

I'm trying to create two css container classes that can be used to:
Vertically align form elements using .group.
Horizontally align form elements using .group.group--inline.
Each for element will use the class .group__item to make sure there's 16px vertical and horizontal distance between the form elements. For example:
.group__item { margin-top: 16px; }
I however want to sure that the entire height and width of the .group can be used for the form elements and that there is no unwanted whitespace. Not having any margin around our components makes it easier to properly layout them.
To negate the margin on the .group__item's I'm adding it as negative margin to the .group and .group--inline. For example:
.group { margin-top: -16px; }
I'm wondering if there are any negative side effects to giving the .group container a negative margin?
function toggleGroupBorder() {
var groups = document.querySelectorAll('.group');
for (var i = 0, j = groups.length; i < j; i++) {
groups[i].classList.toggle('group--show-border');
}
}
.container {
margin: 32px;
padding: 32px;
border: 1px solid #99f;
}
.group {
margin-top: -16px;
}
.group.group--show-border {
border: 1px solid #f99;
}
.group .group__item {
display: block;
margin-top: 16px;
}
.group.group--inline {
margin-left: -16px;
}
.group.group--inline .group__item {
margin-left: 16px;
display: inline-block;
}
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial;
margin: 0;
}
input {
height: 32px;
padding: 0 8px;
}
button {
height: 32px;
padding: 0 24px;
border: none;
}
<button onclick="toggleGroupBorder();">Toggle Group Border</button>
<h2>Vertical field alignment using <code>.group</code></h2>
<div class="container">
<div class="group">
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
</div>
</div>
<h2>Horizontal field alignment using <code>.group.group-inline</code></h2>
<div class="container">
<div class="group group--inline">
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<input class="group__item" type="text" />
<button class="group__item" >Default</button>
</div>
</div>
Or see this CodePen

Spacing form elements with css

I am looking for a neat way to space my form elements but i am not so sure how to do it.
Currently,i am using the ordinary <br/> to space but i wonder if there was a better way of doing this using class or id
<form>
Message: <input type="text" class="msg" name="message" /><br/><br/>
<input type="submit" class="c-add" value="Add" />
<input type="submit" class="c-update" value="Update" />
</form>
I am thinking of
.c-add + .c-update{
margin-right:100px;
}
but that's not valid css.What would be the solution?
.c-add + .c-update is a valid CSS selector. It selects all elements with the "c-update" class that follow immediately an element with the "c-add" class. Example: DEMO (CSS Selector Reference)
Solution
You can seperate multiple selectors with a comma. You do not need to give each input a unique class name. That's only necessary if you want to style them uniquely. Since you did not provide information on how the expected result should look like, i made a demo with different solutions:
DEMO
HTML markup:
<form class="form">
<label>Message:</label><input type="text" class="msg" name="message" />
<input type="submit" class="add" value="Add" />
<input type="submit" class="update" value="Update" />
</form>
Note that i wrapped "Message" with label, which is better markup.
CSS to make a one-row inline form:
.form input {
margin-right: 0.5em;
}
.form label {
float: left;
margin-right: 0.5em;
}
CSS to make a multiple-row form:
.form input {
display: block;
margin-bottom: 1em;
}
.form label {
float: left;
margin-right: 0.5em;
}
​You can mix both approaches by using specific classes for each input element or type.
Use a comma to separate selectors.
.c-add, .c-update {
The structure, perhaps this way:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<ul>
<li>
<label>
<span>Name</span>
<input type="text" name="name" />
<small class="errorText"><?php echo ($_POST["name"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Email</span>
<input type="text" name="email" />
<small class="errorText"><?php echo ($_POST["email"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Subject</span>
<input type="text" name="subject" />
<small class="errorText"><?php echo ($_POST["subject"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<label>
<span>Message</span>
<textarea name="message" cols="80" rows="7"></textarea>
<small class="errorText"><?php echo ($_POST["message"] == "") ? "This field is required" : ""; ?></small>
</label>
</li>
<li>
<input type="submit" name="submit" value="Send" />
</li>
</ul>
</form>
And the CSS for the same:
* {font-family: Segoe UI, Tahoma;}
h1 {font-weight: bold; font-size: 14pt; padding: 5px 0; margin: 5px 0; border: 1px solid #999; border-width: 1px 0;}
input[type='submit'] {padding: 5px 20px; cursor: pointer;}
ul, li {display: block; list-style: none; margin: 0; padding: 0;}
ul li {padding: 5px 0;}
ul li label span {display: block; cursor: pointer;}
ul li label .errorText {color: #f00; font-weight: bold; vertical-align: top;}
ul li label textarea {width: 300px;}
You can see a live demo here: Demo

Strange scrolling behavior in IE with checkboxes in a scrollable div

I have a "multiselect" control that looks like this (sorry for the long id names, they are kinda autogenerated because this whole thing is being generated by a custom tag):
<div class="default-skin-outer" id="myMapSelect_multiSelectOuterDiv">
<div class="default-control" id="myMapSelect_multiSelectControlDiv">
<span class="default-icon-check-text" id="myMapSelect_multiSelectControlCheckWrapperSpan">
<span class="default-icon default-icon-check" id="myMapSelect_multiSelectControlCheckIconSpan"></span><span class="default-icon default-icon-text" id="myMapSelect_multiSelectControlCheckTextSpan">Check All</span>
</span>
<span class="default-icon-uncheck-text" id="myMapSelect_multiSelectControlUncheckWrapperSpan">
<span class="default-icon default-icon-uncheck" id="myMapSelect_multiSelectControlUncheckIconSpan"></span><span class="default-icon default-icon-text" id="myMapSelect_multiSelectControlUncheckTextSpan">Uncheck All</span>
</span>
</div>
<div class="default-skin-inner" id="myMapSelect_multiSelectInnerDiv">
<ul class="default-multiselect">
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="0" class="default-checkbox" id="myMapSelect0" name="myMapSelect"> Zero
</label>
</li>
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="1" class="default-checkbox" id="myMapSelect1" name="myMapSelect"> One
</label>
</li>
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="2" class="default-checkbox" id="myMapSelect2" name="myMapSelect"> Two
</label>
</li>
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="3" class="default-checkbox" id="myMapSelect3" name="myMapSelect"> Three
</label>
</li>
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="4" class="default-checkbox" id="myMapSelect4" name="myMapSelect"> Four
</label>
</li>
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="5" class="default-checkbox" id="myMapSelect5" name="myMapSelect"> Five
</label>
</li>
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="6" class="default-checkbox" id="myMapSelect6" name="myMapSelect"> Six
</label>
</li>
<li class="default-multiselect">
<label class="default-label">
<input type="checkbox" value="7" class="default-checkbox" id="myMapSelect7" name="myMapSelect"> Seven
</label>
</li>
</ul>
</div>
</div>
The CSS for this whole thing is:
div.default-skin-outer {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
width: 300px;
height: auto;
padding: 2px;
background-color: #f0f0f0;
border: 1px solid #999999;
}
div.default-skin-inner {
overflow: auto;
width: 300px;
height: 100px;
}
div.default-control {
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
width: auto;
border: 1px solid #555555;
background-color: #999999;
color: #f0f0f0;
vertical-align: middle;
padding: 2px;
margin-bottom: 2px;
font-weight: bold;
overflow: hidden;
}
ul.default-multiselect {
padding: 0px;
margin: 0px;
white-space: nowrap;
}
ul.default-with-padding {
padding: 0px;
padding-left: 20px;
margin: 0px;
white-space: nowrap;
}
li.default-multiselect {
list-style-type: none;
}
label.default-label {
display: block;
padding: 2px;
}
input.default-checkbox {
width: 13px;
height: 13px;
padding: 0;
margin: 0;
vertical-align: bottom;
position: relative;
top: -1px;
*overflow: hidden;
}
span.default-icon {
background-image: url("/resources/authoring/jqueryui/custom-theme/images/ui-icons_ffffff_256x240.png");
display: inline-block;
height: 16px;
width: 16px;
overflow: hidden;
}
span.default-icon-text {
width: auto;
background: none;
}
span.default-icon-text:hover {
text-decoration: underline;
cursor: pointer;
}
span.default-icon-check-text {
float: left;
}
span.default-icon-uncheck-text {
float: right;
}
span.default-icon-check {
background-position: -64px -144px;
}
span.default-icon-uncheck {
background-position: -96px -128px;
}
This works beautifully in Firefox. The checkboxes scroll without any problem in the scrollable div. But when I looked at this in IE8, it looks terrible.
Firstly, the extra checkboxes bleed outside the main div. Secondly (and this is the really strange thing) when I use the scroll bar, the text scrolls, but the checkboxes do not. They simply stay in place while the text scrolls. I tried googling for a solution but was unable to come up with anything.
Thanks!
UPDATE
So I found out that if I remove the funky part in the checkbox styling:
vertical-align:bottom;
position:relative;
top: -1px;
*overflow: hidden;
It works fine. But I put that in to make sure my labels and checkboxes are lined up properly.
Oh yes as far as the compatibility view is concerned, this is IE8 running under compatibility mode.
In response to the comments about inherited styles, here are styles that the checkbox inherits:
input {
border:1px solid #CFCFCF;
color:#000000;
font-family:Arial,Verdana,Sans-Serif;
font-size:12px;
padding-left:4px;
}
li.default-multiselect {
list-style-type:none;
}
ul.default-with-padding {
white-space:nowrap;
}
table {
empty-cells:show;
}
html, body {
line-height:16px;
}
I don't see anything that could potentially interfere...
There seems to be some strange interaction between inherited styles and the styles I have defined. That much is clear from Jacob's and Ray's comments since they were able to slap this code onto a page and have it render fine in IE without any issues.
I was able to make it behave properly by removing position:relative from the input.default-checkbox style.
I'm assuming that some sort of bizarre interaction is making the checkboxes think they are positioned statically or absolutely (or something) due to which they don't scroll. At least I think that's the reason; someone may be able to provide a better reason and shed light on this. At any rate, by removing the position:relative, I was able to make the strange scrolling-behavior stop. Thanks for helping me figure this out!
As #rossisdead suggested, adding position: relative to the scrolling element solves the problem. I also had to add position: relative to the parent of the scrolling element.
Make sure you don't have position: fixed set on your checkboxes in any other stylesheets.

Resources