Can :checked and :nth-child() be used Together? - css

I have a list of checkboxes. The user can only select 10. I want to highlight the 11th (and greater) checked checkboxes with CSS to show they have to uncheck some. (I know there are other ways to do this including with JS and I understand the browser limitations; this is an exercise)
Checked checkbox labels are highlighted green, thusly:
li > input:checked + label {
color: green;
}
The 11th and greater checkbox label should be highlighted red:
li > input:checked:nth-child(n+11) + label {
background-color: red;
}
But this does not work. A simple test shows that I at least have the nth-child() syntax correct:
li:nth-child(n+11) {
background-color: red;
}
That works. Is there an issue using it with :checked or the + selector?
Edit: Fleshed out example with HTML
Here is what the HTML could look like:
<form action="#" method="get">
<ul>
<li><input type="checkbox" id="checks1" name="checks"> <label for="checks3">Test 1</label></li>
<li><input type="checkbox" id="checks2" name="checks"> <label for="checks3">Test 2</label></li>
<li><input type="checkbox" id="checks3" name="checks"> <label for="checks3">Test 3</label></li>
<!-- etc -->
</ul>
</form>
Here is a working JS fiddle with all scenarios above: http://jsfiddle.net/eQuEW/1/
In this example the first 12 boxes have been checked. Checked check boxes have green labels. Checkboxes 11 and up have bold text just as an example of what I'm talking about. Check box 11 and 12 should have a red background (but they do not). Then if, for example, the user were to de-select checkbox 1, the red background would no longer be on checkbox 11 as 2-11 represent the first 10 checked checkboxes and 12 represents the first one greater than 10 (and, therefore, in error).

This isn't possible with pure CSS no matter what the DOM structure. The reason is that :nth-child and :nth-of-type will always select the same elements regardless of other selectors. So :nth-child( n+11 ) will always select every select box after the 10th one. Adding another filter like :checked will just filter to all the checked inputs with an index > 10.
You can't filter the results of nth-child because it simply counts the number of previous siblings.
And just for the fun of it, to demonstrate what I mean: http://jsfiddle.net/u4xxA/1/

If i'm understanding you right, your HTML looks something like
<ul>
<li>
<input type="checkbox"...>
<label for="...">...</label>
</li>
<li>
<input type="checkbox"...>
<label for="...">...</label>
</li>
</ul>
If that's the case, you have a problem. See, CSS selectors work from the root down, and can't go in reverse. (Selecting a node based on its children doesn't work.) Add to that, :nth-child (and :nth-of-type, which is what i was originally going to suggest) will select nodes based on their index within the parent, which in this case is the <li> element. (That means there will never be an 11th checked box as long as each one is in its own list item.)
What you'd need is something like
(li:has(input:checked)):nth-of-type(n+11) label {
...
}
But :has and the parentheses doesn't exist in CSS, and from what i hear, it probably never will. You'll need to do this with JS.
(Edit: I had some mention of CSS that would work if you rearranged your HTML...but it turns out it didn't work. Pseudo-classes work independently of each other, so you'd always end up selecting the 11th box if it's checked, rather than the 11th checked box.)

Maybe a bit late to the party, but since all the other answers say it can't be done in CSS alone, here is the solution.
You can only do this if all the checkboxes are in the same container. So not one checkbox per list item. If you want the result to look like a list, you will have to add some more CSS.
input:checked + label {
color: green;
}
input:checked ~ input:checked ~ input:checked ~ input:checked
~ input:checked ~ input:checked ~ input:checked ~ input:checked
~ input:checked ~ input:checked ~ input:checked + label {
color: red;
}
/* Put each checkbox on a different line */
label:not(:last-child)::after {
content: '\000A';
white-space: pre;
}
<div class="list">
<input type="checkbox" id="checks1" name="checks"> <label for="checks1">Test 1</label>
<input type="checkbox" id="checks2" name="checks"> <label for="checks3">Test 2</label>
<input type="checkbox" id="checks3" name="checks"> <label for="checks3">Test 3</label>
<input type="checkbox" id="checks4" name="checks"> <label for="checks4">Test 4</label>
<input type="checkbox" id="checks5" name="checks"> <label for="checks5">Test 5</label>
<input type="checkbox" id="checks6" name="checks"> <label for="checks6">Test 6</label>
<input type="checkbox" id="checks7" name="checks"> <label for="checks7">Test 7</label>
<input type="checkbox" id="checks8" name="checks"> <label for="checks8">Test 8</label>
<input type="checkbox" id="checks9" name="checks"> <label for="checks9">Test 9</label>
<input type="checkbox" id="checks10" name="checks"> <label for="checks10">Test 10</label>
<input type="checkbox" id="checks11" name="checks"> <label for="checks11">Test 11</label>
<input type="checkbox" id="checks12" name="checks"> <label for="checks12">Test 12</label>
<input type="checkbox" id="checks13" name="checks"> <label for="checks13">Test 13</label>
<input type="checkbox" id="checks14" name="checks"> <label for="checks14">Test 14</label>
<input type="checkbox" id="checks15" name="checks"> <label for="checks15">Test 15</label>
<input type="checkbox" id="checks16" name="checks"> <label for="checks16">Test 16</label>
<input type="checkbox" id="checks17" name="checks"> <label for="checks17">Test 17</label>
<input type="checkbox" id="checks18" name="checks"> <label for="checks18">Test 18</label>
<input type="checkbox" id="checks19" name="checks"> <label for="checks19">Test 19</label>
<input type="checkbox" id="checks20" name="checks"> <label for="checks20">Test 20</label>
</div>

Related

Disabled input fields not changing text color with CSS in Jquery Mobile

I have some disabled input text fields that I want the text color to be black because it's too greyed out.
I know its a simple one-line code but it won't work for some reason.
Other CSS properties work while it's disabled like the background color and stuff but not the text color.
Here's the CSS:
input[type="text"]:disabled {
color: black;
}
Here's the HTML:
<div class="form-container">
<label for="name">Full Name:</label>
<input id="profileName" type="text" disabled="disabled">
<label for="email">Email ID:</label>
<input id="profileEmail" type="text" disabled="disabled">
<label for="address">Address Line:</label>
<input id="profileAddress" type="text" disabled="disabled">
<label for="city">City:</label>
<input id="profileCity" type="text" disabled="disabled">
<label for="postcode">Postcode:</label>
<input id="profilePostcode" type="text" disabled="disabled">
<label for="state">State:</label>
<input id="profileState" type="text" disabled="disabled">
</div>
This is what it looks like on the android emulator running API 28.
I'm not sure why other properties work but not the text color.
Any ideas?
P.S. this is a cordova project and i am building it in Jquery Mobile v1.4.5
Nice to reply to you about the issue you are facing.
Since, you are using Jquery Mobile. The Jquery mobile on DOM generates div covering the input box. Since, you are also using disabled property of input box. So, jquery mobile implements a class on that div and there the opacity level have been defined.
So, add this:-
.form-container .ui-input-text.ui-state-disabled { opacity:1; color:black; }
Working Fiddle :- https://jsfiddle.net/h3Lbadgv/1/
Hope it works for you.
Thanks
Can you use this one.
.form-container input:disabled{color: black}
<div class="form-container">
<label for="name">Full Name:</label>
<input value="XXXPPPAAPP" id="profileName" type="text" disabled="disabled">
<label for="email">Email ID:</label>
<input value="XXXPPPAAPP" id="profileEmail" type="text" disabled="disabled">
<label for="address">Address Line:</label>
<input value="XXXPPPAAPP" id="profileAddress" type="text" disabled="disabled">
<label for="city">City:</label>
<input value="XXXPPPAAPP" id="profileCity" type="text" disabled="disabled">
<label for="postcode">Postcode:</label>
<input id="profilePostcode" type="text" disabled="disabled">
<label for="state">State:</label>
<input id="profileState" type="text" disabled="disabled">
</div>
Hope It Helps.

Bootstrap - Layout spacing breaks for group of checkboxes when changing visiblity

I have the following scenario using Bootstrap.
The problem is that when I change the visibility of the first checkbox, it kinda breaks the spacing of the layout.
<label for="">Filter</label>
<div class="checkbox">
<label>
<input type="checkbox"> Filter 1
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Filter 2
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox"> Filter 3
</label>
</div>
You can test in this CodePen it by clicking on the button.
Did I build badly the HTML? Or am I missing some feature here
The margin-top for .checkbox+.checkbox is set for -5px, this rule applies, regardless of if the first checkbox is visible or not. Better solution would be to add the margin to the text above (which should be no label!) and don't set different margins for the different checkbox-classes.
<p>Filter</p>
<ul class="checkbox list-unstyled">
<li> <label> <input type="checkbox"> Filter 1</label> </li>
<li> <label> <input type="checkbox"> Filter 2</label> </li>
</ul>
You would need to change your JS to $(".checkbox li").first().toggleClass('hidden')

Change line spacing from <br> to other line spacing methods in css

I am writing an HTML5 page at the moment, I'm having an issue switching format from using br for line spacing to line-height, padding, or margin (whichever is easier). Everything is inside of a form and fieldset tag I do not want every line to be on their own, just some. Some text I do want next to each other because I am making a form that has radio buttons and check boxes. But instead of using br tags how I can switch that out to line-height, padding, or margin in css.
<form>
<fieldset class = "top">
Please Select a car: <br>
<input type="radio" name="car" value="truck">truck
<input type="radio" name="car" value="van">van
<input type="radio" name="car" value="suv">suv<br>
<input type="radio" name="car" value="coupe">coupe
<br>
<br>
<input type="checkbox" name="color" value="Blue">Blue
<input type="checkbox" name="color" value="red">red
<br>
<br>
<input type="checkbox" name="color" value="Orange">Orange
<input type="checkbox" name="color" value="black">Black
<br>
<br>
<input type="checkbox" name="color" value="Green">green
<input type="checkbox" name="color" value="brown">brown
<br>
<br>
....
Try to use a fieldset for each group of radios so you can control them more precisely, put margin only around your group and etc.
You can use labels and span around the label/input groups, so you can control them better.
<span>
<label for="name">Value</label>
<input type="checkbox" name="name" value="1"/>
</span>
Also if you use a class specific for your input, you can make it display in block, like this:
input.block {
display: block;
}
then if you put this class block on your input the label will stay in another line...
For a bigger picture see this fiddle:
http://jsfiddle.net/BxwNL/1/
Update: another solution according to cinnamon comment
You can also use a list for your items, but it would make sense if you grouped them according to their properties, like a list for the colours, a list for the types...
The usage would be like:
<ul>
<li>
<label for="name">Value</label>
<input type="checkbox" name="name" value="1"/>
</li>
<li>
<label for="name2">Value2</label>
<input type="checkbox" name="name2" value="2"/>
</li>
</ul>
Fiddle example for the list solution:
http://jsfiddle.net/BxwNL/2/

CSS: how to make label appear right of the radio?

Say I have the following mark up:
<label for="name">Name:</label>
<input type="radio" name="name" id="name"/>
The order of the tags appear to be proper to me (in a semantic sense: label before the thing you're labeling). But I want to display this as radio button first, followed by the label. How can I do that in CSS?
You don't need CSS. Wrap your input in a label and put the text last.
<label><input type="radio" name="name" id="name"/>Name:</label>
Is that still semantic for you?
Or you could try the float.
Tag order in this case doesn't matter. And even if it did, then it would be the other way around - first you would have to create the radio button, and then reference it in the label.
To answer your question: just do it in the order you want to display it
<input type="radio" name="name" id="name"/>
<label for="name">Name:</label>
fiddle http://jsfiddle.net/Jm2JR/1/..
write like this:
input{
float:left;
}
Check this http://jsfiddle.net/3cLRg/
<label for="name" style="float:right;">Name:</label>
try this:
<label style="position:relative; left:100px;" for="name">Name:</label>
<input type="radio" name="name" id="name"/>
2nd approach is :
<input type="radio" name="name" id="name"/>
<label for="name">Name:</label>
Hi you just define your input tag as like that
<label>Name:
<input type="radio" name="name" id="name"/></label>
<br />
<label>
<input type="radio" name="name" id="name"/> Name
</label>
​
Live demo http://jsfiddle.net/rohitazad/bhBQn/1/

Cannot Horizontally Center Horizontal toggle sets

Im using jQuery Mobile and I have a horizontal toggle set.
I would like to center it in the page, or I would like to stretch it so that it takes up 100% of the width.
Either would be good for what I am doing.
I have tried a lot of different approaches but none have worked so far.
Using jQuery Mobile v1.0
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="checkbox" name="monday" id="monday" class="custom"/>
<label for="monday">Monday</label>
<input type="checkbox" name="tuesday" id="tuesday" class="custom" />
<label for="tuesday">Tuesday</label>
<input type="checkbox" name="wednesday" id="wednesday" class="custom" />
<label for="wednesday">Wednesday</label>
<input type="checkbox" name="thursday" id="thursday" class="custom" />
<label for="thursday">Thursday</label>
<input type="checkbox" name="friday" id="friday" class="custom" />
<label for="friday">Friday</label>
<input type="checkbox" name="saturday" id="saturday" class="custom" />
<label for="saturday">Saturday</label>
<input type="checkbox" name="sunday" id="sunday" class="custom" />
<label for="sunday">Sunday</label>
</fieldset>
I just had this same problem and I solved it by adding by putting the fieldset within a div which has text-align: center and then setting the display of the fieldset to inline.
E.g.
HTML
<div class="custom">
<fieldset data-role="controlgroup" data-type="horizontal">
<input type="checkbox" name="monday" id="monday" />
<label for="monday">Monday</label>
...
</fieldset>
</div>
CSS
.custom {
text-align: center;
}
.custom fieldset {
display: inline;
}
Setting the display of the fieldset to inline may have some other effects, for example I noticed that the margins/padding changed slightly, but apart from that it did what I wanted.

Resources