Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Align label left of toggle switch

I have the following code and currently the label shows on the right side of the toggle switch and I would like to have it on its left. The CSS code is part of a file that ideally should not be modified directly, but expanded. Thanks!

.switch {
  /* Finetune the switch */
  --height: 18px;
  --width: 40px;
  --border: 2px;
  --font-size: 1em;
  --switch-color-checked: #1795fe;
  --switch-color-unchecked: gray;
  --dot-color-checked: white;
  --dot-color-unchecked: white;
  font-size: var(--size);
}

.switch label {
  display: block;
  position: relative;
  cursor: pointer;
  padding-left: calc(var(--width) + 1em);
  min-width: var(--width);
  min-height: var(--height);
  /* Switch Background */
  /* Dot */
}

.switch label::before,
.switch label::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  transition: 0.25s ease-in-out;
  box-sizing: border-box;
}

.switch label::before {
  z-index: 1;
  background-color: var(--switch-color-unchecked);
  width: var(--width);
  height: var(--height);
  border-radius: calc(var(--height) * 0.5);
}

.switch label::after {
  z-index: 2;
  background-color: var(--dot-color-unchecked);
  height: calc(var(--height) - (var(--border) * 2));
  width: calc(var(--height) - (var(--border) * 2));
  transform: translate(var(--border), var(--border));
  border-radius: calc(var(--height) / 2);
}

.switch input {
  width: 0;
  height: 0;
  visibility: hidden;
  display: none;
}

.switch input:checked+label::before {
  transition: 0.5s;
  background-color: var(--switch-color-checked);
}

.switch input:checked+label::after {
  transform: translate(calc(var(--width) - 100% - var(--border)), var(--border));
  background-color: var(--dot-color-checked);
}
<div class="switch">
  <input id="switch__input" type="checkbox">
  <label for="switch__input">
    I am a label that wants to be on the left
  </label>
</div>

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

See CSS overrides in the HTML panel.

.switch {
  /* Finetune the switch */
  --height: 18px;
  --width: 40px;
  --border: 2px;
  --font-size: 1em;
  --switch-color-checked: #1795fe;
  --switch-color-unchecked: gray;
  --dot-color-checked: white;
  --dot-color-unchecked: white;
  font-size: var(--size);
}

.switch label {
  display: block;
  position: relative;
  cursor: pointer;
  padding-left: calc(var(--width) + 1em);
  min-width: var(--width);
  min-height: var(--height);
  /* Switch Background */
  /* Dot */
}

.switch label::before,
.switch label::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  transition: 0.25s ease-in-out;
  box-sizing: border-box;
}

.switch label::before {
  z-index: 1;
  background-color: var(--switch-color-unchecked);
  width: var(--width);
  height: var(--height);
  border-radius: calc(var(--height) * 0.5);
}

.switch label::after {
  z-index: 2;
  background-color: var(--dot-color-unchecked);
  height: calc(var(--height) - (var(--border) * 2));
  width: calc(var(--height) - (var(--border) * 2));
  transform: translate(var(--border), var(--border));
  border-radius: calc(var(--height) / 2);
}

.switch input {
  width: 0;
  height: 0;
  visibility: hidden;
  display: none;
}

.switch input:checked+label::before {
  transition: 0.5s;
  background-color: var(--switch-color-checked);
}

.switch input:checked+label::after {
  transform: translate(calc(var(--width) - 100% - var(--border)), var(--border));
  background-color: var(--dot-color-checked);
}
<style>
  /* overrides to be included in your custom stylesheet */
  
  .switch label {
    display: flex;
    padding-left: 0;
  }
  
  .switch label::before,
  .switch label::after {
    position: relative;
    order: 2; /* could technically be 1, but this is more intuitive */
    margin-left: 8px;
  }
  
  .switch label::after {
    margin-left: -40px;
  }
</style>

<div class="switch">
  <input id="switch__input" type="checkbox">
  <label for="switch__input">
    I am a label that wants to be on the left
  </label>
</div>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading