Explanation
I want to switch between images selecting radios, but I'd like it to be HTML/CSS only. As you can see in the code below, it does work, but in my main code the radio button and the image are within their own divs, which then this code won't work because ~ only acts if .radio-image is directly preceded by input.radio3:checked; also in my main code, the images comes before the radio button.
I also wrote an example like it'd in my main code.
Codes
working code
input.radio1:checked~.image1 {
display: block;
}
input.radio1:checked~.image2 {
display: none;
}
input.radio1:checked~.image3 {
display: none;
}
input.radio2:checked~.image2 {
display: block;
}
input.radio2:checked~.image1 {
display: none;
}
input.radio2:checked~.image3 {
display: none;
}
input.radio3:checked~.image3 {
display: block;
}
input.radio3:checked~.image1 {
display: none;
}
input.radio3:checked~.image2 {
display: none;
}
<input type="radio" name="test" class="radio1" checked/>
<input type="radio" name="test" class="radio2" />
<input type="radio" name="test" class="radio3" />
<div class="image1">
<img src="http://placehold.it/50x50">
</div>
<div class="image2">
<img src="http://placehold.it/75x75">
</div>
<div class="image3">
<img src="http://placehold.it/100x100">
</div>
main code example
input.radio1:checked~.image1 {
display: block;
}
input.radio1:checked~.image2 {
display: none;
}
input.radio1:checked~.image3 {
display: none;
}
input.radio2:checked~.image2 {
display: block;
}
input.radio2:checked~.image1 {
display: none;
}
input.radio2:checked~.image3 {
display: none;
}
input.radio3:checked~.image3 {
display: block;
}
input.radio3:checked~.image1 {
display: none;
}
input.radio3:checked~.image2 {
display: none;
}
<section class="testSection">
<div class="firstDiv">
<div class="image1">
<img src="http://placehold.it/50x50">
</div>
<div class="image2">
<img src="http://placehold.it/75x75">
</div>
<div class="image3">
<img src="http://placehold.it/100x100">
</div>
</div>
<div class="secondDiv">
<form class="testForm">
<div class="thirdDiv">
<div class="inputDiv">
<input type="radio" name="test" class="radio1" checked />
</div>
<div class="inputDiv">
<input type="radio" name="test" class="radio2" />
</div>
<div class="inputDiv">
<input type="radio" name="test" class="radio3" />
</div>
</div>
</form>
</div>
</section>
Thanks in advance,
Luiz.
Related
I am using this code. I would also like when it is the display: block the color of the label changes.
label.divcheck { color:blue; text-decoration:underline; }
input.divcheck { display:none; }
input.divcheck + div { display:none; }
input.divcheck:checked + div { display:block;}
<label class="divcheck" for="navigation">Button Nav</label>
<label class="divcheck" for="other">Button Other</label>
<input type="checkbox" class="divcheck" id="navigation"/>
<div class="navigation">
Foo
</div>
<input type="checkbox" class="divcheck" id="other"/>
<div class="navigation">
Other
</div>
https://stackoverflow.com/a/35781115/15949286
The only way to do this without using JavaScript is to parse the label elements and position them after the input. The example for this is as follows.
label.divcheck {
color: blue;
text-decoration: underline;
}
input.divcheck {
display: none;
}
input.divcheck~div {
display: none;
}
input.divcheck:checked~div {
display: block;
}
input.divcheck:checked~label.divcheck {
color: red;
}
section {
display: flex;
}
<section>
<div>
<input type="checkbox" class="divcheck" id="other" />
<label class="divcheck" for="other">Button Other</label>
<div class="navigation">
Foo
</div>
</div>
<div>
<input type="checkbox" class="divcheck" id="navigation" />
<label class="divcheck" for="navigation">Button Nav</label>
<div class="navigation">
Other
</div>
</div>
</section>
I am trying to build a interactive email content block that would show or hide content based on the selection. It worked before I tried to customize the label. It stopped working after I wrapped the input and label inside a div. I think it could be throwing my selectors off but I am not sure how remedy the problem.
.custom-radios input[type="radio"] + label span img {
opacity: 0;
transition: all 0.3s ease;
}
.custom-radios input[type="radio"]#red + label span {
background-color: red;
}
.custom-radios input[type="radio"]#blue + label span {
background-color: blue;
}
.custom-radios input[type="radio"]#green + label span {
background-color: green;
}
.custom-radios input[type="radio"]#orange + label span {
background-color: orange;
}
.custom-radios input[type="radio"]:checked + label span img {
opacity: 1;
}
#red{
display: none
}
#blue{
display: none
}
#green{
display: none
}
#orange{
display: none
}
input[type="red"]:checked ~ #red {
display: block
}
input[value="blue"]:checked ~ #blue {
display: block;
}
input[value="green"]:checked ~ #green {
display: block;
}
input[value="orange"]:checked ~ #orange {
display: block;
}
<div class="custom-radios">
<div>
<input type="radio" id="red" name="color" value="red">
<label for="red">
<span>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
</span>
</label>
</div>
<div>
<input type="radio" id="blue" name="color" value="blue">
<label for="blue">
<span>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
</span>
</label>
</div>
<div>
<input type="radio" id="green" name="color" value="green">
<label for="green">
<span>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
</span>
</label>
</div>
<div>
<input type="radio" id="orange" name="color" value="orange">
<label for="orange">
<span>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg" alt="Checked Icon" />
</span>
</label>
</div>
<div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;"> </div>
<p style="margin:0;" id="red">
<img src="https://via.placeholder.com/580x200/FF0000/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="blue">
<img src="https://via.placeholder.com/580x200/0000FF/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="green">
<img src="https://via.placeholder.com/580x200/00FF00/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="orange">
<img src="https://via.placeholder.com/580x200/FFA500/FFFFFF" width="580" alt="" style="width:100%;height:auto;max-width:580px;" />
</p>
<div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;"> </div>
</div>
I think selectors may be a weak point for you, again, I encourage you to check the CSS Selectors Reference from w3school.
Anyways, I made a simplified version, as similar as I could to you current structure, so you can see it working.
.hidden_input {
display: none;
}
#red_input+label>span {
background-color: red;
}
#blue_input+label>span {
background-color: blue;
}
label>span>img {
opacity: 0;
transition: all 0.3s ease;
}
.hidden_input:checked+label>span>img {
opacity: 1;
}
#red_content,
#blue_content {
display: none;
}
#red_input:checked~#red_content,
#blue_input:checked~#blue_content {
display: block;
}
<div class="custom-radios">
<input type="radio" id="red_input" class="hidden_input" name="color" value="red">
<label for="red_input">
<span>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg"/>
</span>
</label>
<input type="radio" id="blue_input" class="hidden_input" name="color" value="blue">
<label for="blue_input">
<span>
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/242518/check-icn.svg"/>
</span>
</label>
<div class="spacer" style="line-height:26px;height:26px;mso-line-height-rule:exactly;"> </div>
<p style="margin:0;" id="red_content">
<img src="https://via.placeholder.com/580x200/FF0000/FFFFFF" width="580" style="width:100%;height:auto;max-width:580px;" />
</p>
<p style="margin:0;" id="blue_content">
<img src="https://via.placeholder.com/580x200/0000FF/FFFFFF" width="580" style="width:100%;height:auto;max-width:580px;" />
</p>
</div>
You can also play and place elements wherever you want in your HTML code, and then set their position with grid (it's just another option).
I am trying to place some radio buttons over an image at an exact location. I have placed both in a Div but I am not sure what to do next. Here is where I want the radio buttons to be placed (red circles):
Here is my code so far:
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
body {
background-color: #979797
}
<div class="general">
<img src="https://via.placeholder.com/300" class="center">
<form action="">
<input type="radio" name="answer 1" value="apple">
<input type="radio" name="answer 2" value="chicken">
<input type="radio" name="answer 3" value="carrot">
</form>
</div>
Thank you so much in advance!
I would put the radio buttons in one div each and then give the div a background. Like this:
<html>
<head>
<style>
.general{}
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
.radio-size {
height: 100px;
width: 100px;
}
.bg-apple {
background-image:url('https://images.unsplash.com/photo-1513677785800-9df79ae4b10b?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
background-size: cover;
}
.bg-chicken {
background-image:url('https://images.unsplash.com/photo-1426869981800-95ebf51ce900?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
background-size: cover;
}
.bg-carrot {
background-image:url('https://images.unsplash.com/photo-1445282768818-728615cc910a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80');
background-size: cover;
}
</style>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body bgcolor="#979797">
<div class="general">
<img src="https://images.unsplash.com/photo-1518796745738-41048802f99a?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=1350&q=80" class="center" style="width:20%;" >
<form action="" style="background-img:url("")">
<div class="radio-size bg-apple">
<input type="radio" name="answer 1" value="apple">
</div>
<div class="radio-size bg-chicken">
<input type="radio" name="answer 1" value="chicken"> <br>
</div>
<div class="radio-size bg-carrot">
<input type="radio" name="answer 1" value="carrot">
</div>
</form>
</div>
</body>
</html>
note: The radio-buttons are all supposed to have the same "name" atribute, so that you can only choose one of them. If you want to be able to select multiple options, you should use checkbox instead.
Even though you say you want them over your image, in the picture you shared the radio buttons appear to be on the right side of the image. So i am a bit confused about what you actually want.
But i made a simple example below. You can position them how you like or comment below if you want my help.
P.S. as i said in my comment : The <body> bgcolor attribute is not supported in HTML5. Use CSS instead.
.center {
display: block;
margin-left: auto;
margin-right: auto;
width: 50%;
}
body {
background-color: #979797
}
form {
position:absolute;
right: 25%;
top: 50%;
transform:translateY(-50%) translateX(100%);
display: flex;
flex-direction: column;
}
.general {
position: relative;
}
<div class="general">
<img src="http://via.placeholder.com/640x360" class="center">
<form action="">
<input type="radio" name="answer 1" value="apple">
<input type="radio" name="answer 2" value="chicken">
<input type="radio" name="answer 3" value="carrot">
</form>
</div>
You can use the following solution using flexbox:
body {
background:#979797;
}
.question {
display:flex;
flex-direction:row;
flex-wrap:nowrap;
}
.answers {
display:flex;
flex-direction:column;
}
label.answer {
position:relative;
height:100px;
}
label.answer input[type="radio"] {
top:0;
left:0;
position:absolute;
}
<form action="">
<h2>Question 1:</h2>
<div class="question">
<img src="https://picsum.photos/300">
<div class="answers">
<label class="answer">
<img src="https://picsum.photos/100">
<input type="radio" name="answer1" value="apple">
</label>
<label class="answer">
<img src="https://picsum.photos/100">
<input type="radio" name="answer1" value="chicken">
</label>
<label class="answer">
<img src="https://picsum.photos/100">
<input type="radio" name="answer1" value="carrot">
</label>
</div>
</div>
<h2>Question 2:</h2>
<div class="question">
<img src="https://picsum.photos/300">
<div class="answers">
<label class="answer">
<img src="https://picsum.photos/100">
<input type="radio" name="answer2" value="apple">
</label>
<label class="answer">
<img src="https://picsum.photos/100">
<input type="radio" name="answer2" value="chicken">
</label>
<label class="answer">
<img src="https://picsum.photos/100">
<input type="radio" name="answer2" value="carrot">
</label>
</div>
</div>
</form>
I think you should try the following type of code for the above query. I have changed you HTML structure and have changed your current CSS completely:
.general{
width: fit-content;
}
.img{
display: flex;
align-items: end;
}
img{
border: 1px solid black;
}
body {
background-color: #979797
}
.options{
display: flex;
flex-direction: column;
}
.radio{
position: relative;
height: 50px;
}
input[type="radio"]{
position: absolute;
top: calc(50% - 4px);
right: 0;
margin: 0;
}
<div class="general">
<form action="">
<div class="img">
<img src="https://via.placeholder.com/150" class="center">
<div class="options" class="center">
<div class="radio">
<img src="https://via.placeholder.com/50">
<input type="radio" name="answer1" value="apple">
</div>
<div class="radio">
<img src="https://via.placeholder.com/50">
<input type="radio" name="answer1" value="chicken">
</div>
<div class="radio">
<img src="https://via.placeholder.com/50">
<input type="radio" name="answer1" value="carrot">
</div>
</div>
</div>
</form>
</div>
Remember that if the questions only have a single answer then, the name of the radio box should be same and the value should be different. Otherwise, all the radio buttons will get selected simultaneously and cannot be unselected.
I hope this code is helpful.
Thanks.
I have an auto-generated file input form element:
<div id="university_logo">
<img src=/media/userprofile/Metallica_-_2008_-_Death_Magnetic_-_Front.jpg>
<div class="form-group">
<label class="control-label" for="id_univercity_logo">Logo</label>
<div class="row bootstrap3-multi-input">
<div class="col-xs-12">
Currently:
userprofile/Metallica_-_2008_-_Death_Magnetic_-_Front.jpg<br />
Change:
<input type="file" name="univercity_logo" autocomplete="off" class="" title="" id="id_univercity_logo" />
</div>
</div>
</div>
</div>
I am trying to remove the labels "Currently:" and "Change:"
My css code is:
#university_logo {
.form-group{
.row{
margin-left: 0;
margin-right: 0;
.col-xs-12{
*:not(#id_univercity_logo){
display: none;
}
}
}
}
}
However the two labels don't disappear.
Can I select a label without a label tag?
Could use visibility
.col-xs-12 {
visibility:collapse; /*hidden, in this case same result*/
font-size: 0px;
}
.col-xs-12 input{
visibility:visible;
}
.col-xs-12 a{
visibility:visible;
font-size: 15px;
}
<div id="university_logo">
<img src=/media/userprofile/Metallica_-_2008_-_Death_Magnetic_-_Front.jpg>
<div class="form-group">
<label class="control-label" for="id_univercity_logo">Logo</label>
<div class="row bootstrap3-multi-input">
<div class="col-xs-12">
Currently:
Image Tag<br />
Change:
<input type="file" name="univercity_logo" autocomplete="off" class="" title="" id="id_univercity_logo" />
</div>
</div>
</div>
</div>
try css code this way
#university_logo .form-group .row {
margin-left: 0;
margin-right: 0;
}
.col-xs-12 {
display: none;
}
<div id="university_logo">
<img src=/media/userprofile/Metallica_-_2008_-_Death_Magnetic_-_Front.jpg>
<div class="form-group">
<label class="control-label" for="id_univercity_logo">Logo</label>
<div class="row bootstrap3-multi-input">
<div class="col-xs-12">
Currently:
userprofile/Metallica_-_2008_-_Death_Magnetic_-_Front.jpg<br /> Change:
<input type="file" name="univercity_logo" autocomplete="off" class="" title="" id="id_univercity_logo" />
</div>
</div>
</div>
</div>
My HTML :
<div id="contentapp">
<input type="radio" name="menu" id="interest" checked>
<input type="radio" name="menu" id="about">
<div id="tab">
<label for="interest">Interest</label>
<label for="about">About</label>
</div>
<div id="content">
<article class="interest">
Interest
</article>
<article class="about">
About
</article>
</div>
</div>
My CSS :
#contentapp article {
display: none;
}
#contentapp input#interest:checked ~ article.interest{
display: block;
}
this is my problem . . . : how to do it in correct way for activate display block when radio button clicked ?
You have a div between contentapp and article. This means, you should use it in your css as well
#contentapp div article {
display: none;
}
#contentapp input#interest:checked ~ div article.interest{
display: block;
}
EDIT: Here a jsfiddle: https://jsfiddle.net/634ctoxw/
Try the below one:
#contentapp #content article {
display: none;
}
input#interest:checked ~ #content article.interest{
display: block;
}
You need to handle the ID properly.
Use the following code:
$(document).on('click', "input[type='radio']", function(){
$('#contentapp article').hide();
var id = $(this).attr('id');
$('.' + id).show();
});
#contentapp article {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="contentapp">
<input type="radio" name="menu" id="interest" checked>
<input type="radio" name="menu" id="about">
<div id="tab">
<label for="interest">Interest</label>
<label for="about">About</label>
</div>
<div id="content">
<article class="interest">
Interest
</article>
<article class="about">
About
</article>
</div>
</div>