首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确地将这些文本的中心垂直对齐在开关开关旁边?

如何正确地将这些文本的中心垂直对齐在开关开关旁边?
EN

Stack Overflow用户
提问于 2017-06-12 07:43:33
回答 4查看 37关注 0票数 0

问题:

我在CSS方面有点生疏,所以这可能是个简单的问题。

这就是我的页面的相关部分现在的样子。

我已经尽可能地提取了代码,以便在JSFiddle中复制它。

https://jsfiddle.net/srap5maw/

我已经摆弄过它,尝试过我的Google,但是找不到任何明显的东西。

代码片段:

代码语言:javascript
复制
<div class="centering-table">
<div class="centering-row">
  <div class="centering-element">
    <b>Yes</b>
  </div>
  <div class="centering-element">
    <div class="switch-container">
      <label class="switch">
        <input type="checkbox">
        <div class="slider round"></div>
      </label>
    </div>
  </div>
  <div class="centering-element">
    <b>No</b>
  </div>
</div>
</div>
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2017-06-12 07:53:28

您需要将开关更改为block (而不是inline-block),然后将vertical-align:middle添加到.centering-element。我还从.centering-element中移除了线高。

代码语言:javascript
复制
* {
    padding: 0;
    margin: 0;
    outline: 0;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    text-align: center;
    font-family: Arial;
    font-weight: 400;
    font-size: 20px;
    line-height: 28px;
}

.centering-table {
    margin-top: 20px;
    font-size: 26px;
    display: table;
    margin: 0 auto;
}

.centering-row {
    display: table-row;
}

.centering-element {
    display: table-cell;
    vertical-align:middle;
}

.centering-element b {
    font-weight: 600;
}

.switch-container {
    position: relative;
}

.switch input {
    display: none;
}

.switch {
    position: relative;
    display: block;
    width: 60px;
    height: 34px;
}

.switch input {
    display: none;
}

.slider.round {
    border-radius: 34px;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
    border: 1px solid rgba(211,211,211, .8);
}

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

.slider.round:before {
    border-radius: 50%;
}
代码语言:javascript
复制
<div class="centering-table">
  <div class="centering-row">
    <div class="centering-element">
      <b>Yes</b>
    </div>
    <div class="centering-element">
      <div class="switch-container">
        <label class="switch">
          <input type="checkbox">
          <div class="slider round"></div>
        </label>
      </div>
    </div>
    <div class="centering-element">
      <b>No</b>
    </div>
  </div>
</div>

更新小提琴

票数 2
EN

Stack Overflow用户

发布于 2017-06-12 07:51:03

定义.centering-row如下:

代码语言:javascript
复制
.centering-row {
  display: flex;
  align-items: center;
  justify-content: center;
}

.switch应该是一个块元素:

代码语言:javascript
复制
.switch {
  position: relative;
  display: block;
  width: 60px;
  height: 34px;
}

代码语言:javascript
复制
* {
    padding: 0;
    margin: 0;
    outline: 0;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    text-align: center;
    font-family: Arial;
    font-weight: 400;
    font-size: 20px;
    line-height: 28px;
}

.centering-table {
    margin-top: 20px;
    font-size: 26px;
    display: table;
    margin: 0 auto;
}

.centering-row {
    display: flex;
    align-items: center;
    justify-content: center;
}

.centering-element {
    display: table-cell;
    line-height: 50px;
}

.centering-element b {
    font-weight: 600;
}

.switch-container {
    position: relative;
}

.switch input {
    display: none;
}

.switch {
    position: relative;
    display: block;
    width: 60px;
    height: 34px;
}

.switch input {
    display: none;
}

.slider.round {
    border-radius: 34px;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
    border: 1px solid rgba(211,211,211, .8);
}

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

.slider.round:before {
    border-radius: 50%;
}
代码语言:javascript
复制
<div class="centering-table">
  <div class="centering-row">
    <div class="centering-element">
      <b>Yes</b>
    </div>
    <div class="centering-element">
      <div class="switch-container">
        <label class="switch">
          <input type="checkbox">
          <div class="slider round"></div>
        </label>
      </div>
    </div>
    <div class="centering-element">
      <b>No</b>
    </div>
  </div>
</div>

票数 2
EN

Stack Overflow用户

发布于 2017-06-12 07:57:05

您可以使用flex垂直中心您的东西。

代码语言:javascript
复制
<div class="centering-table">

    <div class="centering-element"><b>Yes</b></div>

    <div class="centering-element">
      <div class="switch-container">
        <label class="switch">
          <input type="checkbox">
          <div class="slider round"></div>
        </label>
      </div>
    </div>

    <div class="centering-element"><b>No</b></div>

</div>

CSS

代码语言:javascript
复制
* {
    padding: 0;
    margin: 0;
    outline: 0;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    text-align: center;
    font-family: Arial;
    font-weight: 400;
    font-size: 20px;
    line-height: 28px;
}

.centering-table {
    margin-top: 20px;
    font-size: 26px;
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 200px;
    margin: 0 auto;
    background: transparent;
}

.centering-row {
}

.centering-element {
    align-self: auto;
    background: transparent;
}

.centering-element b {
    font-weight: 600;
}

.switch input {
    display: none;
}

.switch {
    position: relative;
    display: block;
    width: 60px;
    height: 34px;
}

.switch input {
    display: none;
}

.slider.round {
    border-radius: 34px;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: white;
    -webkit-transition: .4s;
    transition: .4s;
    border: 1px solid rgba(211,211,211, .8);
}

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

.slider.round:before {
    border-radius: 50%;
}

见:https://jsfiddle.net/srap5maw/2/。希望这能有所帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44493949

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档