且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

CSS反向转换并选择以前的同级

更新时间:2022-10-17 22:57:48

您可以使用 flexbox 执行此操作。正如Selva所提到的,CSS只能选择下一个兄弟,而不是以前的。但是,使用flexbox更容易:

  .flexible {display:flex; flex-direction:row;} 
#form1:hover {
width:100%;
}
#form2:hover {
width:100%;
}

https://jsfiddle.net/2g4krj0f/


Bootstrap container has two forms horizontally. Hovering over Form1 will increase the width and decreases the width of form2. but reverse is not happening. Any suggestions please.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

  <style>
  #row{
  background-color: lightblue;
  border-style: groove;
  }

  #form1{
  background-color: lightgreen;
  width: 50%;
  transition: width 0.75s ease;
  }

  #form1:hover{  
    width: 75%;
  }

  #form1:hover + #form2{
     width: 25%
   }

  #form2{
  background-color: orange;
  width: 50%;
  transition: width 0.75s ease;
  }  

  #form2:hover{
  width: 75%;  
  }

  #form2:hover ~ #form1{
     width: 25%
     }
  </style>


</head>
<body>

<div  class="container" >

  <div id="row" class="row" >
    <div id="form1" class="col-sm-6">
    <!-- form1 -->
        <form class="form-horizontal">
  <div class="form-group">
    <label class="control-label col-sm-2" for="email">Email:</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="email" placeholder="Enter email">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="pwd">Password:</label>
    <div class="col-sm-10"> 
      <input type="password" class="form-control" id="pwd" placeholder="Enter password">
    </div>
  </div>
  <div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10">
      <div class="checkbox">
        <label><input type="checkbox"> Remember me</label>
      </div>
    </div>
  </div>
  <div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Submit</button>
    </div>
  </div>
</form>
    </div>
    <div id="form2" class="col-sm-6">
    <!-- form 2 -->
        <form class="form-horizontal">
  <div class="form-group">
    <label class="control-label col-sm-2" for="email">Email:</label>
    <div class="col-sm-10">
      <input type="email" class="form-control" id="email" placeholder="Enter email">
    </div>
  </div>
  <div class="form-group">
    <label class="control-label col-sm-2" for="pwd">Password:</label>
    <div class="col-sm-10"> 
      <input type="password" class="form-control" id="pwd" placeholder="Enter password">
    </div>
  </div>
  <div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10">
      <div class="checkbox">
        <label><input type="checkbox"> Remember me</label>
      </div>
    </div>
  </div>
  <div class="form-group"> 
    <div class="col-sm-offset-2 col-sm-10">
      <button type="submit" class="btn btn-default">Submit</button>
    </div>
  </div>
</form>
    </div>
</div>

</div>

</body>
</html>

Please see the below image. When hovering on Green form, form goes left to right ,Orange form decreases the width and Green form increases the width but this is not happening while hovering on Orange form(Reverse direction).

Basically I'm trying to achieve form like THIS

You can do this with flexbox. As Selva has mentioned, CSS can only select the next sibling, not the previous. However, with flexbox this is much easier:

.flexible{display: flex; flex-direction: row;}
#form1:hover{  
    width: 100%;
}
#form2:hover{  
    width: 100%;
}

https://jsfiddle.net/2g4krj0f/