问题:
我在CSS方面有点生疏,所以这可能是个简单的问题。
这就是我的页面的相关部分现在的样子。

我已经尽可能地提取了代码,以便在JSFiddle中复制它。
https://jsfiddle.net/srap5maw/
我已经摆弄过它,尝试过我的Google,但是找不到任何明显的东西。
代码片段:
<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>发布于 2017-06-12 07:53:28
您需要将开关更改为block (而不是inline-block),然后将vertical-align:middle添加到.centering-element。我还从.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>
发布于 2017-06-12 07:51:03
定义.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>
发布于 2017-06-12 07:57:05
您可以使用flex垂直中心您的东西。
<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/。希望这能有所帮助。
https://stackoverflow.com/questions/44493949
复制相似问题