问题:
我对CSS有点生疏,所以这可能是一个简单的问题。
这就是我的页面的相关部分目前的样子。
我尽可能地在JSFiddle中复制代码。
https://jsfiddle.net/srap5maw/
我已经摆弄它,并尝试了我的Google-foo但找不到任何明显的东西。
代码段:
<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 :(得分: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%;
}
&#13;
<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>
&#13;
答案 1 :(得分:2)
您需要将切换更改为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>
答案 2 :(得分:0)
您可以使用flex 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/。希望这会有所帮助。
答案 3 :(得分:0)
只需:
1)将
vertical-align: middle;
插入.centering-element
2)将
vertical-align: middle;
插入.switch