I want to make selections more visual for colors. For example I want a way to add either an HTML select menu or checkbox to select color.
(blue color square) - Blue
(white color square) - White
(red color square) - Red.
Is it possible to do with select menu? If not whats best way to do it with checkboxes? stylize each one differently with CSS? Or add an image in front of it?
Any code samples or links appreciated, thx.
Since you're only looking for a few colors instead of the whole range, it's probably best to build your own input. We're going to want:
input[type=radio] to select the color
Labels for the inputs containing the label and color
To do this, we can do something like this
<form>
<div class="colorChoice">
<input type="radio" name="color" value="#ff0000" id="select-red">
<label for="select-red"><div class="bg-red"></div> Red</label>
</div>
...
</form>
Each div contains the HTML for each color selection: an input with the value equal to the color's hex code (although you could change this to whatever you want) and a label with a color box and the associated color name.
To create the color boxes, we can set the size and background color of the divs. Doing CSS is probably best, it's wasteful to load images that really aren't necessary.
.colorChoice label div {
width: 24px;
height: 16px;
display: inline-block;
}
.bg-red {
background-color: #ff0000;
}
...
An advantage of this is that the bg-* classes are not just limited to this form - it can also be applied to other elements that need background colors.
.colorChoice label div {
width: 24px;
height: 16px;
display: inline-block;
}
.bg-red {
background-color: #ff0000;
}
.bg-green {
background-color: #00ff00;
}
.bg-blue {
background-color: #0000ff;
}
.bg-black {
background-color: #000000;
}
<form>
<div class="colorChoice">
<input type="radio" name="color" value="#ff0000" id="select-red">
<label for="select-red"><div class="bg-red"></div> Red</label>
</div>
<div class="colorChoice">
<input type="radio" name="color" value="#00ff00" id="select-green">
<label for="select-green"><div class="bg-green"></div> Green</label>
</div>
<div class="colorChoice">
<input type="radio" name="color" value="#0000ff" id="select-blue">
<label for="select-blue"><div class="bg-blue"></div> Blue</label>
</div>
<div class="colorChoice">
<input type="radio" name="color" value="#000000" id="select-black">
<label for="select-black"><div class="bg-black"></div> Black</label>
</div>
</form>
If you want to use checkboxes instead, you can replace name="color" value="..." with just name="..." and change the input types from radio to checkbox.
I am trying to directly style <label> but based on value "black"
The html is output by a Wordpress plugin and the <label> has a #before selector which I would like to target for changing and icon color. However I wish to change the icon color multiple times based on the radio value .. In the case of value="black" I wish to change the icon to black.
Can anybody point me in the right direction?
<div class="mspc-text-wrapper">
<strong class="mspc-attribute-title">Black</strong>
<div class="mspc-radio ui radio checkbox">
<input type="radio" name="pa_colour" value="black">
<label>
::before
::after
</label>
</div>
</div>
You select it using the adjacent sibling selector + like this:
input[value=black] + label::before
label::before { content: '::before'; } /* just so the result is visible */
label::after { content: '::after'; } /* just so the result is visible */
input[value=black] + label::before { color: red; }
input[value=black] + label::after { color: green; }
<div class="mspc-text-wrapper">
<strong class="mspc-attribute-title">Black</strong>
<div class="mspc-radio ui radio checkbox">
<input type="radio" name="pa_colour" value="black">
<label>
</label>
</div>
</div>
I'm having css issues with a second label on a radio button, when this radio button is checked. I already found out that a second (and a third) label is possible using 'for' in the label-tag. It's not possible to group everything in a single label-tag.
How can I change the background for the second label when a radio button is checked?
My (simplified) code is below, for the first label it works, second label it doesn't.
.radioclass:checked + label {
background-color: cyan;
}
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
The + selector in css means "immediate sibling", i.e. the very next element after the input must be a label. This will only select the immediate sibling.
An alternative is ~ which means "any sibling" and targets any label after the input.
In both of these cases, the elements (input and label) are on the same dom level. There is no way to traverse up the dom, grab the sibling and then the label - which is what you are trying to do with the html supplied.
If you are able to change the html, then either place the input outside the two divs or place both labels inside the same div (after the input).
.radioclass:checked ~ div label {
background-color: cyan;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<div class="col-sm-4">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
.radioclass:checked ~ label {
background-color: cyan;
}
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
<label for="id_radiobtn1">second label</label>
</div>
</div>
If you cannot alter the html, then JS is the only other approach (I've changed the input to a checkbox for demonstration purposes):
$('input').on('change', function(){
var func = 'removeClass';
if($(this).is(':checked')) {
func = 'addClass';
}
$('label[for="'+$(this).attr('id')+'"]')[func]('checked');
});
label.checked {
background-color: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="checkbox" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
Using just CSS this is possible, although it does require a rearrangement of your HTML, effectively moving the radio <input> ahead of the elements in which the <label> elements are contained; this removes the (impossible in CSS) requirement of traversing to the parent of the <input> in order to style the non-sibling <label> elements.
/* selects the element with the id of 'id_radiobtn1' when it is
checked, uses the general sibling combinator (~) to select
all sibling <div> elements with the class of 'col-sm-4'
and then finds the descendant <label> elements within
that/those <div> elements whose 'for' attribute-value is
equal to 'id_radiobtn1': */
#id_radiobtn1:checked ~ div.col-sm-4 label[for=id_radiobtn1] {
background-color: cyan;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1" />
<div class="col-sm-4">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
</div>
Now, if you must have a visible radio <input> besides the first of the <label> elements, you can use a pseudo-element to effectively pretend, while also hiding the real radio <input>:
*,
::before,
::after {
/* to include border and padding in the calculated
dimensions (width/height) of the elements: */
box-sizing: border-box;
}
.row>input[type=radio] {
/* hiding the radio <input> elements: */
display: none;
}
/* selecting the <input> elements whose type is
equal to 'radio', which is checked and then
finding all (subsequent) sibling elements
(using the '~' combinator) which <div>
elements with a class of 'col-sm-4', and
traversing to the <label> elements within: */
input[type=radio]:checked~div.col-sm-4 label {
background-color: cyan;
}
label {
/* in order to ensure that the descendant
pseudo-elements are positioned relative
to their parent element: */
position: relative;
/* making room for the pseudo-elements: */
margin-left: 2em;
}
input[type=radio]+.col-sm-4 label::before {
/* the content property is required, even
if only an empty string, to have the
pseudo-element show up: */
content: '';
display: inline-block;
/* positioning absolutely, in relation to
the closest ancestor with a non 'static'
position, in this case the parent
<label> element: */
position: absolute;
/* moving it outside of the <label> element's
left border, to avoid the background-color: */
left: -2em;
/* purely aesthetic: */
width: 0.8em;
height: 0.8em;
/* arbitrary positioning, adjust to taste: */
top: 50%;
transform: translateY(-40%);
/* making the pseudo-element circular in shape: */
border-radius: 50%;
/* colouring the faux 'border': */
box-shadow: 0 0 0 2px #ccc;
}
/* adjusting the colours of the faux radio: */
input[type=radio]:checked+.col-sm-4 label::before {
box-shadow: 0 0 0 2px #000, inset 0 0 0 3px #fff;
background-color: limegreen;
}
<div class="row">
<input class="radioclass" id="id_radiobtn1" type="radio" name="group1" value="1" />
<div class="col-sm-4">
<label for="id_radiobtn1">row one first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">row one second label</label>
</div>
</div>
<div class="row">
<input class="radioclass" id="id_radiobtn2" type="radio" name="group1" value="2" />
<div class="col-sm-4">
<label for="id_radiobtn2">row two first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn2">row two second label</label>
</div>
</div>
You can do with Jquery on change event.
$(document).ready(function() {
$('#id_radiobtn1').on('change',function(){
$( "label:contains('second')" ).css( "background-color", "red" );
});
});
.radioclass:checked + label {
background-color: cyan;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1">second label</label>
</div>
You can't do it with pure css, because backward in css is impossible.You can help of Jquery:
Note: I insert lab2 class to label.
$(document).ready(function(){
$('input[type=radio]').on('change',function(){
if($('.radioclass').is(':checked'))
$('.lab2').addClass('sel');
else
$('.lab2').removeClass('sel');
})
})
$(document).ready(function(){
$('input[type=radio]').on('change',function(){
if($('.radioclass').is(':checked'))
$('.lab2').addClass('sel');
else
$('.lab2').removeClass('sel');
})
})
#id_radiobtn1:checked + label {
background-color: green;
}
.sel {
background-color: red;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="row">
<div class="col-sm-4">
<input class="radioclass" id="id_radiobtn1" type="radio" name="radiobtn1" value="1">
<label for="id_radiobtn1">first label</label>
</div>
<div class="col-sm-4">
<label for="id_radiobtn1" class="lab2">second label</label>
</div>
</div>
I have a DIV with check boxes and labels defined by CSS style when checked:
#project input:checked + label {background-color:#787878;color:white;}
I want to make two buttons with exceptions to color:white.
Checkbox ID="B" label background-color to turn red and Checkbox ID="C" label background-color to turn blue. I can't figure it out how to add that specificity.
Thanks in advance.
EDIT: (adding HTML code)
<input type="checkbox" id="B" value="B" style="display:none;" onclick="document.getElementById('B').style.color = 'red'"><label for="B"> checkbox B </label>
This is my failed attempt with a line level change. I only posted CSS because I thought this could be done in the style.
You don't need the Javascript. Also add #B, #C { display: none; } if needed.
#B:checked + label {
background: red;
color: white;
}
#C:checked + label {
background: blue;
color: white;
}
<input type="checkbox" id="B" />
<label for="B">test</label>
<input type="checkbox" id="C" />
<label for="C">test</label>
Is this what you are looking for input[id="test"]:checked this styles inputs with specific id's and checked state.
And you cant style the background-color in a checkbox as far as i know.
I just want to know how to change the color of placeholder text in my html page.
I used
::-webkit-input-placeholder { color:red; }
It works, but changed the placeholder text colors in my whole page. So let me know if there is any way which we can specify the placeholders or input in html, so that we can change the place holder text color of desired area only.
try this code
.fname::-webkit-input-placeholder {
color: red
}
<div class="content">
<input type="text" name="firstname" class="fname" placeholder="placeholder">
</div>
<input type="text" name="lastname" class="lname" placeholder="placeholder">
or may be can use
.content ::-webkit-input-placeholder {
color: red
}
It will change placeholders only for Chrome. To get individual styles try this CSS instead:
.changed::-webkit-input-placeholder{
color: red;
}
.changed::-moz-placeholder {
color: red;
}
.changed:-ms-input-placeholder {
color: red;
}
<input type="text" class="changed" placeholder="Changed placeholder">
<input type="text" class="original" placeholder="Original placeholder">