How to custom style images and radio buttons? [closed] - html

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
How to use radio button and images to make a similar webpage?
In this, I have made a label and din that I have added a radio button, an image tag, and spam for the name. Then I tried styling it using bootstrap.
Html:
<div>
<h2> Choose Accomodation Type </h2>
<div >
<label className="btn">
<input type="radio" name="test" id="option1" autocomplete="off" checked />
<img src={appartment} />
<span class="checkmark"></span>
<p>Apartment</p>
</label>
<label className="btn">
<input type="radio" name="test" id="option1" autocomplete="off" checked />
<img src={house} />
<span class="checkmark"></span>
<p>house</p>
</label>
</div>
<div>
<button className="btn btn-light btn-lg"> Cancel</button>
<button className="btn btn-outline-primary btn-lg"> Next </button>
</div>
img {
width:150px;
height: 150px;
margin:30px;
padding: 20px;
}
.btn{
margin: 15px;
padding: 5px;
}
[type=radio] {
margin:0;padding:0;
-webkit-appearance:none;
-moz-appearance:none;
appearance:none;
}

Add a label for your input.
Add visibility: hidden for the input.
Put a div inside the label and start styling. You can use :checked pseudo class to see if the radio button is selected.
HTML
<label for="my-btn">
<div></div>
</label>
<input id="my-btn" type="radio"/>
CSS
input {
visibility: hidden;
}
div {
/* custom styles */
}
input:checked div {
/* custom styles */
}
Now once you click on the label which contains your customized div, the radio button will be checked.

Related

Image instead of radio button [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm probably making a small mistake, but I tried to follow the answers to the previously asked questions on the subject.
I'm trying to replace my radio button with an image. The image shows perfectly and I can click the image to select the radio button. But the mark-up won't hide my radio button. It does when I hardcode it in the HTML code, but well... let's not go there!
.radioImg {
display:inline;
width:30%;
}
.radioImg input[type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
/* IMAGE STYLES */
.radioImg input[type=radio] + img {
cursor: pointer;
}
/* CHECKED STYLES */
.radioImg input[type=radio]:checked + img {
outline: 2px solid #f00;
}
<form action= "{{ url_for('mechanics') }}" method="POST">
<fieldset>
<legend>What type of system do you want?</legend>
<br />
<label class="radioImg">
<input checked="checked" name="system" type="radio" value="type1">
<img src="static/type1.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type2">
<img src="static/type2.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type3">
<img src="static/type3.jpg">
</label>
<br />
<br />
<button type="submit" value="Next">Next</button>
</fieldset>
</form>
I tried to follow: Use images instead of radio buttons,
but the radio buttons still won't hide as you can see:
The weird thing is that when I try to replicate your problem with your code, I don't run into the same problem. I think you have some extra CSS somewhere affecting your HTML.
.radioImg {
display: inline;
width: 30%;
}
.radioImg input[type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
/* IMAGE STYLES */
.radioImg input[type=radio]+img {
cursor: pointer;
}
/* CHECKED STYLES */
.radioImg input[type=radio]:checked+img {
outline: 2px solid #f00;
}
<form action="{{ url_for('mechanics') }}" method="POST">
<fieldset>
<legend>What type of system do you want?</legend>
<br />
<label class="radioImg">
<input checked="checked" name="system" type="radio" value="type1">
<img src="static/type1.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type2">
<img src="static/type2.jpg">
</label>
<label class="radioImg">
<input name="system" type="radio" value="type3">
<img src="static/type3.jpg">
</label>
<br />
<br />
<button type="submit" value="Next">Next</button>
</fieldset>
</form>
Apparently, I should clear my cache every time I change the markup of /static/... files.
This is what you were looking for
/* HIDE RADIO */
[type=radio] {
position: absolute;
opacity: 0;
width: 0;
height: 0;
}
/* IMAGE STYLES */
[type=radio] + img {
cursor: pointer;
}
/* CHECKED STYLES */
[type=radio]:checked + img {
outline: 2px solid red;
}
<label>
<input type="radio" name="test" value="small" checked>
<img src="http://placehold.it/40x60/0bf/fff&text=A">
</label>
<label>
<input type="radio" name="test" value="big">
<img src="http://placehold.it/40x60/b0f/fff&text=B">
</label>

Is it possible to set special styles to checkboxes when all of them are unchecked using only CSS?

It's simple to apply CSS to checked/unchecked checkboxes (using :checked) and their labels. However, I need to apply another style to all checkboxes when all of them are unchecked.
This is relatively simple to implement using JavaScript but I have a widget which I'm not eager to modify so I'd like to know whether there's a CSS trick for that. I suspect that there isn't, but there's always somebody who's smarter :)
PS well, the html bit looks like this, nothing special:
<div>
<input type=checkbox id=chkFilterMath>
<span><label for=chkFilterMath>Math</label></span>
<input type=checkbox id=chkFilterHist>
<span><label for=chkFilterHist>History</label></span>
...
</div>
Current CSS uses the
input:not(:checked) + span label
selector to apply the styles to unchecked checkboxes/labels.
A simplified example may be found here: https://jsfiddle.net/k56hz8va/ I'd like to set color: black to the labels when all of checkboxes are unchecked.
Lasciate ogni speranza, voi ch’entrate
No. There are no such CSS selectors that allows to select previous DOM elements in dependence on state of following elements. See Is there a “previous sibling” CSS selector? and Is there a CSS parent selector? posts for details.
There is a hack around this that I use:
Hide the input itself, but keep the label. Then use the pseudo element ::before to insert some icon to denote checked / unchecked.
Here's a demo: https://codepen.io/anon/pen/WdrdPE
and the code:
<input id="option_1" type="checkbox"><label for="option_1">thing 1</label>
<input id="option_2" type="checkbox"><label for="option_2">thing 2</label>
<input id="option_3" type="checkbox"><label for="option_3">thing 3</label>
css:
#import url('https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
input {
display:none;
}
label {
display:block;
}
input:checked + label {
color:red;
}
label:before {
content:"\f1db";
margin-right:.3em;
font-family:Fontawesome;
}
input:checked + label:before {
content:"\f058";
margin-right:.3em;
font-family:Fontawesome;
}
The #sashaikevich's idea is great but requires some work to solve your question. You could place the labels after all inputs. Then your CSS and HTML will be bulky, but you will be able to control styles of the labels in dependence on all inputs state.
Try to run the snippet below. The latest rule has highest priority, therefore if any (at least one) of checkboxes is checked, then the labels is black. Otherwise the labels is red.
[type=checkbox] {
display: none;
}
#check-box-1:checked~[for=check-box-1] .glyphicon-unchecked,
#check-box-2:checked~[for=check-box-2] .glyphicon-unchecked,
#check-box-3:checked~[for=check-box-3] .glyphicon-unchecked,
#check-box-1:not(:checked)~[for=check-box-1] .glyphicon-check,
#check-box-2:not(:checked)~[for=check-box-2] .glyphicon-check,
#check-box-3:not(:checked)~[for=check-box-3] .glyphicon-check
{
display: none;
}
[for] {
color: red;
}
[type=checkbox]:checked~[for] {
color: inherit;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="check-box-set">
<input id="check-box-1" type="checkbox" />
<input id="check-box-2" type="checkbox" checked="checked" />
<input id="check-box-3" type="checkbox" />
<label for="check-box-1">
<span class="glyphicon glyphicon-unchecked" aira-hidden="true"></span>
<span class="glyphicon glyphicon-check" aira-hidden="true"></span>
1
</label>
<label for="check-box-2">
<span class="glyphicon glyphicon-unchecked" aira-hidden="true"></span>
<span class="glyphicon glyphicon-check" aira-hidden="true"></span>
2
</label>
<label for="check-box-3">
<span class="glyphicon glyphicon-unchecked" aira-hidden="true"></span>
<span class="glyphicon glyphicon-check" aira-hidden="true"></span>
3
</label>
</div>
In the example I use Bootstrap Glyphicons. But it is possible to use another glyps, images or CSS shapes.

Triggering :active state html label element

I've been passed a recently-appeared bug which involves a label element nolonger being set to :active.
My (probably) simple question is: How would I toggle the label's :active state when the label is clicked? I can check dev tools and test what the css will do if the label were :active (see screenshot below) but how would I get the label to actually be :active? The :hover state works, so I'm hoping :active can too.
This had previously been working, apparently and, as you can see from the screenshot, the css for an :active state had already been written.
Just to be clear: I don't want to add an 'active' class to the element; I want to exploit the in-build :active state, similar to :hover.
Thanks.
there is no active or selected state for label element.
What you see is a bootsrap trick imo
You are free to give any class to get it worked, e.g.
$('.u').on('click', function(){
$('.u').removeClass('e');
$(this).addClass('e');
});
.u:focus {
background-color: red;
}
.u {
background-color: lightgrey;
}
.e {
background-color: red;
}
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js">
"http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"</script>
<div class="col-xs-12 col-md-10">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-md u e" >
<input type="radio" name="weight" value="1">Small (0-3 lbs)
</label>
<label class="btn btn-md u">
<input type="radio" name="weight" value="2"> Medium (3-7 lbs)
</label>
<label class="btn btn-md u">
<input type="radio" name="weight" value="3"> Large (7+ lbs)
</label>
</div>
</div>
</div>

Changing the color of checkbox if checked [duplicate]

This question already has answers here:
How to style a checkbox using CSS
(43 answers)
Closed 6 years ago.
I am trying to change the background color and border of a checkbox but it is not working.
HTML:
<label for="checkbox1" class="checkbox">
<input id="checkbox1" type="checkbox" role="checkbox" /><span class="custom">Checkbox</span>
</label>
CSS:
.checkbox input:checked {
border-color: red;
background-color:red;
}
JSFiddle Demo
UPDATE:
I can't change the markup
Use below option
input[type="checkbox"]#checkbox1:checked + span {
border-color: red;
background-color:red;
}
You will have to wrap the checkbox in a span to do it.
Found a Fiddle with Ex.
<span class="bigcheck">
<label class="bigcheck">Cheese
<input type="checkbox" class="bigcheck" name="cheese" value="yes"/>
<span class="bigcheck-target"></span>
</label>
</span>
Hope it helps :)

How i can customize my checkbox with css and Bootstrap?

Im crating a form with many fields and a checkboxes, the problem is that im trying to customize the checkbox(unsuccessfully) according with the style of my form.
As you can see in the picture, the tick of the checkbox is black, and i need it green. How can I change the color? How can I change the size of checkbox?
This is my HTML:
<div id="Cajamitad1">
<form role="form">
<div class="checkbox">
<label><input type="checkbox" value="" >
<a class="FontStyle">Vegano</a>
</label>
</div>
< div class="checkbox">
<label><input type="checkbox" value="">
<a class="FontStyle">Diabetico</a>
</label>
</div>
<div class="checkbox">
<label><input type="checkbox" value="">
<a class="FontStyle">Celiaco</a>
</label>
</div>
</form>
</div>
and this my CSS:
#ContForm{
display: inline;
box-sizing: border-box;
}
.checkbox{
margin: 10px;
padding: 10px;
}
.checkbox input[type="checkbox"]{
margin-left: 5px;
}
#Cajamitad1{
margin-top: 10px;
width: 40%;
float: left;
padding-left: 200px;
}
Correct me if I'm wrong, but as far as I know this is not possible.
Here is my reasoning: if you inspect the bootstrap checkbox then you see that the checkbox itself is drawn by an <input type="checkbox"> and you have very limited control over that. Some browsers let you change the size of the checkbox but not all of them.
So the only thing you can do is to make your own checkbox, e.g. by using two images and some css.
Or often more mobile friendly are just two buttons side by side "yes" and "no". Then if you click one it becomes active, like some kind of toggle button.