I want to have a border show up around the image next to the radio button, when that radio button is clicked. Currently, my CSS selector knowledge is lacking and I do not get the expected result.
My expectation is that when I click a radio button , the corresponding image should be highlighted, but it is not...
What is wrong?
label>img ~ .input:checked {
border: 2px solid #f00;
}
<table>
<tr>
<td><label>
<img src="http://bighappyface.com/Happy%20Face%2050x50.png"><br />
<input type="radio" name="page" value="original" />Original </label>
</td>
<td><label>
<img src="http://bighappyface.com/Happy%20Face%2050x50.png"><br />
<input type="radio" name="page" value="standard" checked="checked">Standard
</label></td>
</tr>
</table>
EDIT
Answers so far rearrange HTML elements, which is not desirable from design point of view. I prefer to keep the text at the bottom of the image, not above. I'll re-accept if there is an answer that keeps html elements in order ...
here is a solution who sweets your needs :
Live Demo
input:checked ~ img {
border: 2px solid #f00;
}
label, img {
position: relative;
top: -80px;
}
label, input[type=radio] {
top: 60px;
}
HTML :
<table>
<tr>
<td>
<label>
<input type="radio" name="page" value="original" /> Original<br />
<img src="http://bighappyface.com/Happy%20Face%2050x50.png">
</label>
</td>
<td>
<label>
<input type="radio" name="page" value="standard" checked="checked"> Standard<br />
<img src="http://bighappyface.com/Happy%20Face%2050x50.png">
</label>
</td>
</tr>
</table>
You need to use input instead of .input because the dot addresses a class and you have not specified a class. Additionally, the :checked pseudo-class needs to be written before the element you want to change. The sibling selector ~ should work in theory but I had to re-arrange the html elements. Tested using Chrome, Opera and Firefox.
input:checked ~ img {
border: 2px solid #f00;
}
<table>
<tr>
<td>
<label>
<input type="radio" name="page" value="original" /> Original<br />
<img src="http://bighappyface.com/Happy%20Face%2050x50.png">
</label>
</td>
<td>
<label>
<input type="radio" name="page" value="standard" checked="checked"> Standard<br />
<img src="http://bighappyface.com/Happy%20Face%2050x50.png">
</label>
</td>
</tr>
</table>
fiddle link
http://jsfiddle.net/abasnet/0535aymy/
input[type="radio"]:checked + img {
border: 2px solid #f00;
}
<table>
<tr>
<td>
<input type="radio" name="page" value="original" />Original
<img src="http://idzyanamohddahlan.files.wordpress.com/2011/02/2472566_f520.jpg">
</td>
<td>
<input type="radio" name="page" value="standard" checked="checked">Standard
<img src="http://idzyanamohddahlan.files.wordpress.com/2011/02/2472566_f520.jpg">
</td>
</tr>
Related
I need help about this code please, how can i make the whole label's background color change when i check the checkbox ?
I want the whole td or change it's background when the checkbox checked
I think I can do it by using css only
https://codepen.io/Haitham1000/pen/ZEWPMeY
<table class="choices">
<tr>
<td>
<div class="checkbox">
<label><input type="checkbox" value="">All categories</label>
</div>
</td>
<td>
<div class="checkbox">
<label><input type="checkbox" value="">1</label>
</div>
</td>
<td>
<div class="checkbox disabled">
<label><input type="checkbox" value="" disabled>2</label>
</div>
</td>
<td>
<div class="checkbox">
<label><input type="checkbox" value="">3</label>
</div>
</td>
<td>
<div class="checkbox">
<label><input type="checkbox" value="">4</label>
</div>
</td>
</tr>
</table>
It's simple!
Put your labels after the Input:
<input type="checkbox" value=""><label>All categories</label>
Add this css to your code:
input:checked ~ label {
background-color: blue;
}
That would require a parent selector, which doesn't exist for CSS. You however do siblings that come after the checkbox. This means you could have a background element that stretches the entire width and height and is behind all the other elements change color. See my snippet
input:checked ~ .background {
background-color: blue;
}
.background {
position: fixed;
z-index: -1;
top: 0; left: 0;
width: 100vw;
height: 100vh;
background-color: grey;
}
<input type="checkbox">
<div class="background">
My issue is this piece of code:
<form method="post" name="myform" class="donate-form">
<table class="donate-table">
<input type="hidden" name="cpp_header_image" value="logo.png">
<input type="hidden" name="on0" value="Donation Amount">
<tr>
<th>
<div class="check">
<input name="os0" type="radio" value="5.00" id="radio">
<label for="$5" id="textBlock">$5.00</label>
</div>
</th>
<th>
<div class="checkboxgroup">
<input name="os0" type="radio" value="25.00" id="radio">
<label for="$25" id="textBlock">$25.00</label>
</div>
</th>
</tr>
<tr>
<th>
<div class="checkboxgroup">
<input name="os0" type="radio" value="10.00" id="radio">
<label for="$10" id="textBlock">$10.00</label>
</div>
</th>
<th>
<div class="checkboxgroup">
<input name="os0" type="radio" value="Other" id="radio">
<label for="other" id="textBlock">Custom</label>
</div>
</th>
</tr>
</table>
The issue is within the table "donate-table" closer to the bottom. I have 4 radio buttons, 5,10,25, and other, I want these radio-buttons to be displayed in 2 rows with 2 columns. On chrome (fullscreen) everything looks perfect no horizontal scroll bar and all radio buttons are looking okay but when I shrink the window to be as small as possible or when I use my mobile device the radio buttons get cut off and on my smartphone a horizontal scrollbar appears (I know the issue is because of a smaller screen size but I can't seem to fix it) When I delete 3 radio buttons and only have 1 the problem is gone. How can I fix this so I can display 4 radio buttons without a horizontal scrollbar appearing? I have looked and tried overflow-x:hidden; and some other suggested solutions but none work.
Anyone have a fix? Thank you in advance.
Here is my CSS code if needed:
.donations{
margin-top: 15%;
overflow: hidden;
}
.checkboxgroup {
text-align: center;
display: inline-block;
min-width: 150px;
}
.checkboxgroup label {
display: inline-block;
font-size: 20px;
}
Go through the code comments. Hope this will help you.
01. Replace all <th></th> with <td></td>
02. /* use .donate-table instead of .donations */
03. /* move min-height property to parent class (means .donate-table class) */
/* use .donate-table instead of .donations */
.donate-table{
margin-top: 15%;
overflow: hidden;
width:100%;
min-width:160px;
max-width:500px;
}
/* move min-height property to parent class (means .donate-table class) */
.checkboxgroup {
display: inline-block;
}
.checkboxgroup label {
display: inline-block;
}
<form method="post" name="myform" class="donate-form">
<table class="donate-table">
<input type="hidden" name="cpp_header_image" value="logo.png">
<input type="hidden" name="on0" value="Donation Amount">
<tr>
<td> <!-- Replace all <th></th> with <td></td> -->
<div class="check">
<input name="os0" type="radio" value="5.00" id="radio">
<label for="$5" id="textBlock">$5.00</label>
</div>
</td>
<td>
<div class="checkboxgroup">
<input name="os0" type="radio" value="25.00" id="radio">
<label for="$25" id="textBlock">$25.00</label>
</div>
</td>
</tr>
<tr>
<td>
<div class="checkboxgroup">
<input name="os0" type="radio" value="10.00" id="radio">
<label for="$10" id="textBlock">$10.00</label>
</div>
</td>
<td>
<div class="checkboxgroup">
<input name="os0" type="radio" value="Other" id="radio">
<label for="other" id="textBlock">Custom</label>
</div>
</td>
</tr>
</table>
I want to have four images for selection in radio button, however, when generating the radio button, the first image in td always have bigger width than others image, and the radio button is beside the image but not on top of the image, how can I make the first image have same width like others image in td.
Here are my codes:
<td><input type="radio" name="emotion" id="SeVYzlSRmE.jpg"/>
<label for="SeVYzlSRmE.jpg"><img src="SeVYzlSRmE.jpg" alt="I'm sad" /></label>
</td>
<td><input type="radio" name="emotion" id="zSepCGRnUc.jpg"/>
<label for="zSepCGRnUc.jpg"><img src="zSepCGRnUc.jpg" alt="I'm sad" /></label>
</td>
<td><input type="radio" name="emotion" id="WkzJIGctSz.jpg"/>
<label for="WkzJIGctSz.jpg"><img src="WkzJIGctSz.jpg" alt="I'm sad" /></label>
</td>
<td><input type="radio" name="emotion" id="vsIClceVUI.jpg"/>
<label for="vsIClceVUI.jpg"><img src="vsIClceVUI.jpg" alt="I'm sad" /></label>
</td>
</tr>
And here is the outlook image:
And here is the css I used:
.multiline
{
padding:0px;
white-space: pre-wrap;
height: 100px;
width: 50%;
margein:0px
}
.left-div {
float: left;
width: 50%;
height: 100px;
margin-right: 8px;
background-color: linen;
white-space: pre-wrap;
}
.right-div {
margin-left: 108px;
}
.row {
display: flex; /* equal height of the children */
}
.hide { position:absolute; top:-1px; left:-1px; width:1px; height:1px; }
.spacer {height:4px; }
.beta {border-collapse:collapse; table-layout:fixed; width:1000px;}
table td {word-wrap:break-word;}
.alpha {width:300px;height:300px;}
Try below code :
<style>
.box {
width: 50px;
float:right;}
</style>
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<table>
<tr>
<td><input type="radio" name="emotion" id="SeVYzlSRmE.jpg"/>
<label for="SeVYzlSRmE.jpg"><img class="box" src="SeVYzlSRmE.jpg" alt="I'm sad" /></label>
</td>
<td><input type="radio" name="emotion" id="zSepCGRnUc.jpg"/>
<label for="zSepCGRnUc.jpg"><img class="box" src="zSepCGRnUc.jpg" alt="I'm sad" /></label>
</td>
<td><input type="radio" name="emotion" id="WkzJIGctSz.jpg"/>
<label for="WkzJIGctSz.jpg"><img class="box" src="WkzJIGctSz.jpg" alt="I'm sad" /></label>
</td>
<td><input type="radio" name="emotion" id="vsIClceVUI.jpg"/>
<label for="vsIClceVUI.jpg"><img class="box" src="vsIClceVUI.jpg" alt="I'm sad" /></label>
</td>
</tr>
</table>
</body>
</html>
Try this:
.some_class td{
position:relative;
}
.some_class td input{
position:absolute;
top:34%;
left:0;
}
.some_class td label img{
display:inline-block;
padding-left:20px;
}
<table class="some_class">
<tr>
<td>
<input type="radio" name="emotion" id="SeVYzlSRmE.jpg"/>
<label for="SeVYzlSRmE.jpg">
<img alt="" src="https://i.stack.imgur.com/aSXYu.jpg?s=64&g=1" width="64" height="64" alt="Feel Happy" />
</label>
</td>
<td>
<input type="radio" name="emotion" id="zSepCGRnUc.jpg"/>
<label for="zSepCGRnUc.jpg"><img alt="" src="https://i.stack.imgur.com/aSXYu.jpg?s=64&g=1" width="64" height="64" alt="Feel Happy" /></label>
</td>
<td>
<input type="radio" name="emotion" id="WkzJIGctSz.jpg"/>
<label for="WkzJIGctSz.jpg"><img alt="" src="https://i.stack.imgur.com/aSXYu.jpg?s=64&g=1" width="64" height="64" alt="Feel Happy" /></label>
</td>
<td>
<input type="radio" name="emotion" id="vsIClceVUI.jpg"/>
<label for="vsIClceVUI.jpg"><img alt="" src="https://i.stack.imgur.com/aSXYu.jpg?s=64&g=1" width="64" height="64" alt="Feel Happy" /></label>
</td>
</tr>
</table>
Have you tried setting a float on each element?
In the CSS, define a class and add something like:
.box {
width: 50px;
float:left;}
Desired Behaviour
I have 35 tables containing 6 checkboxes each.
I am using this CSS in order to style their checked and unchecked states:
http://www.csscheckbox.com/checkbox/20695/paperclip-style-thin-css-checkbox/
When using more than 1 instance of a table, the CSS only seems to work (ie apply the correct checked and unchecked states) when each checkbox has a unique name, id and label.
I want to achieve the same effect without having to type a unique name, id and label for all of the 210 checkboxes in the 35 tables.
JSFiddle
http://jsfiddle.net/rwone/8edb0j0g
What I've Tried
In the example below I have shown 2 table instances where each checkbox has a unique name, id and label.
Giving them the same "identifiers" prevents functionality, as does removing them altogether.
table {
display: inline-block;
}
/* from:
http://www.csscheckbox.com/checkbox/20695/paperclip-style-thin-css-checkbox
*/
input[type=checkbox].css-checkbox {
position: absolute;
z-index: -1000;
left: -1000px;
overflow: hidden;
clip: rect(0 0 0 0);
height: 1px;
width: 1px;
margin: -1px;
padding: 0;
border: 0;
}
input[type=checkbox].css-checkbox + label.css-label {
padding-left: 22px;
height: 17px;
display: inline-block;
line-height: 17px;
background-repeat: no-repeat;
background-position: 0 0;
font-size: 17px;
vertical-align: middle;
cursor: pointer;
}
input[type=checkbox].css-checkbox:checked + label.css-label {
background-position: 0 -17px;
}
label.css-label {
background-image: url(http://csscheckbox.com/checkboxes/u/csscheckbox_a1e28825bac3d82a32f547300c847628.png);
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
<table>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG1" id="checkboxG1" class="css-checkbox" />
<label for="checkboxG1" class="css-label">area 01</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG2" id="checkboxG2" class="css-checkbox" />
<label for="checkboxG2" class="css-label">area 02</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG3" id="checkboxG3" class="css-checkbox" />
<label for="checkboxG3" class="css-label">area 03</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG4" id="checkboxG4" class="css-checkbox" />
<label for="checkboxG4" class="css-label">area 04</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG5" id="checkboxG5" class="css-checkbox" />
<label for="checkboxG5" class="css-label">area 05</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG6" id="checkboxG6" class="css-checkbox" />
<label for="checkboxG6" class="css-label">area 06</label>
</td>
</tr>
</table>
<!-- second table here -->
<table>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG7" id="checkboxG7" class="css-checkbox" />
<label for="checkboxG7" class="css-label">area 01</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG8" id="checkboxG8" class="css-checkbox" />
<label for="checkboxG8" class="css-label">area 02</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG9" id="checkboxG9" class="css-checkbox" />
<label for="checkboxG9" class="css-label">area 03</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG10" id="checkboxG10" class="css-checkbox" />
<label for="checkboxG10" class="css-label">area 04</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG11" id="checkboxG11" class="css-checkbox" />
<label for="checkboxG11" class="css-label">area 05</label>
</td>
</tr>
<tr>
<td class="area_checkbox">
<input type="checkbox" name="checkboxG12" id="checkboxG12" class="css-checkbox" />
<label for="checkboxG12" class="css-label">area 06</label>
</td>
</tr>
</table>
It might be useful for you if I briefly describe what trickery is being employed here. The unstyled checkbox is being hidden because its associated label is placed over the top of it. Checkboxes have functionality built in where if you click their associated label they become toggled on, so by clicking the label with the fake checkbox image, you're able to toggle the hidden checkbox on. There's CSS rules to change the fake checkbox image based on whether the hidden checkbox is checked or not.
So the answer to your question is ... sort of. You don't need the name field if you just want the toggle effect, but that's what's referenced when you do a form submission, so you probably will need it later.
The ID on your checkbox is necessary because the ID field is what the label's for attribute references, and you need a relationship between the label and the checkbox as I previously mentioned.
Certain fields are inherently unique; names, id's and label names. You are never supposed to have more than one element with the same name/id/label.
While you could assign more than one checkbox a duplicate of any of these fields, it would be bad practice to do so; it would be an attempt to use an identifier in a way that it is not intended to be used.
In the case that you have multiple elements requiring the same styling properties, classes are then used. Classes are meant to be used in the case that you want to reuse certain styling properties.
Imagine this scenario:
You have two identical checkboxes...
<input type="checkbox" name="checkboxG6" id="checkboxG6" class="css-checkbox" />
<label for="checkboxG6" class="css-label">area 06</label>
.......
<input type="checkbox" name="checkboxG6" id="checkboxG6" class="css-checkbox" />
<label for="checkboxG6" class="css-label">area 07</label>
In this case, the HTML and CSS are both looking for a unique name/id/label. But that truly doesn't exist. How is it supposed to know which one you are referencing? It can't make this decision and therefore neither of them is assigned the styling properties. That is why the fields must be unique.
Suggestion Going Forward
If you don't want to manually set each checkbox, I would check out AngularJS. It makes doing tasks like this extremely easy; you can define rules and use pre-existing directives in your HTML that make it extremely hassle-free.
In AngularJS, you could do something like the following: AngularJS Checklist-model
Have a contact form that I have some options to check for contact times and methods. I had it styled correctly on Chrome, Firefox and Safari. The labels where to the left of the radio option and they were all lined up.
I have since run into the issue where the chrome style has broken and I cannot figure out what is going on as the Firefox and Safari CSS work perfectly. The page is cbasphaltmaintenance.com
<table id="contactOptionTable">
<tr>
<td>
<h1>Prefered contact method:</h1>
</td>
<td>
<input type="radio" value="home" name="contact_Method" class="contactRadioClass"/>
<label for="home">Home</label>
</td>
<td>
<input type="radio" value="mobile" name="contact_Method" class="contactRadioClass" />
<label for="home">Mobile</label>
</td>
<td>
<input type="radio" value="email" name="contact_Method" class="contactRadioClass" />
<label for="home">Email</label>
</td>
</tr>
<tr>
<td>
<h1>Prefered contact time:</h1>
</td>
<td>
<input type="radio" value="morning" name="contact_Time" class="contactRadioClass"/>
<label for="home">Morning</label>
</td>
<td>
<input type="radio" value="afternoon" name="contact_Time" class="contactRadioClass" />
<label for="home">Afternoon</label>
</td>
<td>
<input type="radio" value="night" name="contact_Time" class="contactRadioClass" />
<label for="home">Evening</label>
</td>
</tr>
</table>
Here is the css that works in safari and firefox
#contactOptionTable{
vertical-align: middle;
float: left;
padding: 0;
margin-left: 14%;
}
.contactRadioClass{
margin-top:10px;
}
I don't know what is going on here but it is bugging me. I appreciate all of the help. As you may see I'm a little wet behind the ears.
Add the following class in your CSS.
#contactOptionTable input
{
float:right;
}
You need to reduce margin-left: 5% and add width : 100% to #contactOptionTable.
However I suggest you not to use table structure here, it would be better if you create it using div's