May be i don't understand fully plus selector,
What i want, when user click on radio button home, div one should get displayed,
and when user click on radio button about, div two should get displayed, it did not work,
So i strip down the code, where is the problem, with this code i accepted div one to get displayed as home is by default checked. But it did not happened, so i know where is the problem but i dont know why,
Please read the comment, in the code, as i said which line is giving the problem hint it's css last section,
HTML CODE
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
CSS CODE
.navigation {margin-top:20px;}
.link{cursor:pointer;}
/*making div display*/
.one,.two{
display:none;
}
/*
###This line is not working## want to display div, if user click on radio
button
*/
#home:checked +.container > .one{
display:block;
}
if you want to run the code here is the code pen link https://codepen.io/arif_suhail_123/pen/KvdWey
.container is not a sibling of #home.
To select the element in question, when #home is checked, you can use the ~, which is the general sibling selector:
#home:checked ~ .display > .one
.navigation {margin-top:20px;}
.link {cursor:pointer;}
.one, .two {
display:none;
}
#home:checked ~ .display > .one {
display:block;
}
#about:checked ~ .display > .two {
display: block;
}
<div class="container">
<input type="radio" name="option" id="home" checked />
<input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The + is the adjacent sibling combinator. Which requires:
The elements to be siblings
The selector on the left of + is the first positioned element
The selector on the right of + is the selector that follows.
There must be no other elements between them.
In the following demo:
Each radio was moved in front of the div it's associated with.
Each radio is display:none since there's no need to show them because the interface are the labels.
Demo
input[name='option'],
.one,
.two {
display: none
}
#home:checked+.one {
display: block;
}
#about:checked+.two {
display: block;
}
<div class="container">
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
I believe for the plus operator to work the element has to be the immediate next sibling - So in this case the .one div would have to immediately follow the #home label, and the css would have to be:
#home:checked + .one{
display:block;
}
The html:
<div class="container">
<input type="radio" name="option" id="home" checked />
<div class="one">
<h3>This is first</h3>
</div>
<input type="radio" name="option" id="about" />
...
+ Selector : The element+element selector is used to select elements that is placed immediately after (not inside) the first specified element.
~ Selector : The element1~element2 selector matches occurrences of element2 that are preceded by element1.
So,you must use ~ instead of +.
.navigation {
margin-top:20px;
}
.link{
cursor:pointer;
}
.one,.two{
display:none;
}
#home:checked ~ .display > .one{
display:block;
}
#about:checked ~ .display > .two{
display:block;
}
<div class="container">
Home: <input type="radio" name="option" id="home" checked />
About: <input type="radio" name="option" id="about" />
<div class="navigation">
<label for="home" class="link">Home</label>
<label for="about" class="link">About Us</label>
</div>
<div class="display">
<div class="one">
<h3>This is first</h3>
</div>
<div class="two">
<h3>This is second</h3>
</div>
</div>
</div>
The order of elements is important when using this selector.
So to use the ~ operator the element should be after the first part.
Ex.
input[type=radio]:checked ~ label {
display: none;
}
The Html should be:
<div class="radio-groupe">
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
<label for="choice-2">More</label>
</div>
and not:
<div class="radio-groupe">
<label for="choice-2">More</label>
<input type="radio" name="water" id="choice-2" value="more-than-8-cups-a-day">
</div>
Related
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>
It is work when the radio buttons are same div level with "content1" and "content2",
How to make it work, if I put radio button to another div that outside the div "second"
suppose that the toggle1 is checked then content1 will show up
(using CSS and HTML ONLY, no javascript)
.content1 {
display: none;
}
.content2 {
display: none;
}
.toggle1:checked ~ .grid-container .content1 {
display: block;
}
.toggle2:checked ~ .grid-container .content2 {
display: block;
}
<div class="level1">
<div class="level2">
<input type=radio id="toggle1" name="toggle" class="toggle1">
<label for="toggle1">toggle1</label>
<input type=radio id="toggle2" name="toggle" class="toggle2">
<label for="toggle2">toggle2</label>
<div>
<div>
<div class="second">
<div class="tab content1">Content1</div>
<div class="tab content2">Content2</div>
</div>
When using the general sibling combinator ~, the two elements must be "children of the same parent element." Given your existing code, apply the "grid-container" class to the <div> that is a sibling of the <input> elements.
.content1,
.content2 {
display: none;
}
.toggle1:checked ~ .grid-container .content1,
.toggle2:checked ~ .grid-container .content2 {
display: block;
}
<div class="level1">
<div class="level2">
<input type=radio id="toggle1" name="toggle" class="toggle1">
<label for="toggle1">toggle1</label>
<input type=radio id="toggle2" name="toggle" class="toggle2">
<label for="toggle2">toggle2</label>
<div class="grid-container">
<div>
<div class="second">
<div class="tab content1">Content1</div>
<div class="tab content2">Content2</div>
</div>
</div>
</div>
</div>
</div>
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 have a css:
.form-group input[type=checkbox]:not(.form-group div.checkbox input[type=checkbox]) {
margin-top: 10px;
}
Which aims to have a margin-top for those checkbox without div.checkbox
but I want to use default css for those checkboxes inside div.checkbox
However, I got this warning when I check the css, what is the proper way to do this? thanks.
The below won't work because the CSS :not() selector takes only simple selectors as argument and as per the W3C Specs, a simple selector is either a type selector, universal selector, attribute selector, class selector, ID selector, or pseudo-class.
.form-group input[type=checkbox]:not(.form-group div.checkbox input[type=checkbox]) {
margin-top: 10px;
}
The argument that is used in the above selector is a sequence or chain of simple selectors.
The negation pseudo-class selector is in my opinion the most complex CSS selector to employ and it gets extremely messy if even one element which doesn't respect the rule is introduced somewhere in the middle. For example, a selector div:not(.checkbox) input[type=checkbox] will exclude only the second checkbox in the below structure.
div:not(.checkbox) input[type=checkbox] {
margin-top: 10px;
outline: 2px solid red;
}
<div class='something-else'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
<div class='something-else'>
<div class='checkbox'>
<input type='checkbox' />
</div>
</div>
You may have expected the last two checkboxes to also have been excluded because they have one div.checkbox ancestor above them but that doesn't happen because they also happen to have 1 non div.checkbox ancestor which is making the element get matched. This is why it becomes complex to use this selector.
If your HTML structure is something like the one in the below snippet then you could use combination of direct-child selectors along with the negation pseudo-class to style only checkboxes that aren't part of a div.checkbox ancestor.
.form-group > div:not(.checkbox) input[type=checkbox],
.form-group > input[type=checkbox] {
margin-top: 10px;
outline: 2px solid red;
}
<form class='form-group'>
<input type='checkbox' />
<div class='checkbox'>
<input type='checkbox' />
</div>
<div class='not-checkbox'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
<div class='not-checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
</form>
Or else, you could write the rules for all checkboxes generically and then override for those which are present within the div.checkbox.
.form-group input[type=checkbox] {
margin-top: 10px;
outline: 2px solid red;
}
.form-group div.checkbox input[type=checkbox] {
margin-top: 0px;
outline: none;
}
<form class='form-group'>
<input type='checkbox' />
<div class='checkbox'>
<input type='checkbox' />
</div>
<div class='not-checkbox'>
<input type='checkbox' />
</div>
<div class='checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
<div class='not-checkbox'>
<div class='something-else'>
<input type='checkbox' />
</div>
</div>
</form>