Я обнаружил, что это переключатель неморфизма, и не могу понять, как изменить фон, если он отмечен. Я понимаю, насколько прост этот вопрос. Я делал это несколько раз с основными переключателями, но мне кажется, что либо я сбит с толку, либо этот шаблон проектирования игнорирует мои попытки.
До сих пор я пробовал разные решения, такие как:
input[type = "checkbox"]:checked {
background: red
}
или же
.toggle:checked {
background: red
}
К сожалению, ни один из них не справляется со своей задачей. Я знаю, что это основной вопрос, и подумал, что вы, ребята, могли бы направить меня к правильному решению, пока я борюсь.
body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
font-family: 'IBM Plex Sans Condensed', sans-serif;
font-weight: 300;
background-color: #ecf0f3;
}
.label {
display: inline-flex;
align-items: center;
cursor: pointer;
color: #394a56;
}
.label-text {
margin-left: 16px;
}
.toggle {
position: relative;
height: 30px;
width: 60px;
border-radius: 15px;
overflow: hidden;
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6,
4px 4px 4px 0px #d1d9e6 inset,
-4px -4px 4px 0px #ffffff inset;
}
.toggle-state {
display: none;
}
input[type=checkbox] {
background: yellow
}
.indicator {
height: 100%;
width: 200%;
background: #ecf0f3;
border-radius: 15px;
transform: translate3d(-75%, 0, 0);
transition: transform 0.4s cubic-bezier(0.85, 0.05, 0.18, 1.35);
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6;
}
.toggle-state:checked ~ .indicator {
transform: translate3d(25%, 0, 0);
}<label class = "label">
<div class = "toggle">
<input class = "toggle-state" type = "checkbox" name = "check" value = "check" />
<div class = "indicator"></div>
</div>
<div class = "label-text">change toggle background</div>
</label>@ m4n0 Я пытаюсь изменить фон заливки.
Вы не можете изменить фон самого флажка, потому что флажок скрыт. Он заменен на div .toggle, поэтому вместо этого вам нужно установить его фон.






Вы пытаетесь применить фон к входным данным, но класс индикатора переопределяет его. Почему бы не передать это свойство самому классу показатель?
body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
font-family: 'IBM Plex Sans Condensed', sans-serif;
font-weight: 300;
background-color: #ecf0f3;
}
.label {
display: inline-flex;
align-items: center;
cursor: pointer;
color: #394a56;
}
.label-text {
margin-left: 16px;
}
.toggle {
position: relative;
height: 30px;
width: 60px;
border-radius: 15px;
overflow: hidden;
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6,
4px 4px 4px 0px #d1d9e6 inset,
-4px -4px 4px 0px #ffffff inset;
}
.toggle-state {
display: none;
}
input[type=checkbox] {
background: yellow
}
.indicator {
height: 100%;
width: 200%;
background: #ecf0f3;
border-radius: 15px;
transform: translate3d(-75%, 0, 0);
transition: transform 0.4s cubic-bezier(0.85, 0.05, 0.18, 1.35);
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6;
}
.toggle-state:checked ~ .indicator {
transform: translate3d(25%, 0, 0);
background: red;
}<label class = "label">
<div class = "toggle">
<input class = "toggle-state" type = "checkbox" name = "check" value = "check" />
<div class = "indicator"></div>
</div>
<div class = "label-text">change toggle background</div>
</label>@Sumodh_Nair У меня тоже было, но я все еще хочу изменить фон заливки вместо круглого индикатора. Мне это не кажется таким уж чистым, это дело вкуса.
body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
font-family: 'IBM Plex Sans Condensed', sans-serif;
font-weight: 300;
background-color: #ecf0f3;
}
.label {
display: inline-flex;
align-items: center;
cursor: pointer;
color: #394a56;
}
.label-text {
margin-left: 16px;
}
.toggle {
position: relative;
height: 30px;
width: 60px;
border-radius: 15px;
overflow: hidden;
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6,
4px 4px 4px 0px #d1d9e6 inset,
-4px -4px 4px 0px #ffffff inset;
}
.toggle-state {
display: none;
}
input[type=checkbox]:checked ~ .indicator {
background: red;
}
.indicator {
height: 100%;
width: 200%;
background: #ecf0f3;
border-radius: 15px;
transform: translate3d(-75%, 0, 0);
transition: transform 0.4s cubic-bezier(0.85, 0.05, 0.18, 1.35);
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6;
}
.toggle-state:checked ~ .indicator {
transform: translate3d(25%, 0, 0);
}<label class = "label">
<div class = "toggle">
<input class = "toggle-state" type = "checkbox" name = "check" value = "check" />
<div class = "indicator"></div>
</div>
<div class = "label-text">change toggle background</div>
</label>@Gaurav_Shukla благодарит за ваш вклад, но я ищу решение, чтобы изменить цвет заливки за круглым индикатором.
@ICoded добро пожаловать. Но я постараюсь исправить вашу ошибку.
Вам не нужно помещать ввод внутри div, вы можете поместить его перед ним, и пока он находится в ярлыке, он будет работать нормально. И тогда вы можете обновить селекторы
body {
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
font-family: 'IBM Plex Sans Condensed', sans-serif;
font-weight: 300;
background-color: #ecf0f3;
}
.label {
display: inline-flex;
align-items: center;
cursor: pointer;
color: #394a56;
}
.label-text {
margin-left: 16px;
}
.toggle {
position: relative;
height: 30px;
width: 60px;
border-radius: 15px;
overflow: hidden;
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6,
4px 4px 4px 0px #d1d9e6 inset,
-4px -4px 4px 0px #ffffff inset;
transition: background 0.3s ease;
}
.toggle-state {
display: none;
}
.indicator {
height: 100%;
width: 200%;
background: #ecf0f3;
border-radius: 15px;
transform: translate3d(-75%, 0, 0);
transition: transform 0.4s cubic-bezier(0.85, 0.05, 0.18, 1.35);
box-shadow:
-8px -4px 8px 0px #ffffff,
8px 4px 12px 0px #d1d9e6;
}
input[type=checkbox] +.toggle { /* Now you can select the div next to the input */
background: yellow
}
.toggle-state:checked + .toggle {
background: red;
}
.toggle-state:checked + .toggle .indicator {
transform: translate3d(25%, 0, 0);
}<label class = "label">
<input class = "toggle-state" type = "checkbox" name = "check" value = "check" />
<div class = "toggle">
<div class = "indicator"></div>
</div>
<div class = "label-text">change toggle background</div>
</label>
Вы хотите изменить фон круглой кнопки или фон таблетки?