Select next element when input is checked - css

HTML:
<label>
<input type="checkbox" />
</label>
<div>
stuff
</div>
I'd like to be able to style the DIV element depending on the checked state of the input, like
input ~ div{
display: none;
}
input:checked ~ div{
display: block;
}
Obviously the~ selector doesn't seem to work here. Neither does +
Is there any other solution (besides javascript) ?

Try this, im not sure what its cross browser compatibility is.
input:checked + div
{
background: #333;
height: 30px;
width: 30px;
}
This should work, but I wouldnt do it, I would do Javascript.
See my jsfiddle

Sadly there is no way to select an ancestor in pure CSS, which is what you would require to select an ancestor's sibling.
I have seen people surround other content with a label - while this is a very questionable practice, it would allow you to use the + selector to style the div:
<label>
<input type="checkbox" />
<div>
stuff
</div>
</label>
Edit:
Or this (thanks to #lnrbob for pointing it out)
<label for="myCheckbox">
This is my label
</label>
<input id="myCheckbox" type="checkbox" />
<div>
stuff
</div>

if any one need extra solution
<input id="myCheckbox" type="checkbox" />
<label for="myCheckbox"> This is my label</label>
<div>
show when check box is checked
</div>
and the css
#myCheckbox ~ label ~ div { display: none; }
#myCheckbox:checked ~ label ~ div { display: block; }
happy coding !!!

Related

Styling option tags

I have a drop down that contains options. I would like to partially break & bold some text as well as insert context breaks. I tried using CSS as well as HTML tags but I'm unable to get it. Can someone please suggest a solution?
Thanks in advance
I know this question is a bit old (or not new at least), but I'd like to show a very simple way to emulate a select element rather than using a "replacement plugin" as suggested in How to style the option of a html “select”?.
There are probably many, MANY ways to do this, but I try to keep things extremely simple, so my method of emulation only uses CSS. It is rather bare bones, but I'd like to point out that it is not a complicated thing to do so you might not need a plug in to do it.
Note1: Instead of using <option>, I used <label>. Since <label> is an interactive element, putting something interactive inside (like a <button>) would probably mess it up. Options are normally non-interactive anyway, but just be aware that this simple emulation can't do everything.
Note2: If you want to be able to select multiple options, just do a search for "radio" and replace with "checkbox".
Emulating Select Using Radio - No Collapse
input[type="radio"] {
display: none;
}
input[type="radio"]:checked + label {
background-color: black;
color: #28AADC;
}
/* none functional styles. just regular styling */
.radio_select {
background-color: #28AADC;
display: inline-block;
}
<div class="radio_select">
<div>
<input id="rad1" type="radio" name="radio_select" />
<label for="rad1">Option 1</label>
</div>
<div>
<input id="rad2" type="radio" name="radio_select" checked="checked" />
<label for="rad2">Option 2</label>
</div>
<div>
<input id="rad3" type="radio" name="radio_select" />
<label for="rad3">Option 3</label>
</div>
</div>
Radio select emulation - with collapse
Note: this won't work for mobile devices since it uses :hover.
input[type="radio"] {
display: none;
}
/* style this to your heart's content */
input[type="radio"] + label {
display: none;
}
input[type="radio"]:checked + label {
background-color: black;
color: #28AADC;
display: inline-block;
}
.radio_select:hover label {
display: inline-block;
}
/* none functional styles. just regular styling */
.radio_select {
background-color: #28AADC;
display: inline-block;
}
<!-- NOTE: This technique uses hover, so it won't work for mobile devices.
I couldn't think of a pure CSS way to solve that. Sorry. -->
<div class="radio_select">
<div>
<input id="rad1" type="radio" name="radio_select" />
<label for="rad1">Option 1</label>
</div>
<div>
<input id="rad2" type="radio" name="radio_select" />
<label for="rad2">Option 2</label>
</div>
<div>
<input id="rad3" type="radio" name="radio_select" checked="checked" />
<label for="rad3">Option 3</label>
</div>
</div>

CSS pseudo-class :required does not work together with pseudo-element ::before

My intent is to put a * on labels of required fields.
I am testing with Chrome 47, Firefox 43 and Opera 34.
None of these can understand the CSS selector
span:required::before
According to http://caniuse.com/#feat=form-validation they all should be able to understand it, and if you use
span:hover::before
instead, it actually works.
What do I do wrong?
Here is my Code:
<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
span::before {
content: "\00A0";
}
span:required::before { /* This does NOT work! */
content: "*";
}
span:hover::before { /* But this DOES work! */
content: "_";
}
</style>
</head>
<body>
<form>
<p>
<span required>Name</span>
<input id="name" type="text" />
</p>
<p>
<span>Date of Birth</span>
<input id="birth" type="text" />
</p>
</form>
</body>
</html>
Two problems:
Form labels have a dedicated element, label. You should be using that, not span.
The required attribute only applies to the controls themselves, that is, input, select, textarea, etc. A span is just plain text (and a label is basically that on its own) and the required attribute makes no sense on such an element.1
If you're trying to style a label of a required input, you will need to give it a class name instead.
This has nothing to do with :required::before, though, given that most form elements are replaced elements, it's unlikely you'll find that pseudo-class and that pseudo-element together.
1 contenteditable notwithstanding.
The required attribute must be in the form control, and you should use labels instead of spans.
Then, you can use the selector :required + label::before. To make it work the form control must appear before the label in the DOM order, but then you can use floats or flexbox to rearrange.
p {
overflow: hidden;
}
label {
float: left;
}
label::before {
content: "\00A0";
font-family: monospace;
}
:required + label::before {
content: "*";
}
<form>
<p>
<input id="name" type="text" required />
<label for="name">Name</label>
</p>
<p>
<input id="birth" type="text" />
<label for="birth">Date of Birth</label>
</p>
</form>

Checkbox Label overlapping the checkbox - Bootstrap3

I am using bootstrap 3 & the issue is that label for the checkbox is overlapping the text. I have tried a few thing things but did not work, so if someone can help I will really appreciate it. This is what the code looks like, The class of the form is form-horizontal
<div class="checkbox">
<label class="checkbox-inline no_indent">I have read and agree with privacy and disclosure policy.
<input name="Terms" id="Terms" type="checkbox" ></label>
</div>
It's supposed to be like this with Bootstrap, <input> first and text after. http://jsfiddle.net/sqax02ah/
<div class="checkbox">
<label class="checkbox-inline no_indent">
<input name="Terms" id="Terms" type="checkbox">
I have read and agree with privacy and disclosure policy.
</label>
</div>
You can follow other answers if you do need the checkbox appears at the end.
In Bootstrap the styles expect thecheckbox to be first and then the text and hence a margin-left: -20px is set. For you snippet you need to add custom styles.
.radio input[type=radio], .radio-inline input[type=radio], .checkbox input[type=checkbox], .checkbox-inline input[type=checkbox] {
margin-right: -20px;
margin-left: 0;
}
Fiddle
Try use display: inline-block for .checkbox class, its should to help. Or change position via margin margin-left: 20px;

Change div color with css checked selector

I have some problem when i try to change the color of a div using input tags. If the div is in the same section of the inputs it works perfect. But if i try to put the div in the footer, for example, stop working.
HTML:
<section>
<input id="select1" name="test" type="radio" checked />
<label for="select1">Red</label>
<input id="select2" name="test" type="radio" />
<label for="select2">Green</label>
<input id="select3" name="test" type="radio" />
<label for="select3">Blue</label>
</section>
<footer>
<div class="colorDiv"></div>
</footer>
CSS:
.colorDiv{
width:50px;
height:50px;
background-color:red;
}
#select2:checked ~ .colorDiv{
background-color:green;
}
#select3:checked ~ .colorDiv{
background-color:blue;
}
Here is an example: http://jsfiddle.net/cqscc48g
There is any way to achieve that?
Thanks
Css is a cascading renderer. So it follows the DOM element's structure. Therefore, you can only relate elements that are descendants or, at least following siblings.
You have two options:
1 - Adjust your HTML:
You don't even need to put the div inside the input's section. But at least, you'd have to let the inputs out of the section, to make a "nephew" selector. (of course this denomination does not exists ;) )
JsFiddle - Changin HTML
<input id="select1" name="test" type="radio" checked />
<label for="select1">Red</label>
<input id="select2" name="test" type="radio" />
<label for="select2">Green</label>
<input id="select3" name="test" type="radio" />
<label for="select3">Blue</label>
<footer>
<div class="colorDiv"></div>
</footer>
And then you can select:
#select2:checked ~ footer .colorDiv{
background-color:green;
}
#select3:checked ~ footer .colorDiv{
background-color:blue;
}
2 - Use a Javascript approach:
If you love your HTML structure so much, then you must go Javascript. You can make it a lot sharper, but just an example:
JsFiddle - Using Javascript
function ChangeColor(color) {
var clrDiv = document.getElementsByClassName("colorDiv")[0];
clrDiv.style.backgroundColor = color;
}
document.getElementById("select1").onclick = function() { ChangeColor(""); }
document.getElementById("select2").onclick = function() { ChangeColor("green"); }
document.getElementById("select3").onclick = function() { ChangeColor("blue"); }
Change your markup and go through comments in code,
.colorDiv {
width: 50px;
height: 50px;
background-color: red;
}
#select2:checked~.colorDiv {
background-color: green;
}
#select3:checked~.colorDiv {
background-color: blue;
}
<section>
<input id="select1" name="test" type="radio" checked />
<label for="select1">Red</label>
<input id="select2" name="test" type="radio" />
<label for="select2">Green</label>
<input id="select3" name="test" type="radio" />
<label for="select3">Blue</label>
<div class="colorDiv"></div>
<!-- this should be adjacent as per your css selectors -->
</section>
Fiddle
If you want click inside somewhere div and hover any of body div than set input at the top outside..
<style>
input[type=checkbox] {
display:none;
}
input[type=checkbox]:checked ~ div.content{
display:none;
}
</style>
<input type="checkbox" id="toogle-content"/>
<div>
<label for="toogle-content" id="toogle-content">CLICK ME!</label>
</div>
<div class="content">
I can toggle now ;)
</div>
Use .change() for every input. On change check id from clicked input and then change color of that div

position style series of radio buttons vertically

I have the following html.
<td>
<div id="form_comparison" class="field radio_field">
<input type="radio" id="form_comparison_0" name="form[comparison]" value="1"/>
<label for="form_comparison_0">Increased</label>
<input type="radio" id="form_comparison_1" name="form[comparison]" value="2" />
<label for="form_comparison_1">About the same</label>
<input type="radio" id="form_comparison_2" name="form[comparison]" value="3" />
<label for="form_comparison_2">Decreased</label>
</div>
</td>
Using css, how can I position radio buttons vertically, so that labels are displayed just after their respective radio buttons in the same line?
I don't know why you've wrapped this inside a td element, if you are designing a form layout, than ignore tables and use div for designing your form. Coming to your question, you can wrap the labels around input tag and use display: block; for label
#form_comparison label {
display: block;
}
Wrap each input using label like this
<label for="form_comparison_0">
<input type="radio" id="form_comparison_0" name="form[comparison]" value="1"/>
Increased
</label>
Demo
If you don't have any permissions to change the markup, you can use CSS content property with white-space: pre; and that will give you the desired output
label:after {
content: "\A";
white-space: pre;
margin-bottom: 10px;
}
Demo (No Changes In The Markup)
Note: Use #form_comparison label instead of only label as it will
select and apply all label element in your website where
#form_comparison label will only select label elements inside
#form_comparison
Working FIDDLE Demo
Float your input and label, and for second row, clear the float to make a new row:
#form_comparison input {
float: left;
}
#form_comparison label {
float: left;
}
#form_comparison label + input {
clear: both;
}

Resources