【问题标题】:align text and toggle button in html css在html css中对齐文本和切换按钮
【发布时间】:2023-04-04 00:45:01
【问题描述】:

我创建了一个带有 html 输入复选框和自定义 css 的自定义切换按钮,看起来像这样

您可以看到文本和切换按钮未对齐,我尝试添加边距/填充/高度没有任何效果。 这是html和相应的css ->

<h5 style="display:inline" class="switch">Company</h5>
<label class="switch">
  <input type="checkbox" id="company-survey-checker">
  <span class="slider round"></span>
</label>
<h5 style="display:inline">Survey</h5> 

而这里操作的css是->

<style>
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  -webkit-transition: .4s;
  transition: .4s;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  -webkit-transition: .4s;
  transition: .4s;
}

input:checked + .slider {
  background-color: #2196F3;
}

input:focus + .slider {
  box-shadow: 0 0 1px #2196F3;
}

input:checked + .slider:before {
  -webkit-transform: translateX(26px);
  -ms-transform: translateX(26px);
  transform: translateX(26px);
}

/* Rounded sliders */
.slider.round {
  border-radius: 34px;
}

.slider.round:before {
  border-radius: 50%;
}
</style>

理想情况下,文本和切换按钮应该在同一行,但我发现很难实现,非常感谢任何帮助

【问题讨论】:

    标签: html css checkbox


    【解决方案1】:

    vertical-align:middle 添加到您的label

    .switch {
      position: relative;
      display: inline-block;
      width: 60px;
      height: 34px;
      vertical-align: middle
    }
    
    .switch input {
      opacity: 0;
      width: 0;
      height: 0;
    }
    
    .slider {
      position: absolute;
      cursor: pointer;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: #ccc;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    .slider:before {
      position: absolute;
      content: "";
      height: 26px;
      width: 26px;
      left: 4px;
      bottom: 4px;
      background-color: white;
      -webkit-transition: .4s;
      transition: .4s;
    }
    
    input:checked+.slider {
      background-color: #2196F3;
    }
    
    input:focus+.slider {
      box-shadow: 0 0 1px #2196F3;
    }
    
    input:checked+.slider:before {
      -webkit-transform: translateX(26px);
      -ms-transform: translateX(26px);
      transform: translateX(26px);
    }
    
    
    /* Rounded sliders */
    
    .slider.round {
      border-radius: 34px;
    }
    
    .slider.round:before {
      border-radius: 50%;
    }
    <h5 style="display:inline" class="switch">Company</h5>
    <label class="switch">
      <input type="checkbox" id="company-survey-checker">
      <span class="slider round"></span>
    </label>
    <h5 style="display:inline">Survey</h5>

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 1970-01-01
      • 2019-02-03
      • 2022-10-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-10
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多