How can I align the checkboxes and text vertically? - html

I want the checkboxes to be parallel to each other and the text to be aligned the same.
This is what i have (i'm new to this):
<input type="checkbox" name=”liste1” value="1">This is Example1<br>
<input type="checkbox" name=”liste1” value="2">Example2<br>
<input type="checkbox" name=”liste1” value="3">Example123456<br>
<input type="checkbox" name=”liste1”` value="4">Option4<br>
This is how it looks like

When a parent element is allowed to display: flex, each child element will be aligned vertically when the align-item: center is declared
div {
display: flex;
gap: 3px;
align-items: center
}
<div>
<input type="checkbox" id="liste1" name="liste1" value="1">
<label for="liste1"> Option 1</label>
</div>
<div>
<input type="checkbox" id="liste2" name="liste2" value="2">
<label for="liste2"> Option 2</label>
</div>
<div>
<input type="checkbox" id="liste3" name="liste3" value="3">
<label for="liste3"> Option 3</label>
</div>
<div>
<input type="checkbox" id="liste4" name="liste4" value="4">
<label for="liste4"> Option 4</label>
</div>

To start with, we'll correct the original version of the code:
” is not a valid quote to use in HTML, you should use either " or '
If an HTML tag doesn't have a closing tag (e.g. <p>content</p>) then it is referred to as self-closing, and should have a slash before the closing angle bracket, like this: <input />
This gives us:
<input type="checkbox" name="liste1" value="1" />This is Example1<br />
<input type="checkbox" name="liste1" value="2" />Example2<br />
<input type="checkbox" name="liste1" value="3" />Example123456<br />
<input type="checkbox" name="liste1" value="4" />Option4<br />
This is enough to get it to look right, however generally if you want to show text next to a checkbox, you want it to be clickable and affect the checkbox. The simplest way to do this is to wrap the input and it's label text inside a <label> element, like so:
label {
display: block;
}
input,
span {
vertical-align: middle;
}
<label>
<input type="checkbox" name="liste1" value="1" />
<span>This is Example1</span>
</label>
<label>
<input type="checkbox" name="liste1" value="2" />
<span>Example2</span>
</label>
<label>
<input type="checkbox" name="liste1" value="3" />
<span>Example123456</span>
</label>
<label>
<input type="checkbox" name="liste1" value="4" />
<span>Option4</span>
</label>
(I also used a small CSS rule instead of the <br /> tag, as it's generally preferable to use CSS for layout)

your picture says column but your question says row. Which one do you want?
#container {
display: flex;
flex-direction:column;
gap: 13px;
align-items: center
}
#container2 {
display: flex;
margin-top:20vw;
gap: 83px;
justify-content:center;
}
<div id='container'>
<div>
<input type="checkbox" id="liste1" name="liste1" value="1">
<label for="liste1"> Option 1</label>
</div>
<div>
<input type="checkbox" id="liste2" name="liste2" value="2">
<label for="liste2"> Option 2 xxx</label>
</div>
<div>
<input type="checkbox" id="liste3" name="liste3" value="3">
<label for="liste3"> Option 3 xxx xxx</label>
</div>
<div>
<input type="checkbox" id="liste4" name="liste4" value="4">
<label for="liste4"> Option 4 xxx</label>
</div>
</div>
<div id='container2'>
<div>
<input type="checkbox" id="liste1" name="liste1" value="1">
<label for="liste1"> Option 1</label>
</div>
<div>
<input type="checkbox" id="liste2" name="liste2" value="2">
<label for="liste2"> Option 2</label>
</div>
<div>
<input type="checkbox" id="liste3" name="liste3" value="3">
<label for="liste3"> Option 3</label>
</div>
<div>
<input type="checkbox" id="liste4" name="liste4" value="4">
<label for="liste4"> Option 4</label>
</div>
</div>

Related

How can i avoid line break after radiobutton, make going it to a new line only with it's label, not separately

I have a code like this:
<div class="days" style="width:300px;">
<input id="monday" type="radio" name="monday" value="1">
<label for="monday"> monday</label>
<input id="tuesday" type="radio" name="tuesday" value="2">
<label for="tuesday"> tuesday</label>
<input id="wednesday" type="radio" name="wednesday" value="3">
<label for="wednesday"> wednesday</label>
<input id="thursday" type="radio" name="thursday" value="4">
<label for="thursday"> thursday</label>
</div>
And here is jsfiddle - https://jsfiddle.net/k96x02qL/
So as you see, for some values of width of the main div, sometimes there is a situation, when label is on a new line, but radiobutton is not (picture one):
picture one
So i want to see something like this (with any value of width of main div) (picture two):
picture two
How can i implement it? For some reason i can't wrap input and it's label in one more div. Only can add some styles using "name" html attribute or something like this...
You can do this without changing the HTML by not showing the input button (opacity: 0) and putting a 'pretend' button in a pseudo element before each label.
This snippet uses a couple of Unicode circles to do this but of course you may want to do it another way - e.g. a background radio gradient or an image.
This way the label as a whole goes across whole if there isn't room for it at the end of a line.
input {
opacity: 0;
position: absolute;
left: 50px
}
input:checked+label::before {
content: '\26ab';
}
label {
position: relative;
}
label::before {
content: '\26aa';
margin-right: 5px;
position: relative;
}
<div class="days" style="width:300px;">
<input id="monday" type="radio" name="monday" value="1">
<label for="monday"> monday</label>
<input id="tuesday" type="radio" name="tuesday" value="2">
<label for="tuesday"> tuesday</label>
<input id="wednesday" type="radio" name="wednesday" value="3">
<label for="wednesday"> wednesday</label>
<input id="thursday" type="radio" name="thursday" value="4">
<label for="thursday"> thursday</label>
</div>
You can use nested label/component to frame the radio component as a single box, after that add flex layout to styled the main container (div with class .days) with flex-wrap: wrap css property to auto add line break when content is bigger than container width:
.days {
display:flex;
flex-wrap: wrap;
}
<div class="days" style="width:300px;">
<label for="monday">
<input id="monday" type="radio" name="monday" value="1"> monday
</label>
<label for="tuesday">
<input id="tuesday" type="radio" name="tuesday" value="2"> tuesday
</label>
<label for="wednesday">
<input id="wednesday" type="radio" name="wednesday" value="3"> wednesday
</label>
<label for="thursday">
<input id="thursday" type="radio" name="thursday" value="4"> thursday
</label>
</div>
<br/>
Variant for your case:
<div class="days" style="width:300px;">
<div>
<input id="monday" type="radio" name="monday" value="1">
<label for="monday"> monday</label>
</div>
<div>
<input id="tuesday" type="radio" name="tuesday" value="2">
<label for="tuesday"> tuesday</label>
</div>
<div>
<input id="wednesday" type="radio" name="wednesday" value="3">
<label for="wednesday"> wednesday</label>
</div>
<div>
<input id="thursday" type="radio" name="thursday" value="4">
<label for="thursday"> thursday</label>
</div>
</div>

radio button align with text

I am trying to get my gender button to align with my radio image buttons this is the codes have so far. It gives me the text above the button. Thank you in advance
<div class="genderalign">
<p>Choose:</p>
<input type="radio" name="option" value="1" id="choose-1" />
<label for="choose-1">
<img src="/images/femaleC.png" />
</label>
<input type="radio" name="option" value="2" id="choose-2" />
<label for="choose-2">
<img src="/images/maleC.png" />
</label>
</div>
I've corrected the misplaced </div> so your HTML is now like this.
Then just apply flexbox
.genderalign {
display: flex;
align-items: center;
}
<div>
<div class="genderalign">
<p>Choose:</p>
<input type="radio" name="option" value="1" id="choose-1" />
<label for="choose-1">
<img src="/images/femaleC.png"/>
</label>
<input type="radio" name="option" value="2" id="choose-2" />
<label for="choose-2">
<img src="/images/maleC.png" />
</label>
</div>
</div>

block radio button

I want to make block for radio
This is my code.
<label> Rating
<input type="radio">great
<input type="radio">wonderful
</label>
But it does not work correctly.
label{
display: block;
}
<p> Rating</p>
<label for="radio1"><input type="radio" id="radio1" name="radiogroup1">great</label>
<label for="radio2"><input type="radio" id="radio2" name="radiogroup1">wonderful</label>
<div style="display:flex; width: 120px;">
<p style="flex:1">Rating</p>
<div style="flex:1">
<input type="radio">Good<br>
<input type="radio">Great
</div>
</div>
Do you mean something like this? You could also use a table to get the same effect.
Only with html:
<label> Rating: </label>
<p>
<input type="radio" name="rate" id="great">
<label for="great">great</label>
</p>
<p>
<input type="radio" name="rate" id="wonderful">
<label for="wonderful">wonderful</label>
</p>
Try this simple solution to display as list.
label li {
list-style:none;
}
<label> Rating
<li><input type="radio" name="group1">great</li>
<li><input type="radio" name="group1">wonderful </li>
</label>
You might not have do use any css here, you can create the structure using block elements.
Here you are trying to align a inline element in a block so you can use p, div or any other block element.
/* outline focus */
label:focus, input:focus{
outline: dotted 2px red;
}
/* No CSS to align the below elements */
<label for="rating"> Rating</label>
<p><label for="great">great</label><input type="radio" name="rating" id="great"></p>
<p><label for="wonderful">wonderful</label><input type="radio" name="rating" id="wonderful"></p>

How to move inline radio inputs under label,

So Im a complete beginner and Im stuck on some personal project. Im making forms and I want to have inputs from radio buttons all in the same line, but under the label. I know how to make all elements inline and I know that setting the block property should put them under label. But block element puts all of the inputs on its on line. What I want is all inputs to be on the same line, under lable. I can use tag in HTML, but I want to make it with CSS. Any tips?
<div class="radio" >
<label class="radio" for="age">Your age:</label>
<input type="radio" name="age">0-20
<input type="radio" name="age">20-40
<input type="radio" name="age">40-60
<input type="radio" name="age">60-80
<input type="radio" name="age">80-100
</div>
<div class="radio" >
<label class="radio" for="gender">Your gender</label>
<input type="radio" name="gender">Male
<input type="radio" name="gender">Female
</div>
just put a line break <br />
<div class="radio" >
<label class="radio" for="age">Your age:</label>
<br />
<input type="radio" name="age">0-20
<input type="radio" name="age">20-40
<input type="radio" name="age">40-60
<input type="radio" name="age">60-80
<input type="radio" name="age">80-100
</div>
<div class="radio" >
<label class="radio" for="gender">Your gender</label>
<br />
<input type="radio" name="gender">Male
<input type="radio" name="gender">Female
</div>
Set the label to display: flex; but make sure not to target the radio class or it will also effect the parent div and not work properly.
Instead of setting all of the radio buttons to display: block, setting just the label to display: block will get the effect you want. Block elements will start a new line (if needed) and force the next element to a new line as well. Since you want just the label to be on a new line by itself, setting it to display: block will do the trick.
label.radio {
display: block;
}
<div class="radio">
<label class="radio" for="age">Your age:</label>
<input type="radio" name="age">0-20
<input type="radio" name="age">20-40
<input type="radio" name="age">40-60
<input type="radio" name="age">60-80
<input type="radio" name="age">80-100
</div>
<div class="radio">
<label class="radio" for="gender">Your gender</label>
<input type="radio" name="gender">Male
<input type="radio" name="gender">Female
</div>

Show input type="radio" inline style - with icheck-me class [icheck Plugin]

I have been trying to align the input radio type inline.. but still i can't able to make them inline.. it comes in next line.
This is the coding unit.
<tr>
<td>Display on Homepage</td>
<td>
<div style="display: inline;">
<label>
<input type="radio" class='icheck-me' name="show" value="Yes" data-skin="flat" data-color="orange" checked> Yes
</label>
<label>
<input type="radio" id="radio-inline" class='icheck-me' name="show" value="No" data-skin="flat" data-color="grey"> No
</label>
</div>
</tr>
Try this-
<label class='box'>
<input type="radio" class='icheck-me' name="show" value="Yes" data-skin="flat" data-color="orange" checked> Yes
</label>
<label class='box'>
<input type="radio" id="radio-inline" class='icheck-me' name="show" value="No" data-skin="flat" data-color="grey"> No
</label>
CSS:
.box{ display:inline-block; width: 50px;} .icheck-me{ display:inline-block; float:left;}
You should use the inline on the labels too
<div>
<label style="display: inline;">
<input type="radio" class='icheck-me' name="show" value="Yes" data-skin="flat" data-color="orange" checked> Yes
</label>
<label style="display: inline;">
<input type="radio" id="radio-inline" class='icheck-me' name="show" value="No" data-skin="flat" data-color="grey"> No
</label>
</div>
This is just an example, it's not recommended to add these properties in the style attribute - you should add it to a new class or add the display: inline; to the icheck-me class
You can use display:block to get the radio buttons underneath each other. Also, align the text to the top right of the cell by using: 'style="vertical-align: top;"'
So, your code would look like this:
<tr>
<td style="vertical-align: top;">Display on Homepage</td>
<td>
<div>
<label>
<input type="radio" class="icheck-me" name="show" value="Yes" data-skin="flat" data-color="orange" checked> Yes
</label>
<label>
<input type="radio" id="radio-inline" class="icheck-me" name="show" value="No" data-skin="flat" data-color="grey"> No
</label>
</div>
</td>
</tr>
And CSS:
.icheck-me{
display:block;
}
DEMO