【问题标题】:Making 2 divs float:right with one under the other使 2 个 div 浮动:一个在另一个下方
【发布时间】:2013-07-27 19:32:32
【问题描述】:

我不知道如何更好地表达我的标题,所以我在这里解释一下。我有一个带有 3 个 div 的 HTML/CSS 页面。

第一个 div.presentation 没有浮动规则。我想在它的右边制作菜单。

所以我得到了另外两个 div,div.logindiv.categories。他们都有float: right;clear: both;

我想要这样的东西:

 --------------------------------------     ------------------
|div.presentation                      |   |div.login         |  
|                                      |   |                  |
|                                      |   |                  |
|                                      |   |                  |
|                                      |    ------------------
|                                      |
|                                      |    ------------------
|                                      |   |div.categories    |
|                                      |   |                  |
|                                      |   |                  |
|                                      |   |                  |
 --------------------------------------     ------------------

但是div.logindiv.categories 是在一边的。当div.categories 不存在时,浮动规则可以完美运行。

当我在div.logindiv.categories 之间添加<div style="height:100px"></div> 时,我得到了我想要的,但我相信我们可以做得更好。这也太靠运气了。

这 3 个 div 是 display:inline-block

有什么想法吗?非常感谢。

编辑 http://jsfiddle.net/7nJp9/1/

【问题讨论】:

  • 为什么不把登录和类别 div 放在一个 div 中,然后浮动那个 div 对吗?
  • 嘿,Jason,你能分享你的 jsfiddle 链接吗??
  • 当我这样做时,登录和类别 div 重叠。
  • 我的意思是,将浮动内容应用到新 div 并将它们从原来的两个 div 中删除。
  • 这就是我所做的,但它们仍然垂直重叠。 jsfiddle.net/7nJp9/1(谢谢 Ankit,我忘了!)

标签: css html floating


【解决方案1】:

我已经修改了你的 CSS:

body {
  width: 90%;
  margin: auto;
  font-family: Arial, Verdana, sans-serif;
  font-size: 12px;
  background: #e6f0ff;
}

header.beta {
  position: absolute;
  top: 0;
  left: 0;
  width: auto;
  background: #fff7e0;
  opacity: 0.8;
  padding: 10px 50px;
  color: #444443;
}

header.logo {
  width: 90%;
  height: 125px;
  margin: auto;
  margin-top: 60px;
  margin-bottom: 30px;
  border: 1px dotted grey;
}

nav.tabs {
  display: none;
}

div.presentation, div.login, div.categories {
  position: relative;
  margin-top: 10px;
  display: inline-block;
}

div.right {
  position: relative;
  float: right;
  width: 25%;
}

div.presentation {
  width: 70%;
  clear: both;
}

div.login, div.categories {
  width: 100%;
  clear: both;
}

div.login header, div.presentation header, div.categories header {
  font-size: 14px;
  background: #0a97e2;
  padding: 3px 15px;
  color: white;
  border-radius: 10px 10px 0 0; 
}

div.login section, div.presentation section, div.categories section {
  /*position: absolute;*/
  width: 100%;
  background: white;
  border-radius: 0 0 5px 5px;
}

div.login section p, div.presentation section p {
  /* changed this */
  margin: 0px 10px;
  padding: 3px 10px 3px 10px;
}

div.login section input {
  display: block;
  margin: auto;
  margin-bottom: 10px;
}

/* added this */
div.categories ul {
  margin: 0px 10px;
  padding: 3px 10px 3px 10px;
}

【讨论】:

  • 哦,所以我需要删除 position:absolute 并添加一个 margin-top: O,非常感谢它有效!
【解决方案2】:

HTML

<div class="left">
 <div class="presentation"></div>
</div>
<div class="right">
 <div class="login"></div>
 <div class="categories"></div>
</div>

CSS

.left { 
  float: left; 
}
.right { 
  float: right; 
}

【讨论】:

  • 即使没有 float:left 也可以工作,但现在我的登录和类别 div 是垂直重叠的!查看jsfiddle.net/7nJp9/1感谢您的帮助
  • 添加最小高度:130px;到 div.login 和玩具高度
  • 我相信将所有你想浮动的元素放在一个父元素的某个地方是一个面糊解决方案,即你的解决方案。其他技巧基本上是 hack,withc 最终会在某些时候破坏 CSS,你不知道从哪里开始。
【解决方案3】:
<div style="width:430px; height:430px; border:1px solid;">
    <div style="width:230px; height:100%; float:left; background:#000;"></div>
    <div style="width:180px; float:right; height:150px; border:1px solid;  margin-top:20px;  margin-right:10px;"></div>
    <div style="width:180px; float:right; height:150px; margin-top:20px; margin-right:10px; border:1px solid;"></div
</div>

试试这个

【讨论】:

    猜你喜欢
    • 2022-11-20
    • 2010-10-08
    • 1970-01-01
    • 2013-09-25
    • 2013-02-04
    • 1970-01-01
    • 2013-07-13
    • 2013-07-30
    • 1970-01-01
    相关资源
    最近更新 更多