2017-06-12 31 views
0

問:如何正確地垂直對齊切換開關旁邊的這些文本的中心?

我在CSS有點生疏了,所以這可能是一個簡單的問題。

這就是我的網頁的相關部分此刻的樣子。

toggle and text

而且我是最好的,我可以複製它在的jsfiddle拉出的代碼。

https://jsfiddle.net/srap5maw/

我和它周圍的擺弄,並試圖我的谷歌,但富找不到任何明顯的。

代碼片斷:

<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

您需要將交換機更改爲block(而不是inline-block),然後添加到vertical-align:middle.centering-element。我也已經刪除了的line-height從.centering-element

* { 
 
    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%; 
 
}
<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>

Updated fiddle

2

定義.centering-row如下:

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

而且.switch應該是一個塊元件:

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

* { 
 
    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%; 
 
}
<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>

0

您可以使用柔性CSS垂直中心的東西。

HTML

<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

* { 
    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

只要做到:

1)插入到vertical-align: middle;.centering-element

2)將vertical-align: middle;.switch

Demo