How to automatically select radio button by clicking the padding around it? - html

I read from this question: Can the label tags for attribute be associated with a normal div? that is not possible to give a div the attribute for to be associated to the id in the input.
Normally if I give a for to label and id to input the radio button is automatically selected whenever the user clicks the text in the label.
Let's say I create a form with many radio buttons and each one of them are in a span or a div so I can dive a border that wraps both label and radio button, let's also say I give a 20px padding all directions, how can I make clickable the all area within the border so it autotomatically selects the radio button associated to the div?
I wrote this below but just found out div doesn't recognize for attribute:
HTML
<div class="radio-btn" for="rdBanana"><input type="radio" name="fruit" value="banana" id="rdBanana"><label>Banana</label></div>
<div class="radio-btn" for="rdOrange"><input type="radio" name="fruit" value="orange" id="rdOrange"><label>Orange</label></div>
<div class="radio-btn" for="rdKiwi"><input type="radio" name="fruit" value="kiwi" id="rdKiwi"><label>Kiwi</label></div>
CSS
.radio-btn {
display: inline-block;
border: 1px solid black;
padding: 20px;
}
How can i make all the padding clickable to automatically select radio button?

Place the button inside the <label>.
Instead of <input type="radio"><label>Banana</label>, use <label><input type="radio">Banana</label>.
Everything inside <label> will become clickable and toggle the button.

Related

Hiding a checkbox and creating a custom button

I am trying to hide checkbox, but still allow it to check when a div / button is pressed.
My code for the custom buttons are
<div class="checkbox flex item center-text bg-theme"
style="border: 1px solid #e3e3e3;border-radius: 12px;">
<i class="twf twf-round-pushpin"></i>
<label for="nearest" class ="color-theme right-5">Nearest to me </label>
<input type="checkbox" id="nearest" class = "filtering" value="nearest" >
I've tried adding visibility:none and display:none; but this just doesn't work, when i click the buttons, nothing happens.
You might try to add opacity:0; in the css.
If you do not want the box to be directly checkable, use pointer-events:none in addition to that
Add display:inline-block on the first div so it will take only the minimum place and won't leave a big blank.
Then for the checkbox, using display:none works fine, I've added an onclick callback to log the state of the checkbox on each click on the label.
Try this sample:
<div class="checkbox flex item center-text bg-theme" style="border: 1px solid #e3e3e3;border-radius: 12px; display:inline-block;">
<i class="twf twf-round-pushpin"></i>
<label for="nearest" class ="color-theme right-5">Nearest to me </label>
<input type="checkbox" id="nearest" class = "filtering" value="nearest" style="display: none;" onclick="console.log(this.checked)">
</div>

How to align a label with checkbox

span{
text-align: justify;
}
<input type="checkbox" name="type"><span>Excessive Reinforcement</span><br>
I want to align the label for excessive reinforcement checkbox like image2
For example
Thanks in advance
First of all use <label> instead of <span>.
If we use bootstrap we generally manage this with classes but here if we talk about custom css this can be a solution.
label{
text-align: justify;
float: left;
line-height: 20px;
}
input{
float:left;
}
<input type="checkbox" id="check" name="type"><label for="check">Excessive<br>Reinforcement</label><br>
Above i added id in checkbox and for in label so that checkbox will be selected on click of label also.
If you can change the HTML
The best and new method to use checkbox is
<label><input type="checkbox" name="type">Excessive Reinforcement</label>
span {
text-align: justify;
}
.make-table {
display: table-cell;
/* make it behave like table-cell. so that they fall beside each other. */
}
<div class="any-class">
<label><span class="make-table"><input type="checkbox" name="type"></span>
<span class="make-table">Excessive<br> Reinforcement</span>
</label>
</div>
<hr>
<div style="color:red">Wrap it inside any-class and align as you want.
<br>I added LABEL tag, so that, even if your user clicks on the text, the checkbox will work.</div>
Make this simple change!
This is one way of doing it:
<label for="type-1">
<input id="type=1" type="checkbox" name="type"> Excessive Reinforcement
</label><br>
When using input elements, you should always provide a label with the for attribute assigned the id of the input element. And also make sure the input element ids are unique.

:first-child not working on <input> within <p>

((I'm a beginner so this probably has a simple solution, but I've searched with all the keywords I can think of and can't find what I'm looking for. I appreciate any help — clearly I am in over my head.))
I'm trying to give the first radio button within a paragraph a different margin than the rest of the radio buttons. This is what I have:
p.question:first-child input[type=radio] {
margin: 5px;
}
<p class="question">
<input type="radio" name="sample1" /> Yes
<input type="radio" name="sample1" /> No
<input type="radio" name="sample1" /> Maybe-So
</p>
Shouldn't this target the first radio button within a paragraph? Please be gentle.
You're putting the first-child on the <p>, not the radio button.
Try this.
p.question input[type=radio]:first-child {
margin: 5px;
}
Here's a jsfiddle.

How to display text next to radio button?

got a question on radio buttons. Got a html code and css code below but what is happening is that I have 2 radio buttons and each radio button has a text next to it. The first radio button states "Male"and the other radio button states "Female".
Now "Male" is display next to the radio button which is fine, but "Female" is displayed below the radio button. If I make the table where the whole radio buttons and text are stored wider, then both "Male" and "Female" are displayed under their radio buttons and not next to them.
So my question is what needs to be changed in my css/html code in order to display "Male" and "Female" next to their radio buttons?
Below is the html code:
<table id="replies">
<tr>
<th colspan="2">
Replies
</th>
</tr>
<tr>
<td>Gender: </td>
<td align="left">
<div class="replytd">
<input type="radio" name="reply" value="male" class="replyBtn" /><span class="replyspan">Male</span>
</div>
<div class="replytd">
<input type="radio" name="reply" value="female" class="replyBtn" /><span class="replyspan">Female</span>
</div>
</td>
</tr>
</table>
below is the css code:
#replies{
display:inline-block;
vertical-align:top;
min-width:15%;
max-width:15%;
}
#replies th{
border-collapse:collapse;
border:1px solid black;
}
#replies td{
border-collapse:collapse;
border:1px solid black;
padding:1%;
}
I would use the <label> tag instead of the <span> tag, because they have a better accessibility and correlation to the <input> element.
you can add this in your css code to change display mode about "div"
.replytd
{
float:left;
}
white-space:nowrap helps but could be a slight overkill, given that simply setting a column width would work
That being sad, a more correct approach would be using a <label> element which gets correctly recognized by screen-readers etc. and serves its semantic purpose. Label elements can be implicitly (without a for attribute) linked with inputs when being their parent, like
<label><input type="radio" name="reply" value="male" class="replyBtn" />Male</label>
You set max-width:15%; which is not enough to accommodate the content so the content starts to wrap. Increase or remove max-width.

Break line after input without html markup

I am trying to display a number of inputs and their corresponding labels. They are both inline elements, and I have a working solution with adding a br tag at the end like so
<label for="hello"></label>
<input id="hello" type="text" />
<br>
<label for="stackoverflow"></label>
<input id="stackoverflow" />
Id like to solve this without extraneous HTML markup, i.e with CSS. What is the easiest way to do this?
I have viewed other questions similar to this, but aligning by row instead of by column.
You can wrap the labels around your inputs and display them as blocks:
<style>
label { display: block; }
</style>
<label>
Hello: <input name="hello">
</label>
<label>
StackOverflow: <input name="stackoverflow">
</label>
Note that when you do this you don't need to use the for="name" attribute.
The other way (if you don't want the labels wrapped around your inputs) is to float the labels to the left:
<style>
label { float: left; clear: left; }
</style>
However, I generally prefer a little more markup, something that connects the label and the input into one logical element, called a field:
<div class="field">
<label for="hello">Hello</label>
<input name="hello">
</div>
<div class="field">
<label for="stackoverflow">Stackoverflow</label>
<input name="stackoverflow">
</div>
Because each field is a div, it will display as a block automatically, but you can control it for individual fields as well.
Try to set display:inline-block for your input and label elements. So you can add all block element specific css like witdh or margin-bottom.
You can also set your input and label to display:block and add margin-bottom only to the the input. Or you can reverse it and add a margin-top to your labels ;)
If you want to remove the margin on the last element you can use input:last-child {margin-bottom:0;}
input, label {display:block;}
input {margin-bottom:18px;}
input:last-child {margin-bottom:0;}
/* Or to be specific you can use the attribut-selector
which only works on inputs with type="text"
*/
input[type="text"]:last-child {margin-bottom:0;}