CSS Hidden First Child - html

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;
}

Related

Put the checkbox on the right side of the label

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

I cannot select span:before with CSS

I want to access span elements inside .payment-method. For first span element I want to set image "image1.png" and for second element "image2.png".
Here is my HTML code:
.payment-group .payment-method:nth-child(0){
.payment-method-title label span:before{
content: url(https://icon-library.com/images/delivery-service-icon/delivery-service-icon-6.jpg);
}
}
.payment-group.payment-method:nth-child(1){
.payment-method-title label span:before{
content: url(https://icon-library.com/images/bank-transfer-icon/bank-transfer-icon-6.jpg);
}
}
<div class="payment-group">
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="cashondelivery" value="cashondelivery"/>
<label class="label">
<span>Cash on delivery</span>
</label>
</div>
</div>
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="banktransfer" value="banktransfer"/>
<label class="label">
<span>Bank transfer</span>
</label>
</div>
</div>
</div>
Can someone help me ?
(I am using LESS, but you can help me with plain CSS)
There are a few problems here:
nth-child starts at 1 not 0 in CSS.
The nesting of selectors does not exist in pure CSS, this snippet 'flattens' them
Space is a very important character in a CSS selector. It is a 'combinator'. The second selector missed it out before .payment-method
the before of a pseudo element nowadays should have a double colon as in ::before (this indicates a pseudo element as opposed to a pseudo class).
.payment-group .payment-method:nth-child(1) .payment-method-title label span::before {
content: url(https://icon-library.com/images/delivery-service-icon/delivery-service-icon-6.jpg);
}
.payment-group .payment-method:nth-child(2) .payment-method-title label span::before {
content: url(https://icon-library.com/images/bank-transfer-icon/bank-transfer-icon-6.jpg);
}
<div class="payment-group">
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="cashondelivery" value="cashondelivery" />
<label class="label">
<span>Cash on delivery</span>
</label>
</div>
</div>
<div class="payment-method">
<div class="payment-method-title field choice">
<input type="radio" class="radio" id="banktransfer" value="banktransfer" />
<label class="label">
<span>Bank transfer</span>
</label>
</div>
</div>
</div>

How to specify a complex absolute reference in CSS

I have a HTML page containing some radiobuttons and some related fieldsets, each one with its own id.
I want to hide/view the div depending on a specific radiobutton.
The attached Fiddle do the job.
Here is the HTML
<div id="FS1">External fieldset with radiobuttons
<div class="row">
<input checked="checked" id="R1" name="Group1" type="radio" value="1"/>
<label for="R1">Element 1</label>
<input id="R2" name="Group1" type="radio" value="2"/>
<label for="R2">Element 2</label>
<input id="R3" name="Group1" type="radio" value="3"/>
<label for="R3">Element 3</label>
<fieldset class="fs" id="FS1-1">
<legend>Header element 1</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText1" type="text"/>
<label for="exampleInputText1">Input field in the first element</label></div>
</fieldset>
<fieldset class="fs" id="FS1-2">
<legend>Header element 2</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText2" type="text"/>
<label for="exampleInputText2">This is the input field for the second</label>
</div>
</fieldset>
<fieldset class="fs" id="FS1-3">
<legend>Header element 3</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText3" type="text"/>
<label for="exampleInputText3">And this is the last input field</label>
</div>
</fieldset>
</div>
</div>
and this is the CSS
#FS1 div input:not(:checked) ~ #FS1-1, #FS1-2, #FS1-3 { display: none; } /* to hide all the detail elements */
#FS1 div input[value="1"]:checked ~ #FS1-1 { display: block; } /* to show only the 1th elem */
#FS1 div input[value="2"]:checked ~ #FS1-2 { display: block; } /* to show only the 2nd */
#FS1 div input[value="3"]:checked ~ #FS1-3 { display: block; } /* to show only the 3rd */
Now, what I want to achieve, is to relocate the fieldset outside the div, at the end of the fragment, but this brokes the css references and the fragment doesn't run anymore.
The following is the desired fragment, not running.
I need suggestions on how to redefine the CSS
<div id="FS1">External fieldset with radiobuttons
<div class="row">
<input checked="checked" id="R1" name="Group1" type="radio" value="1"/>
<label for="R1">Element 1</label>
<input id="R2" name="Group1" type="radio" value="2"/>
<label for="R2">Element 2</label>
<input id="R3" name="Group1" type="radio" value="3"/>
<label for="R3">Element 3</label>
</div>
<fieldset class="fs" id="FS1-1">
<legend>Header element 1</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText1" type="text"/>
<label for="exampleInputText1">Input field in the first element</label></div>
</fieldset>
<fieldset class="fs" id="FS1-2">
<legend>Header element 2</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText2" type="text"/>
<label for="exampleInputText2">This is the input field for the second</label>
</div>
</fieldset>
<fieldset class="fs" id="FS1-3">
<legend>Header element 3</legend>
<div class="form-group">
<input class="form-control" id="exampleInputText3" type="text"/>
<label for="exampleInputText3">And this is the last input field</label>
</div>
</fieldset>
</div>
So what you're asking cannot be achieved with plain CSS, but a solution could be achieved with Javascript.
Here is why it is not possible with your current HTML structure, as your current HTML structure looks like this:
With corresponding (simplified) code:
#FS1 div input:not(:checked) ~ #FS1-1, #FS1-2, #FS1-3 { display: none; } /* to hide all the detail elements */
#FS1 div input[value="1"]:checked ~ #FS1-1 { display: block; } /* to show only the 1th elem */
#FS1 div input[value="2"]:checked ~ #FS1-2 { display: block; } /* to show only the 2nd */
#FS1 div input[value="3"]:checked ~ #FS1-3 { display: block; } /* to show only the 3rd */
<div id="FS1">
<div class="row">
<input checked="checked" id="R1" name="Group1" type="radio" value="1"/>
<fieldset class="fs" id="FS1-1"></fieldset>
<fieldset class="fs" id="FS1-2"></fieldset>
<fieldset class="fs" id="FS1-3"></fieldset>
</div>
</div>
As a simple example, I will explain why this line of code works:
#FS1 div input[value="1"]:checked ~ #FS1-1 { display: block; }
Accesses id FS1
Accesses HTML entity div which is a child of FS1
Accesses the HTML entity input for state :checked and internal value="1"
Accesses the sibling element (~) of id FS1-1
Sets the property of display: block for FS1-1
The key thing to note is that the sibling element is selected.
With your proposed changesets to the HTML (see below), you would be required to access the parent element of the HTML entity input for state :checked and internal value="1" and then from there access the sibling, as FS1-1 no longer resides in the div class="row", rather outside it.
As per Wikipedia:
Some noted limitations of the current capabilities of CSS include:
Selectors are unable to ascend. CSS currently offers no way to select a parent or ancestor of an element that satisfies certain criteria.
This has also been touched on here: Is there a CSS parent selector?
There is currently no way to select the parent of an element in CSS. In the meantime, you'll have to resort to JavaScript if you need to select a parent element.
Honestly, it's probably not worth the fight, leave the fieldset inside the div class="row" if possible.
Here are the relevant resources if you wish to go ahead and convert this to a Javascript solution:
Javascript Parent Element
Set Style Javascript
Get Attribute Javascript
Next Sibling Javascript

styling the default html checkboxes

This HTML code shows two checkboxes next to each other
<div id="mr_mrs">
<ul class="mr_mrs form-no-clear">
<li id="mr" class="popular-category">
<label for="Mr" id="mr">Mr</label>
<span><input name="Mr" id="Mr" type="checkbox" /></span>
<div class="clearboth"></div>
</li>
<li id="mrs" class="popular-category">
<label for="Mrs" id="mrs">Mrs</label>
<span><input name="Mrs" id="Mrs" type="checkbox" /></span>
<div class="clearboth"></div>
</li>
</ul>
</div>
I want to customize the checkboxed to square and on selecting the checkbox fill some color to it. IMAGE BELOW
How can this be done ?
To sit the two controls next to each other, set their display to inline block: display: inline-block. (Default for div is block display.)
To enable the user to check the box by clicking the label text, there are a couple options. Both are shown in the example below.
<label> is parent to the text and checkbox.
Use <label for="myId">some text</label><input id="myId"> to associate the label to the input.*
*Note: when using the for/id method, it's label-for and input-id. I noticed in the example the id was applied to both elements, an id should be unique to the page.
.checkbox {
margin: 4px;
display: inline-block;
}
<div class="checkbox">
<label>Box 1<input type="checkbox"></label>
</div>
<div class="checkbox">
<label for="myId">Box 2</label>
<input id="myId" type="checkbox">
</div>

CSS for checked radio button with multiple labels

I'm having css issues with a second label on a radio button, when this radio button is checked. I already found out that a second (and a third) label is possible using 'for' in the label-tag. It's not possible to group everything in a single label-tag.
How can I change the background for the second label when a radio button is checked?
My (simplified) code is below, for the first label it works, second label it doesn't.
.radioclass:checked + label {
background-color: cyan;
}
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
The + selector in css means "immediate sibling", i.e. the very next element after the input must be a label. This will only select the immediate sibling.
An alternative is ~ which means "any sibling" and targets any label after the input.
In both of these cases, the elements (input and label) are on the same dom level. There is no way to traverse up the dom, grab the sibling and then the label - which is what you are trying to do with the html supplied.
If you are able to change the html, then either place the input outside the two divs or place both labels inside the same div (after the input).
.radioclass:checked ~ div label {
background-color: cyan;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<div class="col-sm-4">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
.radioclass:checked ~ label {
background-color: cyan;
}
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
<label for="id_radiobtn1">second label</label>
</div>
</div>
If you cannot alter the html, then JS is the only other approach (I've changed the input to a checkbox for demonstration purposes):
$('input').on('change', function(){
var func = 'removeClass';
if($(this).is(':checked')) {
func = 'addClass';
}
$('label[for="'+$(this).attr('id')+'"]')[func]('checked');
});
label.checked {
background-color: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="checkbox" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
Using just CSS this is possible, although it does require a rearrangement of your HTML, effectively moving the radio <input> ahead of the elements in which the <label> elements are contained; this removes the (impossible in CSS) requirement of traversing to the parent of the <input> in order to style the non-sibling <label> elements.
/* selects the element with the id of 'id_radiobtn1' when it is
checked, uses the general sibling combinator (~) to select
all sibling <div> elements with the class of 'col-sm-4'
and then finds the descendant <label> elements within
that/those <div> elements whose 'for' attribute-value is
equal to 'id_radiobtn1': */
#id_radiobtn1:checked ~ div.col-sm-4 label[for=id_radiobtn1] {
background-color: cyan;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1" />
<div class="col-sm-4">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
Now, if you must have a visible radio <input> besides the first of the <label> elements, you can use a pseudo-element to effectively pretend, while also hiding the real radio <input>:
*,
::before,
::after {
/* to include border and padding in the calculated
dimensions (width/height) of the elements: */
box-sizing: border-box;
}
.row>input[type=radio] {
/* hiding the radio <input> elements: */
display: none;
}
/* selecting the <input> elements whose type is
equal to 'radio', which is checked and then
finding all (subsequent) sibling elements
(using the '~' combinator) which <div>
elements with a class of 'col-sm-4', and
traversing to the <label> elements within: */
input[type=radio]:checked~div.col-sm-4 label {
background-color: cyan;
}
label {
/* in order to ensure that the descendant
pseudo-elements are positioned relative
to their parent element: */
position: relative;
/* making room for the pseudo-elements: */
margin-left: 2em;
}
input[type=radio]+.col-sm-4 label::before {
/* the content property is required, even
if only an empty string, to have the
pseudo-element show up: */
content: '';
display: inline-block;
/* positioning absolutely, in relation to
the closest ancestor with a non 'static'
position, in this case the parent
<label> element: */
position: absolute;
/* moving it outside of the <label> element's
left border, to avoid the background-color: */
left: -2em;
/* purely aesthetic: */
width: 0.8em;
height: 0.8em;
/* arbitrary positioning, adjust to taste: */
top: 50%;
transform: translateY(-40%);
/* making the pseudo-element circular in shape: */
border-radius: 50%;
/* colouring the faux 'border': */
box-shadow: 0 0 0 2px #ccc;
}
/* adjusting the colours of the faux radio: */
input[type=radio]:checked+.col-sm-4 label::before {
box-shadow: 0 0 0 2px #000, inset 0 0 0 3px #fff;
background-color: limegreen;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="group1" value="1" />
<div class="col-sm-4">
<label for="id_radiobtn1">row one first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">row one second label</label>
</div>
</div>
<div class="row">
<input class="radioclass" id="id_radiobtn2" type="radio" name="group1" value="2" />
<div class="col-sm-4">
<label for="id_radiobtn2">row two first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn2">row two second label</label>
</div>
</div>
You can do with Jquery on change event.
$(document).ready(function() {
$('#id_radiobtn1').on('change',function(){
$( "label:contains('second')" ).css( "background-color", "red" );
});
});
.radioclass:checked + label {
background-color: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
You can't do it with pure css, because backward in css is impossible.You can help of Jquery:
Note: I insert lab2 class to label.
$(document).ready(function(){
$('input[type=radio]').on('change',function(){
if($('.radioclass').is(':checked'))
$('.lab2').addClass('sel');
else
$('.lab2').removeClass('sel');
})
})
$(document).ready(function(){
$('input[type=radio]').on('change',function(){
if($('.radioclass').is(':checked'))
$('.lab2').addClass('sel');
else
$('.lab2').removeClass('sel');
})
})
#id_radiobtn1:checked + label {
background-color: green;
}
.sel {
background-color: red;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1" class="lab2">second label</label>
</div>
</div>