html - 将两个 div 并排放置

标签 html css

我正在尝试将两个 div 放在一起。 html

<div id="status">
  <div id ="wrapLeft">
    <input type="text" placeholder="username"/>
    <input type="password" placeholder="password"/>
  </div>
  <div id ="wrapRight">
    <input type="text" placeholder="url"/>
    <input type="text" placeholder="url"/>
  </div>
  <div id="clear"></div>
</div>

CSS

  body {
    font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
    font-size: 100%;
  }      #status {
    /* avoid an excessively wide status text */
    white-space: pre;
    text-overflow: ellipsis;
    overflow: hidden;
    width: 600px
  }
  input{
    width:100px;
    height:25px;
    border-top:0px;
    border-left: 0px;
    border-right: 0px;
    outline: none;
  }
  #wrapLeft{
    position: relative;
    width: 30%;
    height: 100%;
    background: black;
    float: left
  }
  #wrapRight{
    position: relative;
    width: 65%;
    height: 100%;
    background: red;
    float: right;
  }
  #clear{
    clear:both;
  }

然而不知何故,div 不在同一层,右边的在左边的下面。如何解决这个问题?我尝试使用左右浮动,但它没有对齐哪个应该对齐。什么会导致这个? example

感谢帮助

最佳答案

对主要div 使用display:flex

再看看CSS,我已经删除了一些CSS。

body {
    font-family: "Segoe UI", "Lucida Grande", Tahoma, sans-serif;
    font-size: 100%;
  }      #status {
    /* avoid an excessively wide status text */
    white-space: pre;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  
  #status{
  width:100%;
  display:flex;  /* It's Important */ 
  }
  input{
    width:100px;
    height:25px;
    border-top:0px;
    border-left: 0px;
    border-right: 0px;
    outline: none;
  }
  #wrapLeft{
    width: 30%;
    height: 100%;
    background: black;

  }
  #wrapRight{
    width: 70%;
    height: 100%;
    background: red;
  }
 
<div id="status">
  <div id ="wrapLeft">
    <input type="text" placeholder="username"/>
    <input type="password" placeholder="password"/>
  </div>
  <div id ="wrapRight">
    <input type="text" placeholder="url"/>
    <input type="text" placeholder="url"/>
  </div>
  <div id="clear"></div>
</div>

关于html - 将两个 div 并排放置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45787055/

相关文章:

html - 在使用 ngIf 的下拉按钮上优化 css

javascript - 文本未显示在网站上

javascript - 为什么我的 HTML 中嵌入的用于随机化列表的 JavaScript 不起作用?

Jquery 访问大量嵌套的输入

javascript - 滑动框错误

javascript - 当动态图像数量时在html中水平分布

css - 强制 child 服从 parent 在CSS中的 flex 边界

html - 查找并修复页面底部的巨大空白

css - 字体速记在 Firefox 中不起作用

javascript - 在 Bootstrap Modal 中使用 HandsonTable 不起作用