Use css to change a property when another property is True - html

I want my input box to be visible when a slider is selected. My css code is:
CSS:
input:not(:checked) + .slider2:before {
content:"Informal";
.new {
type: "visible";
}
}
HTML:
<label class="switch">
<input type="checkbox" checked>
<div class="slider"></div>
</label>
<input id="new" type="hidden">
...but it's not working. How can I have it so when input:not(:checked) + .slider2:before is True, set properties of .new? I am open to changing the display, not the input type.

If you want the text box to be shown only when checkbox is not checked
you can do like this
.switch {
position: relative;
display: inline-block;
/*(sliderwidth*2 + left)*/
width: 72px;
height: 34px;
}
/*.switch input {display:none;}*/
.slider {
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
}
.slider:before {
/*Its the white box*/
position: absolute;
content: "";
height: 26px;
width: 32px;
left: 4px;
bottom: 4px;
background-color: white;
}
input:checked + .slider {
background-color: #2196F3;
}
input:checked + .slider:before {
transform: translateX(32px);
/*must be same as slider width*/
}
input:checked + .slider:after {
content:"Off";
}
input:not(:checked) + .slider:before {
content:"On";
}
#new {
display:none;
}
input:not(:checked) + .slider:before {
content:"Informal";
}
input:not(:checked) + .slider + #new{
display:block;
}
#new{
position:absolute;
right:20;
bottom:0;
margin-left:90px;
margin-top:0px;
}
<label class="switch">
<input type="checkbox">
<div class="slider"></div>
<input id="new" type="text" value="whatever" >
</label>
Hope this helps

Related

CSS: How to set url image properly to checked checkbox

I have this scenario, I have this list of checkbox with corresponding image for it, If the checkbox is checked, I want to append black circle at the back of checkbox image
Sample current output:
Sample expected output:
Code for populating checkbox:
<div
key={item.id}
className="chk-multiple-badge form-check form-check-inline"
>
<input
className="chkbox-badge form-check-input"
type="checkbox"
id={item.id}
/>
<label
htmlFor={item.id}
className="form-check-label form-check-label-badge"
>
<Row>
<Col>
<img
className="chk-badge-img"
src={item.imgBase64}
alt={item.badge_name}
/>
</Col>
</Row>
<Row>
<Col>{item.badge_name}</Col>
</Row>
</label>
</div>
And CSS for checkbox:
:checked + .form-check-label-badge:before {
content: url("../../../../assets/images/checkd-badge.png");
position: absolute;
cursor: pointer;
}
.chkbox-badge {
display: none;
}
Pure semantic HTML/CSS solution
This is easy to implement
This is what you need to do:
Your checkboxes need to have distinct id attributes. This allows you to connect a to it, using the label's for-attribute.
Example:
<input type="checkbox" id="myCheckbox1" />
<label for="myCheckbox1"><img src="http://someurl" /></label>
Here is a working snippet :
ul {
list-style-type: none;
}
li {
display: inline-block;
}
input[type="checkbox"][id^="cb"] {
display: none;
}
label {
border: 4px solid transparent; border-radius: 50%;
display: block;
position: relative;
margin: 10px;
cursor: pointer;
}
label:before {
background-color: white;
color: white;
content: " ";
display: block;
border-radius: 50%;
border: 1px solid grey;
position: absolute;
top: -5px;
left: -5px;
width: 25px;
height: 25px;
text-align: center;
line-height: 28px;
transition-duration: 0.4s;
transform: scale(0);
}
label img {
height: 100px;
width: 100px;
transition-duration: 0.2s;
transform-origin: 50% 50%;
}
:checked + label {
border-color: #000;
}
:checked + label:before {
content: "✓";
background-color: grey;
transform: scale(1);
}
:checked + label img {
transform: scale(0.9);
border-radius: 50%;
z-index: -1;
}
<ul>
<li><input type="checkbox" id="cb1" />
<label for="cb1"><img src="https://m.media-amazon.com/images/I/61Dnvcie7PL._SL1024_.jpg" /></label>
</li>
<li><input type="checkbox" id="cb2" />
<label for="cb2"><img src="https://m.media-amazon.com/images/I/91vx4DQg4jS._AC_SX466_.jpg" /></label>
</li>
<li><input type="checkbox" id="cb3" />
<label for="cb3"><img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSZ31Gis0FZirNYl6OGbnHlXWJTtgZXUVB6eNt7DomL5ZnK_v94cJpNLw24jKMcuQW3q7U&usqp=CAU" /></label>
</li>
</ul>

HTML/CSS, a checkbox checked make another buttons unchecked

enter image description hereI have a toggle button implemented as checkbox element and some buttons, there are all clickable, the problem is: when this toggle button be clicked, the button clicked before will be unclicked.
Here is the html and css file on github:
https://github.com/jokao1030/Test/tree/b121d8ff0b586cdfc34d1f65996aec9c7a73965c
Maybe someone know how is it going? Please give me some advise, thank you !
It is becoming unchecked because you are not using checkboxes, you are using buttons. Buttons are not meant for use like this, they are meant for one time clicks. As soon as you click somewhere else on the page, the button is no longer active which is why your styling no longer applies to it. Instead, use <input type="radio"> or <input type="checkbox">
I think you will want to do more than this, but this should get you started.
Create an array to keep track of the selected cities.
When the slider is clicked, pop off the last element in the array (which removes that city from the array of selected cities), and at the same time remove the "selected" class so that the background coloring is also removed
const selected = [];
document.querySelectorAll('button').forEach((el) => {
el.addEventListener('click', (e) => {
const city = e.target.classList[0];
document.querySelector(`.${city}`).classList.add('selected');
if (!selected.includes(city)) selected.push(city);
console.log(selected);
});
});
document.querySelector('#check').addEventListener('click', () => {
const uncity = selected.pop();
document.querySelector(`.${uncity}`).classList.remove('selected');
console.log(selected);
});
.choicediv, .ein_auspendler{
position:relative;
max-width: 500px;
}
.selected{
background: #d6d8ff;
}
.slider{
position: absolute;
cursor: pointer;
border: 1.5px solid #adb0ff;
border-radius:10px;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ffffff;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
font-weight: bold;
border-radius:6px;
height: 24px;
width: 80px;
left:3px;
bottom: 1px;
top: 4px;
background-color: #d6d8ff;
-webkit-transition: .4s;
transition: .4s;
}
#check:checked + .slider:before {
-webkit-transform: translateX(150px);
-ms-transform: translateX(150px);
transform: translateX(150px);
}
.text {
color: black;
}
.text:after {
position: absolute;
top: 7px;
right: 8px;
content: "Auspendler";
font-size:14px;
}
.text:before {
position: absolute;
top: 7px;
left: 8px;
content: "Einpendler";
font-size:14px;
}
#check + .slider + .text:after {
font-weight:normal;
}
#check + .slider + .text:before {
font-weight:bold;
}
#check:checked + .slider + .text:after {
font-weight:bold;
}
#check:checked + .slider + .text:before {
font-weight:normal;
}
.auswahl_buttons{
position:relative;
right:0px;
top:10px;
width:240px;
height:36px;
}
/* BUTTONS */
button{
background-color: white;
border: 1px solid #D6D8FF;
border-radius: 12px;
}
button:hover{
background: #D6D8FF;
}
button:focus{
background: #D6D8FF;
};
<div class="choicediv">
<h1>Pendlermobilität</h1>
<h2>Berlin - Brandenburg</h2>
<p1>Möchten Sie eingehende oder<br></p1>
<p2>ausgehende Bewegeungen ansehen?<br></p2>
<label class="ein_auspendler">
<input type="checkbox" id="check" unchecked>
<div class="slider"></div>
<div class="text"></div>
</label>
<p3>Von wo soll es losgehen?<br></p3>
<!-- ---------BUTTONS------------->
<div class="container">
<button class ="Berlin">Berlin</button>
<button class ="Barlim">Barlim</button>
<button class="Cottbus">Cottbus</button>
<button class="Dahme-Spreewald">Dahme-Spreewald</button>
<button class="Frankfurt (Oder)">Frankfurt (Oder)</button>
<button class="Havelland">Havelland</button>
<button class="Märkisch-Oderland">Märkisch-Oderland</button>
<button class="Oberhavel">Oberhavel</button>
<button class="Oberspreewald-Lausitz">Oberspreewald-Lausitz</button>
<button class="Oder-Spree">Oder-Spree</button>
<button class="Ostprignitz-Ruppin">Ostprignitz-Ruppin</button>
<button class="Potsdam">Potsdam</button>
<button class="Potsdam-Mittelmark">Potsdam-Mittelmark</button>
<button class="Prignitz">Prignitz</button>
<button class="Spree-Neiße">Spree-Neiße</button>
<button class="Teltow-Fläming">Teltow-Fläming</button>
<button class="Uckermark">Uckermark</button>
</div>

CSS Checkbox styling - size color change

I am trying to do simple checkbox styling.
The color change is not working.
How to change the color and border?
input[type=checkbox]{
height: 25px;
width: 25px;
background-color: #eee;
}
input[type=checkbox]:checked {
background-color: #3ee738;
}
<p>
<input type="checkbox" id="showall" name="showall" value=0>
<label for="showall">Show All</label>
</p>
<p>
<input type="checkbox" id="showfl" name="showfl" value=0>
<label for="showfl">Filtered</label>
</p>
<p>
input[type=checkbox] + label {
display: block;
margin: 0.2em;
cursor: pointer;
padding: 0.2em;
}
input[type=checkbox] {
display: none;
}
input[type=checkbox] + label:before {
content: "\2714";
border: 0.1em solid MediumSeaGreen;
border-radius: 0.2em;
display: inline-block;
width: 1em;
height: 1em;
padding-left: 0.2em;
padding-bottom: 0.3em;
margin-right: 0.2em;
vertical-align: bottom;
color: transparent;
transition: .2s;
}
input[type=checkbox] + label:active:before {
transform: scale(0);
}
input[type=checkbox]:checked + label:before {
background-color: MediumSeaGreen;
border-color: MediumSeaGreen;
color: #fff;
}
input[type=checkbox]:disabled + label:before {
transform: scale(1);
border-color: #aaa;
}
input[type=checkbox]:checked:disabled + label:before {
transform: scale(1);
background-color: #bfb;
border-color: #bfb;
}
<p>
<input type="checkbox" id="showall" name="showall" value=0>
<label for="showall">Show All</label>
</p>
<p>
<input type="checkbox" id="showfl" name="showfl" value=0>
<label for="showfl">Filtered</label>
</p>
<p>
Checkboxes are not currently able to be styled with CSS, as they are typically based on the user's operating system's default styling. The way to get around this with CSS is to style another element, in your case, the <label> tag to serve as the visual indicator of the checkbox, and then some pseudo selector and pseudo element CSS to trigger the change when the input is checked.
Because the input and the label are connected via the id and for attributes, shrinking the input hide it works effectively and still maintains accessibility.
li {
position:relative;
}
input {
height:1px;
width:1px;
position:absolute;
opacity:0;
top:0;
left:0;
}
label {
cursor:pointer;
padding-left:25px;
}
label::before {
content:"";
position:absolute;
left:0;
top:0;
height:15px;
width:15px;
border:1px solid blue;
}
label::after {
content:"";
color:white;
position:absolute;
left:2px;
top:0;
}
input[type=checkbox]:checked + label::before {
background:blue;
}
input[type=checkbox]:checked + label::after {
content:"✓";
}
<ul>
<li><input id="cat" type="checkbox"> <label for="cat">Cat</label></li>
<li><input id="dog" type="checkbox"> <label for="dog">Dog</label></li>
</ul>
What you need to do is to hide the actual html element of the checkbox and create a new one that can be styled easily.
So the logic of this approach is to have a span for the box and a span for the icon and style each accordingly - showing the icon when the input is checked using the :checked pseudoselector.
Note that the label wraps around the whole group so that clicking anywhere on the label will toggle the checkbox - doing it this way removeds the need for the for attribute.
I also added a checked function that will toggle the other checkbox whebn the showAll check is checked.
Edit - note that you can use other methods of getting the icon in there- you may prefer the font awesome fa-check icon for example.
const showAll = document.querySelector("#showAll");
showAll.addEventListener('click', () => toggleAll());
function toggleAll() {
document.querySelector("#showfl").checked = showAll.checked;
}
.checkbox-wrapper {
margin-bottom: 16px;
}
input[type=checkbox]{
display: none
}
.checkbox-box{
display: inline-block;
height: 25px;
width: 25px;
background-color: #eee;
text-align: center
}
.checkbox-icon {
visibility: hidden
}
input[type=checkbox]:checked + .checkbox-box{
background-color: #3ee738;
}
input[type=checkbox]:checked + .checkbox-box .checkbox-icon {
visibility: visible
}
<div class="checkbox-wrapper">
<label>
<input type="checkbox" id="showAll" name="showAll" />
<span class="checkbox-box">
<span class="checkbox-icon">&check;</span>
</span> Show All
</label>
</div>
<div class="checkbox-wrapper">
<label>
<input type="checkbox" id="showfl" name="showfl" />
<span class="checkbox-box">
<span class="checkbox-icon">&check;</span>
</span> Filtered
</label>
</div>

CSS - Change parent div background-color on input check

.chk-circle {
width: 8px;
height: 8px;
background: #afb0b5;
border-radius: 100%;
position: relative;
float: left;
margin-top: 4px;
margin-right: 8px;
}
.chk-circle label {
display: block;
width: 4px;
height: 4px;
border-radius: 100px;
cursor: pointer;
position: absolute;
top: 2px;
left: 2px;
z-index: 1;
background: #fff;
}
.chk-hide {
visibility: hidden;
}
.chk-circle input[type=checkbox]:checked + label {
background: #63a70a;
}
<div class="chk-circle">
<input class="chk-hide" type="checkbox" id="chk1"/>
<label for="chk1"></label>
</div>
Which produces the following:
What I want to do is have the outer circle turn green when the input is checked, I have tried the following but the reverse happens:
Try this:
Apply with border property :
Updated
I was added the i for bullet creation.If you need increase the bullet size increase the width & height of I tag.And also added text in label. Both condition are working
.chk-circle input[type=checkbox]:checked + i {
top:0;
left:0;
border:2px solid green;
}
.chk-circle > i {
position:relative;
float:left;
display: block;
width: 6px;
height: 6px;
margin-top:5px ;
border-radius: 100px;
cursor: pointer;
z-index: 1;
border:3px solid #ddd;
background: #fff;
cursor:pointer;
}
.chk-circle > label{
cursor:pointer;
margin:0 10px auto;
}
.chk-hide {
visibility: hidden;
}
.chk-circle > input[type=checkbox]:checked + i{
border:3px solid green;
}
<div class="chk-circle">
<label for="chk1">some 1</label>
<input class="chk-hide" type="checkbox" id="chk1"/>
<i></i>
</div>
<div class="chk-circle">
<label for="chk2">some 2</label>
<input class="chk-hide" type="checkbox" id="chk2"/>
<i></i>
</div>
<div class="chk-circle">
<label for="chk3">some 3</label>
<input class="chk-hide" type="checkbox" id="chk3"/>
<i></i>
</div>
Try border-color property:
.chk-circle input[type=checkbox]:checked + label {
border-style: solid;
border-color: #0f0;
}
div {
background: pink;
width: 50px;
height:50px;
}
input[type=checkbox]:checked+div{
background: green;
}
input[type=checkbox]:checked + div p {
background: blue;
}
<input type="checkbox" checked>
<div>
<p>Test</p>
</div>
if you can change the structure of your html:
<input class="chk-hide" type="checkbox" id="chk1"/>
<div class="chk-circle">
<label for="chk1"></label>
</div>
you can do it like this:
.chk-hide[type=checkbox]:checked +.chk-circle {background: green;}
Example

CSS Star Rating w/ radio buttons

I am working on a css star rating system with radios.
However I can't seem to work out what I am doing wrong here...it seems to highlight when I click...but it should be highlighting when I hover.
<fieldset>
<input type="radio" id="star-5" name="rating-01" value="5"><label for="star-5"></label>
<input type="radio" id="star-4" name="rating-01" value="4"><label for="star-4"></label>
<input type="radio" id="star-3" name="rating-01" value="3"><label for="star-3"></label>
<input type="radio" id="star-2" name="rating-01" value="2"><label for="star-2"></label>
<input type="radio" id="star-1" name="rating-01" value="1"><label for="star-1"></label>
</fieldset>
input[type=radio] { display: none; visibility: collapse; }
input[type=radio] + label {
position: relative; float: right; clear: none; display: block;
width: 18.4%; height: 120px; margin: 0 1% 0 1%; padding: 0;
outline: 0; cursor: pointer;
background-color: green;
}
input[type=radio]#star-1 + label { margin: 0 1% 0 0; } input[type=radio]#star-5 + label { margin: 0 0 0 1%; }
input[type=radio]:hover + label,
input[type=radio]:hover + label ~ input[type=radio] + label {
background-color: red;
}
UPDATE
So this is what I managed to do so far: JSFiddle;
CSS:
input[type=radio] { display: none; visibility: collapse; }
input[type=radio] + label {
position: relative; float: right; clear: none; display: block;
width: 18.4%; height: 120px; margin: 0 1% 0 1%; padding: 0;
outline: 0; cursor: pointer; background-color: green;
}
input[type=radio]#star-1 + label { margin: 0 1% 0 0; } input[type=radio]#star-5 + label { margin: 0 0 0 1%; }
fieldset > input[type=radio]:checked ~ label {
background-color: blue;
}
fieldset:hover > input[type=radio] + label:hover,
fieldset:hover > input[type=radio] + label:hover ~ label {
background-color: gold;
}
However there still is a problem...when you select the boxes and they go blue. And you hover back the blue stays while the hovered over part is yellow.
How can I reset the blue once I hover over?
There is something I created long time ago. There is javascript too, for calculating total votes.
Fiddle example
Code snipet :
function showme(id) {
document.getElementById('result').innerHTML = 'My vote : '+id.value;
var r = document.getElementById('res').value.split(',');
var s='';
for(var i=0;i<5;i++) {
if(id.value==(i+1).toString()) {
var x = parseInt(r[i])+1;
r[i]=x.toString();
}
if(i==4) {s+=r[i];} else {s+=r[i]+',';}
}
document.getElementById('res').value=s;
calc();
}
function calc() {
var x=document.getElementById('res').value.split(',');
var r=0;
var t=0;
for(var i=0;i<5;i++) {
t+=parseInt(x[i]);
r+=(parseInt(x[i])*(i+1));
}
var s=parseInt((r/t)*20);
document.getElementById('bar').style.width=s.toString()+'%';
document.getElementById('sta').innerHTML=s.toString()+'%';
}
#divRateCnt {
width: 130px;
position: relative;
}
#divRareCnt, #divStarsCnt{
height: 26px;
background: url(http://s4.postimg.org/wi683zp2h/stars.png) 0 0px repeat-x;
}
#divRateCnt input{
display: none;
}
#divRateCnt label{
display: none;
position: absolute;
top: 0;
left: 0;
height: 26px;
width: 130px;
cursor: pointer;
}
#divRateCnt:hover label{
display: block;
}
#divRateCnt label:hover{
background: url(http://s4.postimg.org/wi683zp2h/stars.png) 0 -52px repeat-x;
}
#divRateCnt label + input + label{width: 104px;}
#divRateCnt label + input + label + input + label{width: 78px;}
#divRateCnt label + input + label + input + label + input + label{width: 52px;}
#divRateCnt label + input + label + input + label + input + label + input + label{width: 26px;}
#divRateCnt input:checked + label{
display: block;
background: url(http://s4.postimg.org/wi683zp2h/stars.png) 0 -52px repeat-x;
}
<input type="hidden" value="0,0,0,0,0" id="res" />
<div id="divRateCnt">
<div id="divStarsCnt"></div>
<input type="radio" name="rating" id="star5" value="5" onchange="showme(this);"><label for="star5"></label>
<input type="radio" name="rating" id="star4" value="4" onchange="showme(this);"><label for="star4"></label>
<input type="radio" name="rating" id="star3" value="3" onchange="showme(this);"><label for="star3"></label>
<input type="radio" name="rating" id="star2" value="2" onchange="showme(this);"><label for="star2"></label>
<input type="radio" name="rating" id="star1" value="1" onchange="showme(this);"><label for="star1"></label>
</div>
<br>
<span id="result"></span>
<br><br>
Total vote(s) :<br>
<div style="width:130px;height:26px;background:url(http://s4.postimg.org/wi683zp2h/stars.png) 0 0 repeat-x;position:relative;">
<div id="bar" style="width:130px;height:26px;background:url(http://s4.postimg.org/wi683zp2h/stars.png) 0 -26px repeat-x;position:absolute;top:0;left:0;width:0%;"></div>
</div>
<span id="sta"></span>
https://jsfiddle.net/ypne7t2w/1/
this resets the label color when hovering the fieldset, but doesn't target the label hover:
fieldset:hover > input[type=radio] + label,
fieldset:hover > input[type=radio] + label ~ label {
background-color: green;
}
Here you go. Probably the simplest pure css solution you could come up with.
HTML
<div class="stars">
<input type="radio" id="rate-5" name="rating-1">
<label for="rate-5"></label>
<input type="radio" id="rate-4" name="rating-1">
<label for="rate-4"></label>
<input type="radio" id="rate-3" name="rating-1">
<label for="rate-3"></label>
<input type="radio" id="rate-2" name="rating-1">
<label for="rate-2"></label>
<input type="radio" id="rate-1" name="rating-1">
<label for="rate-1"></label>
</div>
CSS
.stars {
float: left;
overflow: hidden;
width: 100%;
}
.stars input[type=radio]:checked ~ label:after {
background: blue;
}
.stars input[type=radio] {
display: none;
}
.stars input[type=radio]:first-child + label {
padding-right: 0;
}
.stars:hover input[type=radio]:checked ~ label:after,
.stars label:after {
background: green;
}
.stars label {
box-sizing: border-box;
width: 20%;
padding-right: 2%;
height: 120px;
float: right;
cursor: pointer;
}
.stars label:after {
display: block;
content: "";
height: 120px;
width: 100%;
float: right;
transition: all 0.25s;
background: green;
}
.stars label:hover:after,
.stars label:hover ~ label:after {
background: gold !important;
}
https://jsfiddle.net/rsyfchfo/
Hope this helps :)
[update] Added "reset selection on hover".
[update2] Added hover on white space between stars.