How to make checkbox tri-state? - css

Technology Used : Angular 5
Scenario :
I have a tree view with a checkbox. Whenever data is loaded onto the page, based on the property I am making checkbox checked or unchecked.
Todo :
Whenever property is false I want to add a cross mark in checkbox by default.
Below is my code which will render tree view with the checkbox.
<tree-root #tree [options]="options" [nodes]="nodes">
<ng-template #treeNodeTemplate let-node="node" let-index="index">
<input (change)="check(node, !node.data.checked)"
type="checkbox"
[indeterminate]="node.data.indeterminate"
[checked]="node.data.checked" class="css-checkbox">
{{ node.data.name }}
</ng-template>
</tree-root>
In the above code snippet whenever node.data.checked is true then I am making checkbox checked by default. Whenever node.data.checked comes false I want to display cross mark in the checkbox.
I have searched over the net but did not get any use full links.
Can someone help me to make this work?
Any help would be greatly appreciated. Thank you.

When you trigger [checked], the :checked pseudo-class will be applied. You can use this to target checkboxes which have been checked (either manually or programmatically).
Adding a cross in the checkbox is a little harder, as you cannot add one to an input[type=checkbox] itself. To get around this, you can hide the checkbox itself (with display: none), and make use of a <label> which is associated with the checkbox, bound by the label's for attribute. By using for, when clicking on the label you'll simulate clicking on the checkbox itself.
The label can be styled to look like the original checkbox, and also offers extra flexibility, allowing for things like a background-image (which can be used to show the cross).
Assuming your <label> elements come directly after your <input> elements, you can target them with the adjacent sibling combinator (+), with selectors such as input + label. This can be chained with the :checked pseudo-class to style the labels for the checkboxes that are checked: input:checked + label.
Here's an example:
input[type=checkbox].css-checkbox {
display: none;
}
input[type=checkbox].css-checkbox + label.css-label {
padding-left: 20px;
height: 15px;
display: inline-block;
line-height: 15px;
background-repeat: no-repeat;
background-position: 0 0;
font-size: 15px;
vertical-align: middle;
cursor: pointer;
}
input[type=checkbox].css-checkbox:checked + label.css-label {
background-position: 0 -15px;
}
.css-label {
background-image: url(http://csscheckbox.com/checkboxes/lite-x-red.png);
}
<input id="one" class="css-checkbox" type="checkbox" />
<label for="one" class="css-label"></label>
<input id="two" class="css-checkbox" type="checkbox" />
<label for="two" class="css-label"></label>
While a checkbox can only ever have two states (either checked or unchecked), what is possible with Angular is to update the associated class of the element. If you want a tick (or similar) as well, this is your best bet.
Simply create one class for when the value is truthy, one for when it is falsy, and apply the relevant class to the label depending on the value. The positive class would have a check background-image, and the negative class would have a cross. The 'default' would show up empty, as is in the above example.

Related

Remove dotted border/outline in focused option in HTML select in Firefox

I'm unable to remove the dotted border/outline around the selected/focused option in the multiple size select. It appears like this:
I have tried several things such :
:focus {
outline:none;
}
::-moz-focus-inner {
border:0;
outline: none;
}
select:-moz-focusring {
color: transparent;
text-shadow: 0 0 0 #000;
}
None of it seems to work. Here's the JSFiddle Link: https://jsfiddle.net/esdgujft/
Edit: This appears only in Firefox browser-
You can change the style of the select, but not that of the option, because that depends on the broswer you're using.
See also:
How to remove border of drop down list : CSS
This might be a solution, but it contains javascript:
https://www.w3schools.com/howto/howto_custom_select.asp
Styling options in selects is still not widely supported in 2020. If you're lucky, you might be able to style the font, color and background color, but that's pretty much it.
At least today, most browsers actually paint select boxes themselves. In the dark ages of the web, most browsers used the system toolkit directly to paint these.
If you absolutely have to fix this Firefox paint issue, I would suggest building a html/javascript solution which implements a select client side. Most UI libraries have this already built in.
Otherwise, I would suggest not using select boxes if possible. It has been noted in usability studies that select boxes are more confusing for users than simple radio buttons (or check boxes for multiple-select). As a bonus effect, radio buttons grouped together inside a form field can be styled much better in browsers. You can even style them to look like a select box.
For example, this simple code will pretty much mimic a select, but with much greater stylability. Though, you'll probably want to add a few keyboard event listeners to enable keyboard navigation.
<formfield>
<legend>Choose:</legend>
<div class="select">
<input type="radio" name="choice" value="choice-1" id="choice-1" checked/><label for="choice-1" tabindex=3>Choice 1</label>
<input type="radio" name="choice" value="choice-2" id="choice-2"><label for="choice-2" tabindex=3>Choice 2</label>
<input type="radio" name="choice" value="choice-3" id="choice-3"><label for="choice-3" tabindex=3>Choice 3</label>
<input type="radio" name="choice" value="choice-4" id="choice-4"><label for="choice-4" tabindex=3>Choice 4</label>
</div>
</formfield>
<style type="text/css">
.select {
display: flex;
flex-direction: column;
border: 1px solid #00000099;
width: 200px;
}
.select label {
padding-right: 0.5rem;
padding-left: 0.25rem;
}
.select input[type=radio] {
display: none;
}
.select input[type="radio"]:checked+label {
background: lightblue;
}
</style>
But as I said earlier, it is possibly more intuitive/user friendly to just use regular radio buttons. As a general rule-of-thumb, they have better affordance than select boxes.

Styling a checkbox without touching the checkmark

I'd just like to change the background color and remove the border so that I have a white square.
There's a working hack (except on IE) where you can hide the input and put a span element in its place that works just as well, except it hides the checkmark (the :checked status) and it requires you to use your own image.
From another StackOverflow answer:
.myCheckbox input {
display: none;
}
.myCheckbox span {
width: 20px;
height: 20px;
display: block;
background: url("link_to_image");
}
.myCheckbox input:checked + span {
background: url("link_to_another_image");
}
<label for="test">Label for my styled "checkbox"</label>
<label class="myCheckbox">
<input type="checkbox" name="test"/>
<span></span>
</label>
I'd like to use the default checkmark.
You can't style checkboxes. This is always accomplished via one hack or another. This is really a dup of this question. There are ample resources available there, I won't attempt to repeat them.

How to achieve a "mini" select style using Bootstrap (or straight CSS)?

I'm using Bootstrap's btn-mini class for "mini" buttons and am looking for something analogous to create a "mini" select element, where the select button (i.e. the part you click to show the list of options, not the list of options itself) is the same size and style as a mini button.
When I apply the btn-mini class to a select element, the font style of the select button is the same size as a mini button, but the size of the select button itself is unchanged from the default size.
Is there a different Bootstrap class I should use? Or another way to do it?
P.S. I'm working on OS X Chrome, but naturally hope there is a cross-browser compatible solution.
Just in case any Bootstrap 3 users come across this old question, here's the BS3 way:
<select class="form-control input-lg"></select>
<select class="form-control"></select>
<select class="form-control input-sm"></select>
<input class="form-control input-lg">
<input class="form-control">
<input class="form-control input-sm">
There is no input-xs, though, so you'd have to make that yourself if you wanted smaller.
.input-xs, select.input-xs {
height: 20px;
line-height: 20px;
}
HTML
<select class="btn btn-mini">
<!-- options -->
</select>
<span class="caret"></span>
CSS
select.btn-mini {
height: auto;
line-height: 14px;
}
/* this is optional (see below) */
select.btn {
-webkit-appearance: button;
-moz-appearance: button;
appearance: button;
padding-right: 16px;
}
select.btn-mini + .caret {
margin-left: -20px;
margin-top: 9px;
}
The last 2 rules are optional, it will make <select> look like <button>, I've also added a caret to it. See this fiddle.
For Bootstrap 4, to set height we can use form-control-sm class with form-control.
<select class="form-control form-control-lg">
<option>Large select</option>
</select>
<select class="form-control">
<option>Default select</option>
</select>
<select class="form-control form-control-sm">
<option>Small select</option>
</select>
And to set width of these we have to use grid column classes like .col-sm-*, .col-md-*, .col-lg-*, etc.
So put the select code in:
<div class="row">
<div class="col-md-3">
... select tag code ...
</div>
</div>
and it will look like this:
You can call the last one a "mini" select element.
This is not a final answer, but I wanted to share what I've gotten so far for anyone else curious about doing this.
As suggested by jackwanders, I've gone ahead and created a custom CSS class:
.select-mini {
font-size: 11px;
height: 20px;
width: 100px;
}
This font-size and height rules more or less get the select box to be the same size as a mini button, but the text isn't quite aligned in the same way (it's slightly shifted up). Note you need to use height not line-height to override a height rule for select elements that Bootstrap sets elsewhere. (The width rule is just to change the widget and can be whatever you want.)
My CSS-fu isn't good enough to quickly make the mini select look fully consistent with the mini buttons, and from what I can see select's behave oddly when it comes to CSS anyhow, but hopefully this will be helpful as a start to others. Meanwhile, still open to better answers!
Based on btn-xs class I've prepared this:
.input-xs{
height: 20px;
line-height: 1.5;
font-size: 12px;
padding: 1px 5px;
border-radius: 3px;
}
Note that width is not limited!
Please check this fiddle to see it in action.
Pavlo answer is better. But when mouse cursor is over caret click doenst` work. Just one thing must be added into caret class to fix it:
select.btn-mini + .caret {
margin-left: -14px;
margin-top: 19px;
pointer-events: none;
}

div input tag value changing color

i have the following problem with that code in my html tag:
<div id="searchbar"><input name="searchbar" type="text" id="searchfield" value="some text" onfocus="
if(this.value==this.defaultValue)
this.value='';
this.style.color='#11111'"
onblur="
if(this.value=='')
this.value=this.defaultValue;
this.style.color='#22222'
if(this.value==this.defaultValue)
this.style.color='#111111'"
okay, the main goal is to approve that in my searchfield where the standard value is given with "some text" should change color for only new entries.
that message "some text" will disappear on click into it. now, the new value should change its color from standard #222222 to #111111. second, when leaving this field there have to be two different ways to look at. the first possibility should be that if the new value is different from the standard one with "some text" the color should stay on #222222. second possibility would be if the new entry is the same like standard value or even no entry was made then the color should change back to the default one to #222222. so the code above works up to the last point.
if there is someone who could give me advice on how to solve that i really would appreciate. thanks a lot.
you can use the :focus pseudoclass
css on focus change the css as like this
Css
input[type="text"]{
color:white;
background:red;
padding:5px;
}
input[type="text"]:focus{
color:black;
background:yellow;
}
Live demo http://jsfiddle.net/VQ99D/
More info http://www.cssdrive.com/index.php/examples/exampleitem/focus_pseudo_class/
Instead of your long complicated code just use HTML5 placeholder
Jsfiddle Example
HTML
<div id="searchbar">
<input name="searchbar" type="text" id="searchfield" placeholder="Some Text" />
</div>
CSS
#searchbar input {
color: green;
}
input::-webkit-input-placeholder {
color: red;
}
input:-moz-placeholder {
color: red;
}​
Change the color according to your wish.
Note: Placeholder will not support in IE.
UPDATE
If you wants to use a placeholder to support all browsers use this placeholder--a jQuery plugin and tweek the style.css in it to achieve your result.

Change Style/Look of Asp:CheckBox using CSS

I want to change the standard "3D" look of the standard asp.net checkbox to say solid 1px. If I try to apply the styling to the Border for example it does just that - draws the standard checkbox with a border around it - which is valid I guess.
Anyway, is there a way to change how the actual textbox is styled?
Rather than use some non-standard control, what you should be doing is using un-obtrusive javascript to do it after the fact. See http://code.google.com/p/jquery-checkbox/ for an example.
Using the standard ASP checkbox simplifies writing the code. You don't have to write your own user control, and all your existing code/pages don't have to be updated.
More importantly, it is a standard HTML control that all browsers can recognize. It is accessible to all users, and work if they don't have javascript. For example, screen readers for the blind will be able to understand it as a checkbox control, and not just an image with a link.
I think the best way to make CheckBox looks really different is not to use checkbox control at all. Better use your own images for checked/unchecked state on-top of hyperlink or image element. Cheers.
None of the above work well when using ASP.NET Web Forms and Bootstrap.
I ended up using Paul Sheriff's Simple Bootstrap CheckBox for Web Forms
<style>
.checkbox .btn, .checkbox-inline .btn {
padding-left: 2em;
min-width: 8em;
}
.checkbox label, .checkbox-inline label {
text-align: left;
padding-left: 0.5em;
}
.checkbox input[type="checkbox"]{
float:none;
}
</style>
<div class="form-group">
<div class="checkbox">
<label class="btn btn-default">
<asp:CheckBox ID="chk1" runat="server" Text="Required" />
</label>
</div>
</div>
The result looks like this...
Simplest best way, using the ASP checkbox control with custom design.
chkOrder.InputAttributes["class"] = "fancyCssClass";
you can use something like that.. hope that helps
paste this code in your css and it will let you customize your checkbox style. however, it's not the best solution, it's pretty much displaying your style on top of the existing checkbox/radiobutton.
input[type='checkbox']:after
{
width: 9px;
height: 9px;
border-radius: 9px;
top: -2px;
left: -1px;
position: relative;
background-color: #3B8054;
content: '';
display: inline-block;
visibility: visible;
border: 3px solid #3B8054;
transition: 0.5s ease;
cursor: pointer;
}
input[type='checkbox']:checked:after
{
background-color: #9DFF00;
}
Why not use Asp.net CheckBox button with ToggleButtonExtender available from the Ajax control toolkit.
Not sure that it's really an asp.net related question.. Give this a shot, lots of good info here:
http://www.456bereastreet.com/archive/200409/styling_form_controls/
Keep in mind that the asp:CheckBox control actually outputs more than just a single checkbox input.
For example, my code outputs
<span class="CheckBoxStyle">
<input id="ctl00_cphContent_cbCheckBox"
name="ctl00$cphContent$cbCheckBox"
type="checkbox">
</span>
where CheckBoxStyle is the value of the CssClass attribute applied to the control and cbCheckBox is the ID of the control.
To style the input, you need to write CSS to target
span.CheckBox input {
/* Styles here */
}
They're dependent on the browser really.
Maybe you could do something similar to the answer in this question about changing the file browse button.
Well, I went through every solution I could find.
The Ajax Control Toolkit works, but it creates a weird html output with all kinds of spans and other styling that is hard to work with.
Using css styling with the ::before tags to hide the original control's box would work, but if you placed runat=server into the element to make it accessible to the code-behind, the checkbox would not change values unless you actually clicked in the original control.
In some of the methods, the entire line for the label would end up under the checkbox if the text was too long for the viewing screen, or would end up underneath the checkbox.
In the end, (on the adice of #dimarzionist's answer here in this page) I used an asp.net ImageButton and used the codebehind to change the image. With this solution I get nice control over the styles and can determine whether the box is checked from the codebehind.
<asp:ImageButton ID="mycheckbox" CssClass="checkbox" runat="server" OnClick="checkbox_Click" ImageUrl="unchecked.png" />
<span class="checkboxlabel">I have read and promise to fulfill the rules and obligations</span>
And in the code-behind
protected void checkbox_Click(object sender, ImageClickEventArgs e) {
if (mycheckbox.ImageUrl == "unchecked.png") {
mycheckbox.ImageUrl = "checked.png";
//Do something if user checks the box
} else {
mycheckbox.ImageUrl = "unchecked.png";
//Do something if the user unchecks the box
}
}
What's more, is with this method, The <span> you use for the checkbox's text will wrap perfectly with the checkbox.
.checkboxlabel{
vertical-align:middle;
font-weight: bold;
}
.checkbox{
height: 24px; /*height of the checkbox image*/
vertical-align: middle;
}

Resources