I have two radio buttons which show/hide divs oh my website. The first radio button controls the visibility of a div. In this div I have another radio button which shows/hides another div. The first radio button works, but when I click on the second one nothing happens.
This is an example of my HTML and CSS code :
HTML :
<body>
<label for="a">B</label><input type="radio" id="a" name="a" value="1">
<div id="B">
<p>Div B</p>
<label for="c">D</label><input type="radio" id="c" name="c" value="1">
</div>
<div id="D">
<p>Div D</p>
</div>
</body>
CSS :
#B
{
float:left;
background-color: green;
width: 150px;
height: 50px;
display: none;
}
#D
{
float:right;
background-color: red;
width: 150px;
height: 50px;
display: none;
}
#a:checked ~ #B
{
display: block;
}
#c:checked ~ #D
{
display: block;
}
I don't understand why the second button does nothing.
Is there a solution without using JS?
Thank you,
As #showdev said, #c is not a sibling of #D so sibling selectors won't works in that case.
If you wants this to work with only CSS you have to change your structure or do this with javascript.
EDIT : I made it works with only CSS and HTML arrangement :
See this fiddle
HTML :
<body>
<label for="a">B</label><input type="radio" id="a" name="a" value="1">
<div id="B">
<p>Div B</p>
</div>
<label for="c">D</label><input type="radio" id="c" name="c" value="1">
<div id="D">
<p>Div D</p>
</div>
</body>
CSS that change :
#a:checked ~ #B
{
display: block;
}
#c:checked ~ #D
{
display: block;
}
label:nth-of-type(2), input:nth-of-type(2) { display: none; clear: both;}
#a:checked ~ label:nth-of-type(2), #a:checked ~ input:nth-of-type(2) {
display: block;
}
You should use javascript, unless you need a restricted solution that doesn't involves js at all.. You can easily solve your problem with an onClick function, like this:
<input type="radio" id="a" name="a" value="1" onclick="somefunctionA()">
<label for="c">D</label><input type="radio" id="c" name="c" value="1" onclick="somefunctionB()">
<script>
function somefunctionA(){
//do something!
}
function somefunctionB(){
//do something!
}
</script>
Related
I hate css, I really do. I think this one will be trivial for most of you so please help me with this one.
I would like to create a radiobutton which have to change the background color of the label it's in. Here's the code which obviously does not work:
js:
<div className="container">
<label className="check" htmlFor="id">
<input {...radio.input} name="radio" type="radio" id="id" />
</label>
</div>
and css:
.check {
background-color: green;
display: inline-block;
height: 34px;
position: relative;
width: 60px;
cursor: pointer;
}
.check input {
display:none;
}
input[type="radio"]:checked + .check {
background-color: blue;
}
.container {
margin: 0 auto;
}
The + selector in CSS selects the next element in the HTML. Doing input + label is not going to work because your input is wrapped in the label.
The easiest solution for this would be to apply a checked CSS class in react when the input is checked. The other option would be to place the label AFTER the input in your markup, but that will probably require you to apply more CSS to get the appearance you need.
I really love CSS, I really do! ;)
Change your HTML to:
<div className="container">
<input {...radio.input} name="radio" type="radio" id="id" />
<label className="check" htmlFor="id">
</label>
</div>
and style the radio button individually.
I am trying to close a div when a checkbox is clicked with css only not JQuery or Javascript but it seems not working properly. How can I adjust it?
div[id^="div-"] {
display: block;
}
div[id^="div-"]:target {
display: none;
}
<input type="checkbox" checked>
<div id="div-1">
Here is the content.
</div>
How can I link the <a> click and the checkbox?
I think the only way to do this with pure css would be to have the checkbox as a direct sibling to the div:
#div-1 {display:none}
#checkbox:checked + #div-1 {display:block;}
<input id="checkbox" type="checkbox" checked>
<div id="div-1">
Here is the content.
</div>
#text{
width:100px;
height:100px;
background:black;
}
input[type=checkbox]:checked ~ #text{
display:none;
}
<input type="checkbox" name="check" value="checked">Click here<br>
<div id="text"></div>
Using only CSS you can do something like this.
JSFiddle
The + is the adjacent sibling selector, more info at https://developer.mozilla.org/en/docs/Web/CSS/Adjacent_sibling_selectors
#close + #div-1 {
display: none;
}
#close:checked + #div-1 {
display: initial;
}
<input id="close" type="checkbox" checked />
<div id="div-1">Here is the content.</div>
First you should remove the anchor and just let the input element because this trick that i'm showing needs elements in the same level or the second element be in lower levels of html structure.
<input type="checkbox" checked>
<div id="div-1">
Here is the content.
</div
css
div[id^="div-"] {
display: block;
}
input:checked ~ div[id^="div-"] {
display: none;
}
jsfiddle
I have a radio button which id is someID-1 and a div which id is navi1.
<input name="nappi" type="radio" id="someID-1" />
<div>
<div id="navi1">
<div style="z-index:100;position:fixed;right:0;top:0;bottom:0;">
<label for="someID-2">2</label>
</div>
</div>
</div>
This CSS code works just fine:
div[id^="navi"] {
display: none;
}
But this does not work OK:
input#someID-1:checked #navi1 {
display: block;
}
How should I modify the code?
I have tens of radio buttons (id names between someID-1 and someID-99). I would like to have dynamic code.
I do not want to use JavaScript.
You can make like this. you can read the details of the selector that i used here
#navi1{
display: none;
}
input[type="radio"]#someID-1:checked + div #navi1{
display: block;
}
.box{
border: 1px solid #ddd;
width: 200px;
height: 200px;
text-align: center;
}
<input name="nappi" type="radio" id="someID-1" />
<div class="box">
<div id="navi1">
<div>
<label for="someID-2">2</label>
</div>
</div>
</div>
You need to navigate the document hierarchy correctly in your CSS. So this works:
div[id^="navi"]
{
display: none;
}
#someID-1:checked + div div {
display:block;
}
I've seen some tricks to change the background color (or other css attributes) on a group of radio buttons. Here is some html
<div class="myclass col-xs-3">
<input type="radio" name="mygroup" value="one" data-bind="checked: SelectedAttributeValueId" />
</div>
<div class="myclass col-xs-3">
<input type="radio" name="mygroup" value="two" data-bind="checked: SelectedAttributeValueId" />
</div>
<div class="myclass col-xs-3">
<input type="radio" name="mygroup" value="three" data-bind="checked: SelectedAttributeValueId" />
</div>
I've tried things like:
.myclass input[type="radio"]:checked{
background-color:#f2f2f2;
}
and
.myclass :checked{
background-color:#f2f2f2;
}
here is a fiddle link. I am using knockout, so maybe this is the tool I should use to style the <div> elements?
All input is appreciated, I would prefer not to use jquery or javscript here (although knockout is okay)
It is not possible to style the radio buttons circle.
However, you can use pseudo-elements (in this case :before) to render a box around the radio button, then style it in CSS.
input[type="radio"] {
display: inline-block;
position: relative;
width: 25%;
margin: 0;
}
input[type="radio"]:before {
content: '';
position: absolute;
z-index: -1;
top: -.5em;
right: 0;
bottom: -.5em;
left: 0;
border: 1px solid black;
background-color: #0073ae;
}
input[type="radio"]:checked:before {
background-color: #f2f2f2;
}
<input type="radio" name="mygroup" value="one" /><input
type="radio" name="mygroup" value="two" /><input
type="radio" name="mygroup" value="three"/>
Here's a solution via jquery.
$('[type=radio]').click(function(){
if($(this).val() == "one") {
$('.myclass').css("background-color", "yellow");
}
//...two...three
});
I found this example (the "checked" version) and it works:
http://jsfiddle.net/2cTwA/
But I want to wrap the input and labels inside a container element (like nav), and if I do that the tabs stop working :(
Is there any solution for this?
found solution: http://jsfiddle.net/2cTwA/7/
With a slight HTML and CSS modification - DEMO
CSS
input { display: none; }
nav { overflow: hidden }
label { float: left; display: inline-block; padding: 5px 10px; }
label a { color: #d33; text-decoration: underline; cursor: pointer; }
.tab { display: none; border: 1px solid #333; padding: 10px; }
a[name="tab1"] + .tab { display: block }
:target + .tab { display: block }
:target ~ a[name="tab1"] + .tab { display: none }
HTML
<section class="tab-area tabs-checked">
<nav>
<input checked type="radio" name="tab" id="tab-A" />
<input type="radio" name="tab" id="tab-B" />
<input type="radio" name="tab" id="tab-C" />
<label class="tab-link" for="tab-A">Tab 1</label>
<label class="tab-link" for="tab-B">Tab 2</label>
<label class="tab-link" for="tab-C">Tab 3</label>
</nav>
<a name="tab3"></a>
<article class="tab">
<h3>Tab 3</h3>
</article>
<a name="tab2"></a>
<article class="tab">
<h3>Tab 2</h3>
</article>
<a name="tab1"></a>
<article class="tab">
<h3>Tab 1.</h3>
</article>
</section>
You're using the sibling selector (~), and by using a containing element such as nav, you are removing the inputs and labels from being siblings of the articles.
You simply need to rewrite your css where you use the tilde.
here is the sass example for 12(max) tabs using CSS only
.tabs {
input[type=radio] {
display: none;
#for $i from 1 through 12 {
&:nth-of-type(#{$i}):checked ~ .content .tab:nth-child(#{$i}) {
display: block;
}
}
}
label {
cursor: pointer;
display: inline-block;
}
.tab {
display: none;
}
}
and here is the html markup
<div class="tabs">
<input name="controls" type="radio" id="controls-tab" checked="true"/>
<label for="controls-tab">controls</label>
<input name="controls" type="radio" id="panels-tab"/>
<label for="panels-tab">panels</label>
<input name="controls" type="radio" id="readme-tab"/>
<label for="readme-tab">readme</label>
<div class="content">
<div class="tab">
</div>
<div class="tab">
</div>
<div class="tab">
</div>
</div>
</div>
no need to relatively position tab divs inside content div. no need to set content height.