Border around label if radio is checked [duplicate] - html

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Highlight label if checkbox is checked (html/css)
I would like label to get a red border if radio is checked.
Code so far:
HTML:
<center style="margin-top:20px;">
<label class="big">
This is a box 1
<input name="radio-group1" type="radio" />
</label>
<br/>
<label class="big">
This is a box 2
<input name="radio-group1" type="radio" class='sex' />
</label>
</center>
CSS:
.big {
display:block;
width:100px;
height:100px;
background-color:gainsboro;
cursor:pointer;
}
.big:hover {
border:1px solid blue;
}
No JS solutions please. I have been trying with sibling and children selectors but unsuccesfuly.
JSFIDDLE: http://jsfiddle.net/QqVCu/10/

You would have to rearrange the HTML so the label/red-border-element comes after the radio.
HTML
<center style="margin-top:20px;">
<div class="big">
<input id="box1" name="radio-group1" type="radio" />
<label for="box1">This is a box 1</label>
</div >
<br/>
<div class="big">
<input id="box2" name="radio-group1" type="radio" />
<label for="box2">This is a box 2</label>
</div >
</center>
CSS
.big {
display:block;
width:100px;
height:100px;
background-color:gainsboro;
cursor:pointer;
position: relative;
}
.big:hover {
border:1px solid blue;
}
label {
width: 100%;
height: 100%;
display: block;
}
input[type="radio"] {
position: absolute;
top: 20px;
left: 50%;
}
input[type="radio"]:checked + label {
border: red 1px solid;
}
​
http://jsfiddle.net/QqVCu/12/
But it starts getting weird. A little javascript wouldn't hurt.
edit: this version is a little cleaner

You can use :checked selector, but this will only work for the checkbox itself. Otherwise there is no way to do it in pure CSS - you will have to resort to JavaScript (which I do realize you said you wanted to avoid - but pure CSS won't do it).

What you are trying is not possible with current structure of your html. There is no such thing as a parent selector. There is a sibling selector though, wich could be used to accomplish what you are after. First you would have to restructure your html to something like this:
<div>
<input name="radio-group1" id="box1" type="radio" />
<label class="big" for="box1">
This is a box 1</label>
</div>
<div >
<input name="radio-group1" id="box2" type="radio" class='sex' />
<label class="big" for="box2" >
This is a box 2</label>
</div>
I made label and input siblings in stead of parent/child. They will still work the same thanks to their id and for attributes. I also changed their order to be able to use the next sibling selector. The extra div is required to do some absolute positioning to put them back in the same order you had in your fiddle.
Next i added a few lines of css. The real magic happens here:
div input:checked+label {
border: 1px solid red;
}
This will selected all 'next sibling' of an input that is checked and has a div as a parent. You could further finetune this to only work on radio's and in reality i would add a class to the wrapper div, but this is just a 'proof of concept'.
The rest of the css i added is just some positioning to mimic the layout you had in your example. This will also need some finetuning.
The working example is here: http://jsfiddle.net/QqVCu/14/

Related

Styling Only Radio Inputs Within a Two-Level Deep Div

I have a containing div that has three divs inside. I want to style only the two divs that contain the radio input. Without using class names, is it possible to select those two divs?
If not, how do I select just the radio inputs and style those? Here's my attempt, with non-working CSS:
.container > div > input[type="radio"] {
border:1px solid green;
}
<div class="container">
<div>
<input type="radio" id="22" name="SetFour">
<label for="22"><span>Selection One</span></label>
</div>
<div>Some Random Div</div>
<div>
<input type="radio" id="23" name="SetFour">
<label for="23"><span>Selection Two</span></label>
</div>
</div>
CodePen for reference
You can use nth-of-type. But do this only if you have no alternatives and are sure that this block will not change in the future.
.container > div:nth-of-type(1),
.container > div:nth-of-type(3) {
border:1px solid green;
}
The selector selects the radio buttons, but the radio inputs don’t support the border property.
In case you want to select the divs, not the inputs, use classes; although there is a :has() pseudo‐class in the specifications, no major browser currently supports it.
https://caniuse.com/css-has
https://www.w3.org/TR/selectors-4/#relational
you have to set them a class.
write the similar class and styling.
or their id.

How to hide / display different page sections with radio input buttons

I'm an amature web developer, just getting my feet wet with my first proper html project.
I'm trying to build a webpage where you have a 'tab' selection in the top row and the currently selected tab shows a different section on the main page (hiding the other sections when not selected). I've done this using a radio input inside a label inside a table made of divs, and defined all of what I think is the correct CSS. (see below)
But the tabs do not work: depending on what I change, either every section shows up at once, or none of them do.
Here's my HTML:
<div class="sheet-table">
<div class="sheet-table-row sheet-candara">
<div class="sheet-col">
<label class="container" title="Adventure tab">
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab1" value="1">
<span class="sheet-tabs sheet-tab1 sheet-center">SECTION 1</span>
</label>
</div>
<div class="sheet-col">
<label class="container" title="Lifestyle tab">
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab2" value="2">
<span class="sheet-tabs sheet-tab2 sheet-center">SECTION 2</span>
</label>
</div>
<div class="sheet-col">
<label class="container" title="Options tab">
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab3" value="3">
<span class="sheet-tabs sheet-tab3 sheet-center">SECTION 3</span>
</label>
</div>
</div>
</div>
Then later on I have the section content (which I've commented out here):
<div class="sheet-section-tab1">
<!-- section 1 'adventure' content -->
</div>
<div class="sheet-section-tab2">
<!-- section 2 'lifestyle' content -->
</div>
<div class="sheet-section-tab3">
<!-- section 3 'options' content -->
</div>
And the CSS (a portion of which was copied and edited from W3Schools.com):
.container {
display: inline-block;
position: relative;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.container input {
position: absolute;
opacity: 0;
cursor: pointer;
}
.sheet-tabs {
position: inherit;
padding: 0.2em 0.9em;
z-index: 9999;
}
.container:hover input ~ .sheet-tabs {
background-color: rgba(30, 20, 20, 0.3);
border-radius: 0.5em;
content: attr(title);
}
.container input:checked ~ .sheet-tabs {
background-color: rgba(30, 20, 20, 0.7);
color: white;
border-radius: 0.5em;
}
div[class^="sheet-section"] {
display: none;
}
.sheet-section-tab1,
.sheet-section-tab2,
.sheet-section-tab3 {
display: none;
}
input.sheet-tab1:checked ~ div.sheet-section-tab1,
input.sheet-tab2:checked ~ div.sheet-section-tab2,
input.sheet-tab3:checked ~ div.sheet-section-tab3{
display: block;
}
Now, the strange thing that I discovered while trying to get this to work was that it does work when I take the inputs and spans out of their respective labels and div tables, like so:
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab1" value="1">
<span class="sheet-tabs sheet-tab1 sheet-center">SECTION 1</span>
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab2" value="2">
<span class="sheet-tabs sheet-tab2 sheet-center">SECTION 2</span>
<input type="radio" name="attr_tab" class="sheet-tabs sheet-tab3" value="3">
<span class="sheet-tabs sheet-tab3 sheet-center">SECTION 3</span>
<div class="sheet-section-tab1">
<!-- section 1 'adventure' content -->
</div>
<div class="sheet-section-tab2">
<!-- section 2 'lifestyle' content -->
</div>
<div class="sheet-section-tab3">
<!-- section 3 'options' content -->
</div>
But then I lose all the CSS styling that I worked so hard on...
Is there any way to get the functionality that I desire without compromising on the style, or am I doing something fundamentally wrong here? Or is there just a simple mistake I'm not seeing? Thanks!
(Also, I don't know if this will be relevant to any answers, but for reasons specific to my hosting platform I can't use the id attribute).
Using just HTML and CSS to create a tab system like you propose is possible, but is going to be messy and overall inefficient, and styling of the page will be difficult due to how the radio buttons will need to be aligned in order to achieve this. I highly recommend using java script to accomplish what you are doing. Here is a link to a HowTo on JavaScript tabs. However, if it is a must for this project to be accomplished through the strict use of HTML and CSS, let me know and I'll take a deeper look. From now what I can tell you is a possible way to accomplish this is to use the "+" selector. Structure the page in a way so that it alternate between radio button and the div/tab to be displayed, as so:
radio button
div
radio button
div
radio button
div
This way, the radio button is assigned to the div below it. Now you can target a checked radio button through the use of ":checked" and then using "+" which will select the closest div to the radio button. This can be accomplished in css using the following code:
.tabs {
display: none;
}
[name="radiobutton"]:checked + .tab {
display: block;
}
Cheers!
Have you tried adding in your Css overflow:Hidden;

Radio button checked change style and appear text

I have to create a navigation menu using HTML and CSS without Javascript for my eBay store. What I want to do is to create many radio buttons with many labels and clicking on a label make label bold and make appear a text.
First effect on JSFiddle: https://jsfiddle.net/fb2yn5ts/
(click on label make a text appears)
My Label
<div id="descrizione">
This is some text
</div>
css
#descrizione{
display: none;
}
#descrizione:target{
display: block;
}
Second effect on JSFiddle: https://jsfiddle.net/cv83q9ow/
(Click on label make label bold)
<div>
<input type="radio" id="check" class="check-with-label" />
<label for="check" class="label-for-check">My Label</label>
<div>
css:
.check-with-label:checked + .label-for-check {
font-weight: bold;
}
I can't merge these effects into one, do you know why and how can I solve this?
Thank you very very much!
OK guys I solved this problem by doing this:
HTML:
<input type="radio" name="navbar_menu_store" id="input_description" class="radio_item_menu">
<label for="input_description" class="label_item_menu">Description</label>
<input type="radio" name="navbar_menu_store" id="input_shipping" class="radio_item_menu">
<label for="input_shipping" class="label_item_menu">Shipping</label>
<div id="contentDescription"><p class="testo_scheda">
This is some text</p>
</div>
<div id="contentShipping"><p class="testo_scheda">
This is some other text</p>
</div>
css:
#input_description:checked ~ #contentDescription, #input_shipping:checked ~ #contentShipping {
display: block;
}
#contentDescription, #contentShipping{
display: none;
}
.radio_item_menu:checked + .label_item_menu {
font-weight: bold;
}
Preview: https://jsfiddle.net/LLornfn8/
Thank you very much anyway!

Is the use of label and radio button as CSS hook proper HTML?

For CSS, the combination of <label /> and <input type="radio" /> is a blessing - it allows commanding elements back through the DOM with no need for javascript, with all sorts of useful applications. Example:
<style>
#wrapper {width:300px; height:200px; text-align:center;}
.slide {position:relative; width:300px; height:200px; line-height:200px; border:1px solid black; font-size:3em; color:crimson;}
input {display:none;}
input[name="ito"] + .slide {display:none;}
input[name="ito"]:checked + .slide {display:block;}
.slide label {position:absolute; top:0; color:black;}
.slide label:first-of-type {left:0;}
.slide label:last-of-type {right:0;}
#is:checked ~ [for="is"], #this:checked ~ [for="this"], #ok:checked ~ [for="ok"] {color:crimson;}
</style>
<div id="wrapper">
<input name="ito" type="radio" id="is" checked>
<div class="slide">IS
<label for="ok">«</label><label for="this">»</label>
</div>
<input name="ito" type="radio" id="this">
<div class="slide">THIS
<label for="is">«</label><label for="ok">»</label>
</div>
<input name="ito" type="radio" id="ok">
<div class="slide">OK?
<label for="this">«</label><label for="is">»</label>
</div>
<label for="is">1</label><label for="this">2</label><label for="ok">3</label>
</div>
https://jsfiddle.net/v6eeqxog/
The code is technically valid as per https://html5.validator.nu, but how properly is it used?
I mean, there is some obvious level of clutter in the code, but going beyond that - is this ok for accessibility or other such concerns?
This doesn’t seem to be keyboard-accessible. You can’t focus the radio button (with the Tab key) to switch the "slides" (with the Arrow keys). It works without this CSS because then you can focus the radio buttons.
See WCAG 2.0 guideline 2.1: Keyboard Accessible: Make all functionality available from a keyboard.

Align form input and label on same row

How can I align below html in the format below?
<div>
<div>
<label>Counterparty</label>
<input id="paymentsApp-inpt-cpty" ng-model="selectedPaymentCopy.counterparty" ng-required="true" />
</div>
<div>
<label>Value Date</label>
<input id="paymentsApp-inpt-date" ng-model="selectedPaymentCopy.valueDate" />
</div>
<div>
<label>Credit Account</label>
<input id="paymentsApp-inpt-acc" ng-model="selectedPaymentCopy.creditAccount" />
</div>
<div>
<label>Amount</label>
<input id="paymentsApp-inpt-amt" ng-model="selectedPaymentCopy.amount" />
</div>
</div>
I am not using bootstrap in my project.
So can anybody tell me what will be the css for this?
Method #1: Flexbox:
div > div {
display: flex;
}
div > div > label {
flex-basis: 125px;
}
div > div > input {
flex: 1;
}
DEMO
Method #2: display: inline-block:
div > div > label {
display: inline-block;
width: 125px;
}
div > div > input {
display: inline-block; /* optional */
}
DEMO
Note that flexbox is supported by all major browsers, except IE 8 & 9. Some recent browser versions, such as Safari 8 and IE10, require vendor prefixes. For a quick way to add all the prefixes you need, post your CSS in the left panel here: Autoprefixer.
While flex is a good option, and I use it to good effect all the time, it can create some subtle issues that will be tough to handle if you're not thoroughly familiar with its behavior.
If you want something simpler, you could just try floating the inputs to the right. To do that you would have to place the input before the label and just leave the label alone.
Styles (a little verbose for illustration purposes, and I gave the container a fixed width for the same reason):
.test {
width: 400px;
}
.test > div > input {
float: right;
}
Markup (note switched position of input and label):
<div class="test">
<div>
<input id="paymentsApp-inpt-cpty" ng-model="selectedPaymentCopy.counterparty" ng-required="true" />
<label>Counterparty</label>
</div>
<div>
<input id="paymentsApp-inpt-date" ng-model="selectedPaymentCopy.valueDate" />
<label>Value Date</label>
</div>
<div>
<input id="paymentsApp-inpt-acc" ng-model="selectedPaymentCopy.creditAccount" />
<label>Credit Account</label>
</div>
<div>
<input id="paymentsApp-inpt-amt" ng-model="selectedPaymentCopy.amount" />
<label>Amount</label>
</div>
</div>