代码之家  ›  专栏  ›  技术社区  ›  Shubhank Gupta

并排和水平对齐两个DIV

  •  0
  • Shubhank Gupta  · 技术社区  · 7 年前

    我正试着把两条线对齐 <div> s并排排列,并将它们对齐。一个包含一个复选框,该复选框使用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;
    }
    <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 回复  |  直到 7 年前
        1
  •  2
  •   jcal    7 年前

    如果使用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>
        2
  •  0
  •   Murugesan Rathinam    7 年前

    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>
    

    试试这个片段。

        3
  •  0
  •   Varsha Dhadge    7 年前

    可以避免使用内联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>
        4
  •  0
  •   suhail c    7 年前

    我做了一些改变检查这个

    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;
    }
    
        5
  •  0
  •   rflmyk    7 年前

    .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>