I want to design a series of horizontal checkboxes where the checkboxes are located on the right side of the labels.
This is what I have written where the labels are located on the left side.
.add-margin {
margin-top: 10px !important;
}
<div>
<div ng-init="loaded()" ng-class="{'add-margin': descriptionsAvailable}" class="checkbox-inline" ng-repeat="opt in options track by $index">
<label>
<input ng-disabled="answer['none']" type="checkbox" ng-model="answer[opt]" ng-true-value="true" ng-false-value="false" name="checkbox-answer-input" display= inline-block >
<span>{{opt}}</span><br/>
<span><i>{{descriptions[$index]}}</i></span>
</label>
</div>
</div>
How can I fix this?
Use the code like this
<div>
<div ng-init="loaded()" ng-class="{'add-margin': descriptionsAvailable}" class="checkbox-inline" ng-repeat="opt in options track by $index">
<label>
<span>{{opt}}</span>
<input ng-disabled="answer['none']" type="checkbox" ng-model="answer[opt]" ng-true-value="true" ng-false-value="false" name="checkbox-answer-input" display= inline-block >
<br/>
<span><i>{{descriptions[$index]}}</i></span>
</label>
</div>
</div>
If I understand your question correctly, you want the checkbox on left and the text on right with opt and description on separate rows.
You need to add some styles like following and provide relevant classes. I hope this helps.
HTML
<div>
<div ng-init="loaded()" ng-class="{'add-margin': descriptionsAvailable}" class="checkbox-inline" ng-repeat="opt in options track by $index">
<label class="container">
<input ng-disabled="answer['none']" type="checkbox" ng-model="answer[opt]" ng-true-value="true" ng-false-value="false" name="checkbox-answer-input" display= inline-block >
<div class="description">
<span>{{opt}}</span><br/>
<span><i>{{descriptions[$index]}}</i></span>
</div>
</label>
</div>
</div>
CSS
.add-margin {
margin-top: 10px !important;
}
.container {
display: flex;
flex-direction: row-reverse;
}
.description {
margin-right: 8px;
}
Here is a screenshot of how I assume you want the data to show up. If you require a different layout, then let me know
Related
I'm curious if it's possible to place checkboxes in different areas based on their checked status, with only CSS! I'm a big CSS-only fan when there is a nice comprehensible solution to it.
I obviously put all my hope into grid and flex, but I couldn't come up with a solution which worked as I wanted to.
I hope this image makes my whole problem clear:
Active checkboxes in "Area A" and when not active in "Area B". Two Columns.
The HTML would be as simple as:
<div class="options">
<input type="checkbox" id="checkbox1">
<label for="checkbox1">checkbox</label>
<input type="checkbox" id="checkbox2">
<label for="checkbox2">checkbox</label>
...
</div>
It would also be okay when the input and label tag are wrapped.
And I can toggle a class on the wrapping tag... (using JS of course)
<div class="options">
<div class="checked">
<input type="checkbox" id="checkbox1">
<label for="checkbox1">checkbox</label>
</div>
<div class="">
<input type="checkbox" id="checkbox2">
<label for="checkbox2">checkbox</label>
</div>
...
</div>
CSS I tried with this approach:
.options {
display: grid;
grid-template-columns: 1fr 1fr;
}
.checked {
grid-row: 1;
}
But I can't manage to keep them in two columns when more than two boxes are checked.
I did some work to see what i could come up with, sadly i wasn't able to create exactly what you had asked for, but it might be close enough for what you need, or give some creative input :)
https://codepen.io/noex98/pen/OJgVqZr
HTML:
<div class="options">
<div class="checkWrap">
<input type="checkbox" id="checkbox1">
<label for="checkbox1">checkbox 1</label>
</div>
<div class="checkWrap">
<input type="checkbox" id="checkbox2">
<label for="checkbox2">checkbox 2</label>
</div>
<div class="checkWrap">
<input type="checkbox" id="checkbox3">
<label for="checkbox3">checkbox 3</label>
</div>
<div class="checkWrap">
<input type="checkbox" id="checkbox4">
<label for="checkbox4">checkbox 4</label>
</div>
<div class="checkWrap">
<input type="checkbox" id="checkbox5">
<label for="checkbox5">checkbox 5</label>
</div>
<div class="checkWrap">
<input type="checkbox" id="checkbox6">
<label for="checkbox6">checkbox 6</label>
</div>
</div>
CSS:
.options {
display: flex;
flex-wrap:wrap;
}
.checkWrap {
width:50%;
}
.checkWrap--checked {
order: -1;
}
JS:
let wrappers = document.querySelectorAll('.checkWrap')
document.querySelector('.options').addEventListener('click', () => {
for (wrapper of wrappers){
if (wrapper.getElementsByTagName('input')[0].checked == true){
wrapper.classList.add('checkWrap--checked')
} else {
wrapper.classList.remove('checkWrap--checked')
}
}
})
I have multiple checkboxes and everyone is displayed on a row. I want to have 3 or 4 checkboxes on a row, but I can't do it.
Angular:
<div class="form">
<mat-label>Choose skills:</mat-label>
<div class="container">
<div *ngFor="let skill of listOfSkills; index as i" id="skills">
<input type="checkbox" (change)="getSkill($event,i)">{{skill}}
</div>
</div>
</div>
CSS:
.container input[type=checkbox]{
display:inline-block;
width: 200px;
text-align: center;
}
Base on your question, maybe I can understand as you try to align all checkbox inside a container div ? Not sure is this what you looking for ?
div{
display:flex;
align-items:center;
}
input{
margin-right:10px;
}
input:last-child{
margin-right:0px;
}
<div>
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
<input type="checkbox" />
</div>
Your css sets the display property to inline-block on the input tag, but the surrounding div tag is still a block element and therefore will always wrap.
Your css should look like this:
css:
.container .checkbox {
display:inline-block;
width: 200px;
text-align: center;
}
html:
<div class="form">
<mat-label>Choose skills:</mat-label>
<div class="container">
<div *ngFor="let skill of listOfSkills; index as i" class="checkbox">
<input type="checkbox" (change)="getSkill($event,i)">{{skill}}
</div>
</div>
</div>
I have an un-editable HTML, which cannot change anything.
I need to hide the first checkbox and the second one will show. It is done in CSS, but somehow it doesn't work as expected.
Here is its LIVE sample.
Please help.
.treeview-container .treeview-item:first-child .form-check label input[type="checkbox"] {
visibility: hidden;
}
<div class="treeview-container">
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />First Box
</label>
</div>
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />Second Box
</label>
</div>
</div>
</div>
</div>
The problem is that .treeview-item:first-child is targetting both of the checkboxes' respective .form-check containers (as they are both the first child of their parent .treeview-item).
This is perhaps a little counter-intuitive, as you may expect the :first-child pseudo-selector to only target the very first occurence of a child of .treeview-item. This is not the case, as the :first-child selector actually targets the first child of each of the .treeview-item parents.
In order to correct this, you can simply use two child combinator selectors (>) to ensure that .treeview-item is a direct child of .treeview-container, and .form-check is a direct child of that .treeview-item.
This can be seen in the following:
.treeview-container > .treeview-item > .form-check label input[type="checkbox"] {
visibility: hidden;
}
<div class="treeview-container">
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />First Box
</label>
</div>
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />Second Box
</label>
</div>
</div>
</div>
</div>
Hope this helps! :)
.treeview-item:first-of-type {
display: none;
}
You can create an ID and add it to any elements you want hidden. However this only hides the element. If you do not want the user to be able to change the checkbox you may want to remove that input type all together.
.treeview-container .treeview-item:first-child .form-check label input[type="checkbox"] {
visibility: hidden;
}
#hideMe {
display: none;
}
<div class="treeview-container">
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" id = "hideMe"/>First Box
</label>
</div>
<div class="treeview-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" />Second Box
</label>
</div>
</div>
</div>
</div>
Using child combinator (>) in between two selectors will select a direct child of the parent. Currently, your code is selecting both inputs as you are just checking for decendents ..ie if the input has an ancestor as .treeview-container or not.
So using two consecutive child combinator will help you get expected result.
Code below.
.treeview-container > div > .form-check label input[type="checkbox"] {
visibility: hidden;
}
I need to right align a hyperlink in a <div>, however the only way I can get it to move right is to use the <p> tag. This puts the hyperlink on a new line rather than the same line.
How can I get it on the same line, and also give some spacing? Right now it looks like a run on sentence
<div>
<label class="control-label" for="ddlProfile">Profile:</label>
<input type="text" data-ng-model="viewModel.Name" class="form-control" />
<button >New</button>
<button >Edit</button>
<p style="text-align:right; display:block; margin:auto"><a href="https://www.w3schools.com" >Open New Form</a>
Display Detail</p>
</div>
You can use flexbox for this. Wrap the content in two parts - one for the left side, and another for the right. Then use margin to position the right side as you want.
.flex {
display: flex;
}
.right {
margin-left: auto;
}
/* your second question */
.right a {
margin-left: 1em;
}
<div class="flex">
<div class="left">
<label class="control-label" for="ddlProfile">Profile:</label>
<input type="text" data-ng-model="viewModel.Name" class="form-control" />
<button>New</button>
<button>Edit</button>
</div>
<div class="right">
Open New Form
Display Detail
</div>
</div>
Why not just float the elements to the right? but you need to change the order of the elements.
.float-right{
float:right;
}
.right-space{
padding-right:10px;
}
<div>
<label class="control-label" for="ddlProfile">Profile:</label>
<input type="text" data-ng-model="viewModel.Name" class="form-control" />
<button>New</button>
<button>Edit</button>
<a class="float-right" href="https://www.w3schools.com">Display Detail</a>
<a class="float-right right-space" href="https://www.w3schools.com">Open New Form</a>
</div>
Using Bootstrap version 2.3.2, I have a form layout like the below image and since the checkbox has an inline label, there is an aligning issue.
Adding margin to input[type="checkbox"] only gives margin to the checkbox, not the inline label. How do I make it so the checkbox and its label vertically align to the text fields next to it?
Here is the
JS BIN if you are interested.
In your HTML add a class that will handle the checkbox margin:
<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<label>label 1</label>
<input type="text" />
</div>
<div class="span3">
<label>label 2</label>
<input type="text" />
</div>
<div class="span3 checkbox">
<input type="checkbox" />test description
</div>
</div>
</div>
and in your CSS:
input[type="checkbox"] {
// i just remove this part..
}
.checkbox {
margin: 30px 0 0 0;
}
Don't put the margin on the checkbox, but on the parent div.
Check this jsFiddle.
Hope this helps
Try to always use something like this:
<div class="span3">
<label for="checkbox" class="checkbox">
<input type="checkbox" id="checkbox" class="checkbox">test description
</label>
</div>
http://jsbin.com/itAdAWA/1/edit
How about putting a <label> before the checkbox like this? ..
<div class="container-fluid">
<div class="row-fluid">
<div class="span3">
<label>label 1</label>
<input type="text">
</div>
<div class="span3">
<label>label 2</label>
<input type="text">
</div>
<div class="span3">
<label>test</label>
<input type="checkbox">
</div>
</div>
</div>
Bootply: http://bootply.com/86998
I just solved this exact problem in bootstrap 3, by simply limiting the height of inline checkboxes to 12 pixels. They are by default 40px, I don't know why !
<div class="checkbox-inline">
<label>
<input type="checkbox" checked="checked" />
<span>My correctly aligned check-box</span>
</label>
</div>
add this in your css file (I personally have a css file named bootstrap-custom.css):
/*
Checkboxes in inline forms are misaligned because for an unknow reason they inherit a height of 40px !
This selector limit the height of inline checkboxes to 12px which is the perfect value to align them to
the other widgets in an inline form.
*/
.radio-inline, .checkbox-inline {
max-height: 12px;
}
Not ideal solution but change your code to ...
<div class="span5">
<input type="checkbox">test description</input>
</div>
and set the margin-top on that. I will result as you want - better.
Bootstrap v5+
<!-- mt-md-4 pt-md-3 this apply margin and padding only for desktop -->
<div class="col-md-3 mb-3 md-mt-4 md-pt-3">
<input class="form-check-input" type="checkbox" value="" id="flexCheckDefault">
<label class="form-check-label" for="flexCheckDefault">
Default checkbox
</label>
</div>