In CSS, selecting parent <label> for an <input> that is selected - css

Edit: apparently cant use <> braces or it hides names?...
I've seen a few variations of this question asked, however none of what I found fits my particular issue, which I think is a simple issue. I am creating the following radio button group in react:
const myOptions = ["YTD", "Week", "Month", "Pre AS", "Post AS"]
const myButtons =
<form>
<div className="radio-group">
{myOptions.map((d, i) => {
return (
<label>
<input
type={"radio"}
value={myOptions[i]}
checked={timeframeNew === myOptions[i]}
onChange={this.handleTimeframeNewChange}
/>
<span>{myOptions[i]}</span>
</label>
)
})}
</div>
</form>;
and here is my current CSS for styling the buttons to look nice...
input[type=radio] {
position: absolute;
visibility: hidden;
display: none;
}
label {
color: #333;
background: #EEE;
display: inline-block;
cursor: pointer;
font-weight: bold;
padding: 5px 20px;
border: 2px solid orange;
}
input[type=radio]:checked + label {
color: red;
background: #333;
}
label + input[type=radio] + label {
border-left: solid 2px blue;
}
.radio-group {
border: solid 2px green;
display: inline-block;
margin: 20px;
border-radius: 10px;
overflow: hidden;
}
Unfortunately, the CSS is not working as intended. In particular, the following selection - input[type=radio]:checked + label does not work because there is no label immediately after an input. The only way so far I have been able to successfully get my react onChange handler function to work is by putting the input inside of the label, like this, and then returning the label in each .map loop.
*Since the return needs to be a single element, if I want to take the out of the label, I would need to then include them both in a div or a span, and for some reason doing so breaks my onChange handler...
So my question is, how how how can I, in CSS, grab the label that corresponds to the clicked input. I would like to change the entire label's color and background when it is / isn't clicked, so selecting the span does not help (since that only changes the texts color/background, not the whole label.
Thanks in advance!!

CSS can select child and sibling elements, but not parent elements. I often hide default radio buttons and checkboxes and create my own, like this:
.button-group{
font-size:0; /*Prevents a space from occuring between buttons*/
}
.button-group input{
position:absolute;
visibility:hidden; /* display:none causes some browsers to ignore the input altogether */
}
.button-group input+span{
display:inline-block;
line-height:20px;
font-size:1rem;
vertical-align:top;
padding:0 10px;
color:#000;
border-left:1px solid #a00;
}
.button-group label:first-child input+span{
border-radius:10px 0 0 10px;
border-left:0;
}
.button-group label:last-child input+span{
border-radius:0 10px 10px 0;
}
.button-group input:not(:checked)+span{
background-color:#faa;
}
.button-group input:not(:checked)+span:hover{
background-color:#f66;
}
input[type=radio]:checked+span{
background-color:#f33;
}
<div class="button-group">
<label>
<input type="radio" value="1" name="myfield" />
<span>Option 1</span>
</label>
<label>
<input type="radio" value="2" name="myfield" />
<span>Option 2</span>
</label>
<label>
<input type="radio" value="3" name="myfield" />
<span>Option 3</span>
</label>
</div>

*Since the return needs to be a single element, if I want to take the out of the label, I would need to then include them both in a div or a span, and for some reason doing so breaks my onChange handler...
You can use <React.Fragment> <input /> <span /> </ReactFragment> to return multiple elements without rendering them inside a div or span

Related

Custom CSS to radio button labels

I need to add custom styling to the checked/active radio button behind the label.
I can get the border and width of the buttons fine, just can't set a background color to the checked/active button only. As the input-label is outside the div I can't seem to manage it.
I can't mess with the code below, can only change CSS.
Can anyone help me please?
<label class="radio-inline display-block col-sm-3" for="concern" style="color: rgb(102, 102, 102);">
<span class="has-pretty-child">
<div class="clearfix prettyradio labelright blue has-pretty-child">
<input class="radio the_input_element" name="runway_surface" id="concern" value="Concern" style="display: block !important; color: rgb(50, 55, 60);" autocomplete="off" type="radio">
<a class="checked fa fa-check ui-state-active" style="color: rgb(0, 163, 201);"></a>
</div>
<span class="input-label radio-label" style="color: rgb(102, 102, 102);">Concern</span>
</span>
Something like this:
The idea here is the label element which can create amazing things.
Playing with it can generate you a bunch of great effects.
You just need to hide the radios or checkboxes and work with its labels, and you need to know three important css selectors for this effect:
The general next sibling: element ~ sibling{ style } which select all the sibling found after the element
The direct next sibling: element + sibling{ style } which select only the first sibling after the element
The checked input selector: input:checked{ style } which selects the input if it's checked only.
And this effect can be done with these steps:
Create an input and a label for every choice you need
Connect every input with its label using the for and id
Hide the input using something like display: none or others
Set a style for your label which will be the default mode
Set a new style for the label that placed after a checked input input:checked + lebel{ style }
Now we can apply it:
nav{
width: fit-content;
border: 1px solid #666;
border-radius: 4px;
overflow: hidden;
display: flex;
flex-direction: row;
flex-wrap: no-wrap;
}
nav input{ display: none; }
nav label{
font-family: sans-serif;
padding: 10px 16px;
border-right: 1px solid #ccc;
cursor: pointer;
transition: all 0.3s;
}
nav label:last-of-type{ border-right: 0; }
nav label:hover{
background: #eee;
}
nav input:checked + label{
background: #becbff;
}
<nav>
<input type="radio" id="x1" name="x"/>
<label for="x1">Choice 1</label>
<input type="radio" id="x2" name="x"/>
<label for="x2">Choice 2</label>
<input type="radio" id="x3" name="x"/>
<label for="x3">Choice 3</label>
<input type="radio" id="x4" name="x"/>
<label for="x4">Choice 4</label>
<!-- as many choices as you like -->
</nav>
And it's done now.
You can search for many many ideas on codepen and you can see this great navigation bar using only css and navigates throw the different pages:
Nav Bar Using Only CSS
Or See this collapsed nav bar that can be opened or closed using only css too:
Open & Close Nav Bar Using CSS
To style a checkbox or a radio button you need to hide the real input and style another element to look like the one you need. Please refer to this w3schools page for a tutorial on how to do this: https://www.w3schools.com/howto/howto_css_custom_checkbox.asp
you need to hide the real input and style another element to look like the one you need. Please refer to this
<input type='checkbox' name='checkbox-btns' id='checkbox-btn-2'/> <label htmlFor='checkbox-btn-2' class='btn'> Unsafe</label>
input[type=checkbox]{display:none;}
input[type=checkbox] + label.btn{
width:300px;
padding: 5px 5px;
text-align: center;
margin: 5px 5px;
display: inline-block;
text-transform: capitalize;
font-size: 10px;
font-weight: 600;
outline: none;
position: relative;
-webkit-transition: all 0.3s;
-moz-transition: all 0.3s;
transition: all 0.3s;
border: 1px solid #0088cc;
color: #0088cc;
-webkit-transition: none;
-moz-transition: none;
transition: none;
border-radius: 10px;
cursor: pointer;
}
input[type=checkbox]:checked + label.btn{
background: #0088cc;
color:white;
}

How to make semantic input with text choice and arrow behavior like number input

I'm looking to make inputs like this with choices low/medium/high and have arrow controls similar to input[type=number]. What's a semantic way to code this?
If you want to be semantic and accessible; But also keep this design;
Your best choice is to use input[type=radio] and label[for].
You'll have to use :checked pseudo-attribute and ~ adjacent selectors.
Here is a working snippet, I didn't spent time on design.
.almost-select {
display: inline-block;
background: #EEE;
border: #CCC solid;
border-radius: .2rem;
}
.almost-select input,
.almost-select output{
display: none;
}
.almost-select .buttons {
display: inline-block;
border-left: inherit;
}
.almost-select label {
font-size: .5em;
}
.almost-select #temp_high:checked ~ output[for='temp_high'] {
display: inline-block;
}
.almost-select #temp_low:checked ~ output[for='temp_low'] {
display: inline-block;
}
<div class='almost-select'>
<input type='radio' name='temp' id='temp_high' checked />
<input type='radio' name='temp' id='temp_low' />
<output for='temp_high'>High</output>
<output for='temp_low'>Low</output>
<div class='buttons'>
<label for='temp_high'>▲</label>
<label for='temp_low'>▼</label>
</div>
</div>
But how many options do you have ?
If you have a select with many many options, it will result in a really big CSS...

Is it posible to make an input checkbox a Bootstrap glyphicon?

Is it posible to make an input checkbox a Bootstrap glyphicon?
I want to make up the default checkboxes with a nice Bootstrap glyphicon.
For example: glyphicon glyphicon-unchecked and when checked: glyphicon glyphicon-check.
I've tried this:
input[type='checkbox'] {
position: relative;
top: 1px;
display: inline-block;
font-family: 'Glyphicons Halflings';
font-style: normal;
font-weight: 400;
line-height: 1;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
content: "\e157";
}
But nothing happened..
I don't know if that's posible?
You can achieve that in a couple of methods:
Method 1
inside a <label> tag, two <tags> that represent the icons that you want need to be placed(or outside, per use scenario)
then toggle these two <tags>, when the input[type='checkbox'] is checked or unchecked
done.
Method 2
a cleaner approach to the above one, would be to use the css from bootstraps icons, and place them in a :before(or :after depending on your scenarion) on the <label> tag
then toggle the content prop. of the :before class, that the icons that you want have, when the input[type='checkbox'] is checked or unchecked
done.
Check out the demo here and also, a couple of more through documentation on this matter:
Add boostrap icon to input boxes
Boostrap checkbox documentation
If you're able to modify your markup a bit, this should do:
<label for="myCheckbox" class="glyphy">
<input type="checkbox" id="myCheckbox" />
<span class="glyphicon glyphicon-unchecked"></span>
label words
</label>
$('.glyphy').click(function (e) {
if ($(e.target).is('input')) { // prevent double-event due to bubbling
$(this).find('.glyphicon').toggleClass('glyphicon-check glyphicon-unchecked');
}
});
Demo
if you have the icons, you can style it as such: http://jsfiddle.net/swm53ran/164/
/*hide checkbox and radio buttons*/
input[type=checkbox],
input[type=radio] {
width: 2em;
margin: 0;
padding: 0;
font-size: 1em;
opacity: 0; /*This is the part tht actually hides it*/
}
/*normalize the spacing*/
input[type=checkbox] + label,
input[type=radio] + label {
display: inline-block;
margin-left: -2em;
line-height: 1.5em;
}
/*unchecked css*/
input[type=checkbox] + label > span,
input[type=radio] + label > span {
display: inline-block;
background-image: url('http://upload.wikimedia.org/wikipedia/commons/thumb/0/06/Face-sad.svg/48px-Face-sad.svg.png');
width: 50px;
height: 50px;
}
/*selected checkbox css*/
input[type=checkbox]:checked + label > span > span {
width: 50px;
height: 50px;
display:block;
background-image: url('http://wscont1.apps.microsoft.com/winstore/1x/a14c3995-34d7-454c-82e2-0c192e48b91a/Icon.173718.png');
}
/*selected radio css*/
input[type=radio]:checked + label > span > span {
width: 50px;
height: 50px;
display:block;
background-image: url('http://wscont1.apps.microsoft.com/winstore/1x/a14c3995-34d7-454c-82e2-0c192e48b91a/Icon.173718.png');
}
<div>
<input id="check1" type="checkbox" name="check1" value="check1" />
<label for="check1"><span><span></span></span>Checkbox</label>
</div>
<div>
<input id="radio1" type="radio" name="radio" value="radio1" />
<label for="radio1"><span><span></span></span>Radio1</label>
</div>
<div>
<input id="radio2" type="radio" name="radio" value="radio2" />
<label for="radio2"><span><span></span></span>Radio2</label>
</div>

How to show multiple font colors inside the Text Field

i want text inside the text field like "your name* "
the color of "your name" should be black and the color of * should be red.
how can i do this??
Please help me.
You cannot; the value of a text input field is plain text.
Put the explanation, including the requiredness indicator if desired, in a label, not into the field. The you can use markup for the indicator, and color it:
<label for=name>Your name<span class=reqd>*</span>:</label>
<input id=name name=name size=40 required>
I think you should want this
CSS
label{
color:black;
}
label span {
color:red;
}
input{
line-height:25px;
color:gray;
font-size:16px;
}
input:focus{
color:black;
}
HTML
<label>
Your Name:- <input type="text" value="your name"><span>*</span>
</label>
Live demo http://jsfiddle.net/rohitazad/dZmaZ/
You can't have different colours in one text box. (Reliably across browsers anyway)
The most common approach to this issue for required fields is to place the asterisk directly after the text box in an element with a class to set the text to red.
Example: http://jsfiddle.net/JzFd4/
This is what i was asking here in question.
Multiple Colors Placeholder.
Answer Link Here
Some Trick here
.input-field {
position: relative;
display: inline-block;
}
.input-field > label {
position: absolute;
left: 0.5em;
top: 50%;
margin-top: -0.5em;
opacity: 0.5;
}
.input-field > input[type=text]:focus + label {
display: none;
}
.input-field > label > span {
letter-spacing: -2px;
}
.first-letter {
color: red;
}
.second-letter {
color: blue;
<div class="input-field">
<input id="input-text-field" type="text"></input>
<label for="input-text-field">
<span class="first-letter">your name</span>
<span class="second-letter">*</span>
</label>
</div>

Anyway to have a label respond to :focus CSS

Is there any way to have a label respond to focus. I have some code where the textfield has a different style on focus. The label also however needs a slightly different style. I tried this but it did not effect the label.
#header .search label {
background:url(http://www.golfbrowser.com/images/icons/search.png) left top no-repeat;
padding-left:20px;
height:20px;
float:right;
}
#header .search label:focus {
background:url(http://www.golfbrowser.com/images/icons/search-z.png) left top no-repeat;
padding-left:20px;
height:20px;
float:right;
}
#header .search input {
padding:0px;
border:0px;
width:140px;
height:20px;
float:left;
padding-right:10px;
background:url(http://www.golfbrowser.com/images/icons/searchbar.png) right top no-repeat;
}
#header .search input:focus {
padding:0px;
width:190px;
height:20px;
float:left;
padding-right:10px;
background:url(http://www.golfbrowser.com/images/icons/searchbar-z.png) right top no-repeat;
}
The label contains an image and the other part of a round corner and it too must change colour in order for the field to look correct.
Any ideas,
Marvellous
You can't actually give focus to a label. It's not a focusable form element. Besides, even if you could do that, then whatever previously had focus (that means your input) would lose it to the label anyway.
You may have to restructure your HTML (and modify your CSS accordingly) so that you can select input:focus and then that input's corresponding label. For instance, if you moved your label after your input and used the following CSS selector for your label, you should be able to accomplish what you want.
#header .search input:focus + label
BoltClock's answer is the more semantic, lightweight way of achieving this functionality. However it's not always possible to have a specific HTML structure (especially to facilitate a minor feature like this) and CSS lacks the ability to traverse up the HTML hierarchy. To get around that, here are two alternatives. The first is coming soon (CSS spec 4) and the second is our old mate Javascript.
First up, CSS4's :focus-within pseudo selector. This does exactly what it says on the tin (scopes based on any child element's active focus state). Read more about the spec here. So assuming you have a HTML structure of:
<div class="input-wrapper">
<label for="elem">Label Text
<input name="elem" id="elem" type="text" />
</label>
</div>
you could scope the 'focussed' label by simply:
label:focus-within{}
by the same token, you could also scope the parent div with:
div.input-wrapper:focus-within{}
Magical. But not for use today :(
Second up, if you're already using a JS selector engine (i.e. jQuery, Sizzle, etc.), you could also do something along the lines of:
$('input').on("focus", function(){
var input = $(this);
// assuming label is the parent, i.e. <label><input /></label>
var label = $(this).parent();
label.addClass('focussed');
input.on("blur", function(){
label.removeClass('focussed');
input.off("blur");
});
});
This is untested but the essence of what this achieves is using a class rather than the :focus pseudo selector. So you can add your focussed styles to label.focussed{}. I've added (and self-removed) the blur handler to facilitate removing the class.
Now using Flex box will solve this. Have the label element follow the input element in the HTML. Then use flex-direction: column-reverse to change its position to appear above the input element. You can then use the input:focus + label: {} to target the label.
.input-container {
display: flex;
flex-direction: column-reverse;
}
.input-container > input {
/* your input styles */
}
.input-container > input:focus + label {
/* targets the label when the input receives focus */
color: red;
}
<div class="input-container">
<input type='email' />
<label>Email</label>
</div>
use:checked instead of :focus and you must give id,name,value into 'input'.
Found a good solution - order property made a trick:
input:focus {
background-color: #F2FFF0;
}
* {
font-family: "Arial";
font-size: 13px;
}
div.settings {
display:grid;
grid-template-columns: max-content max-content;
grid-gap: 7px
}
div.settings label {
text-align:right;
padding-top: 3px
}
div.settings label:after {
content: ":";
}
div.settings input:focus + label:before {
content: "\25B6 ";
font-size: 12px;
color: red;
}
input {
border: 1px solid #ccc;
padding: 2px 4px;
font-size: 13px;
}
<div class="settings">
<input style="order:2" type="text" title="(vardas, pavardė ir pan.)" autocomplete="off" id="name" name="name" required minlength="4" maxlength="128" size="50"><label style="order:1" for="name">Pirkėjas</label>
<input style="order:4" type="text" title="" autocomplete="off" id="company" name="company" required minlength="4" maxlength="128"><label style="order:3" for="company">Įmonės pavadinimas</label>
<input style="order:6" type="text" title="" autocomplete="off" id="companyCode" name="companyCode" required minlength="4" maxlength="128"><label style="order:5; min-width: 160px" for="companyCode">Įmonės (asmens) kodas</label>
</div>

Resources