两个div并排且水平对齐

时间:2019-01-09 13:03:17

标签: javascript html css

我试图将两个<div>并排对齐,并将它们对齐在同一行中。一个包含一个复选框,可以使用CSS更改为切换按钮,另一个包含纯文本。问题是,当我尝试使它们并排放置时,其内容未正确垂直对齐。切换按钮高于第二个<div>中的文本。

这是我的代码:

.switch {
  position: relative;
  display: inline-block;
  width: 47px;
  height: 25px;
}

.switch input {
  display: none;
}

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

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

input:checked+.slider {
  background-color: #2196F3;
}
<div style="float:left;">
  <div style="float: left;">
    <p id="show_crash_toggle_text" style="font-size:20px;" <b>User</b>&emsp;</p>
  </div>
  <div style="margin-left: 60px;">
    <label class="switch">
          <input type="checkbox" onclick = "show_crash_log()">
          <span class="slider"></span>
        </label>
  </div>
</div>
<div style="margin-left:150px;">
  <b style="font-size:20px">Search by: </b>
</div>

有些图片显示了问题。


5 个答案:

答案 0 :(得分:1)

如果使用flexbox,则可以使用align-items: center来水平对齐div。

.flex-container {
  display:flex;
  align-items: center;
}

.switch {
position: relative;
display: inline-block;
width: 47px;
height: 25px;
}

.switch input {display:none;}

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

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

input:checked + .slider {
background-color: #2196F3;
}
<div class="flex-container">
    <p id="show_crash_toggle_text" style="font-size:20px;"><b>User</b></p>
  <div>
    <label class="switch">
      <input type="checkbox" onclick = "">
      <span class="slider"></span>
    </label>
  </div>
<b style="font-size:20px">Search by: </b>
</div>

答案 1 :(得分:0)

CSS代码,用于水平div对齐

<style>
.container {
  width: 100%;
  margin: 0 auto;
}
.block {
  width: 100px;
  float: left;
}

</style>

HTML内容

<div  class="container" >

  <div  class="block">
    <p id="show_crash_toggle_text" >  <b>User</b>&emsp;</p>
  </div>

  <div  class="block" >
    <p id="show_crash_toggle_text">  <b>User</b>&emsp;</p>
  </div>

  <div class="block" > 
    <p>
    <label class="switch">
      <input type="checkbox" onclick = "show_crash_log()">
      <span class="slider"></span>
    </label>
    </p>
  </div>

  <div  class="block" >
    <p> Search by: </p>
   </div>

</div>

尝试此代码段。

答案 2 :(得分:0)

可以避免使用内联CSS

.switch {
position: relative;
display: inline-block;
width: 47px;
height: 25px;
}

.switch input {display:none;}

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

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

input:checked + .slider {
background-color: #2196F3;
}
.user-switch-wrap{
  width: 50%;
  display: flex;
  justify-content: space-around;
  align-items: center;

}
<div class="user-switch-wrap">
  <div class="user-txt">
    <p id="show_crash_toggle_text"><b>User</b></p>
  </div>
  <div class="user-switch">
    <label class="switch">
      <input type="checkbox" onclick = "show_crash_log()">
      <span class="slider"></span>
    </label>
  </div>
  <div class="search-by">
    <b>Search by: </b>
  </div>
</div>

答案 3 :(得分:0)

我进行了一些更改,对此进行检查

HTML

<div class="parent">
  <div class="child">
    <p id="show_crash_toggle_text" style="font-size:20px;" <b>User</b>&emsp;</p>
  </div>
  <div>
    <label class="switch">
      <input type="checkbox" onclick = "show_crash_log()">
      <span class="slider"></span>
    </label>
  </div>
  <div>
    <b style="font-size:20px">Search by: </b>
  </div>
</div>

CSS

.parent {
  display: flex;
  align-items: center;
}
.switch {
    position: relative;
    display: inline-block;
    width: 47px;
    height: 25px;
    margin:0px 10px;
}

.switch input {display:none;}

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

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

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

答案 4 :(得分:0)

试试看,伙计:

.switch {
  position: relative;
  display: inline-block;
  width: 47px;
  height: 25px;
}

.switch input {
  display: none;
}

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

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

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


.search-box,
.search-box__toggle{
    display: flex;
    flex-direction: row;
    justify-content: center;
    justify-items: center;
    align-items: center;
}

.search-box__toggle  + * {
  margin-left: 10px;
}
<div class="search-box">
    <div class="search-box__toggle">
        <p id="show_crash_toggle_text" style="font-size:20px;" <b>User</b>&emsp;</p>
        <label class="switch">
            <input type="checkbox" onclick = "show_crash_log()">
            <span class="slider"></span>
        </label>
    </div>
    <div>
        <b style="font-size:20px">Search by: </b>
    </div>
</div>