【问题标题】:Css fluid layout with two columns [duplicate]具有两列的CSS流体布局[重复]
【发布时间】:2017-06-27 15:02:10
【问题描述】:

我需要这样布局:

----------------------
|   header           |
----------------------
|  N  |              |
|  A  |   CONTENT    |
|  V  |              |
|-----|              |
|     | ----BAR----  |
|EMPTY|              |
|     |              |
----------------------

我希望整体宽度是身体的 100%, 导航的宽度为 15%,但最小宽度为 120px。 条的宽度(即内容 div 中的一个元素)必须是内容 div 的 100%。

在 html 中,我有导航 div 必须位于内容 div 之前的限制。

编辑: 我在 html 中的代码是

<div id="main">
<div id="header"></div>
<div id="nav"></div>
<div id="content><p id="bar">Title of paragraph</p></div>
</div>

我现在在css中的代码是:

#nav {
    float: left;
    width: 15%;
    min-width: 120px;
    }
#content{
    float: right;
}
#bar {
display: block;
background-color: #D2DEE4;
}

你能帮帮我吗?

【问题讨论】:

  • 你需要分享你的代码..你试过什么?
  • 这个布局不新颖;只需搜索您的标题。
  • 记得接受/投票。
  • @Albert,为一个相当简单的问题添加多个答案几乎没有帮助,可能会被视为垃圾邮件。从外面看,您似乎是在争取选票,而不是试图提供帮助。如果你把你的答案分成一个,你会解释每一种技术,它的优点和缺点会更好。
  • @AndreiGheorghiu 所以应该在一篇文章中回答多个答案?

标签: html css fluid-layout


【解决方案1】:

此方法是静态的,不允许使用百分比(对于其他屏幕尺寸,您必须使用媒体查询。)

*{
  margin: 0;
  padding: 0;
}

header{
  padding: 10px;
  width: 100%;
  background: #888;
}

.sidebar{
  box-sizing: border-box;
  float:left; 
  background: #06f;
  display:block;
  width: 120px;
  padding: 10px;
}

.content{
  box-sizing: border-box;
  padding: 10px;
  display:block;
  background:#ddd;
  margin-left: 120px;
}
<header>This is the header</header>
<div class="sidebar">
   This is the sidebar
</div>
<div class="content">
  This is the content
  <br>
  test
  <br>
  test
  <br>
  test
</div>

【讨论】:

  • 请添加一些解释,说明此代码为何/如何帮助 OP。这将有助于为未来的观众提供一个可以从中学习的答案。请参阅this Meta question and its answers 了解更多信息。
【解决方案2】:

我更喜欢 flexbox 方法,但请记住,这不适用于 IE9 及以下版本。

Flex 基础(又名flex: {shrink} {grow} {flex-basis})是必需的,因为 flex 不适用于 css 宽度。

*{
  margin: 0;
  padding: 0;
}

header{
  padding: 10px;
  width: 100%;
  background: #888;
}

.sidebar{
  padding: 10px;
  flex: 0 0 120px;
  background: #07f;
}

.content{
  padding: 10px;
  background: #ddd;
  width: 100%;
}

.container{
  display: flex;
  flex-direction: row;
}
<header>
  This is the header
</header>

<div class="container">
  <div class="sidebar">
     This is the sidebar
  </div>

  <div class="content">
    This is the content
    <br>
    test
  </div>
</div>

【讨论】:

  • 这对我有用,但使用 flex-direction: column;而不是 flex-direction: row;!
  • 奇怪哈哈
  • 实际上,可以为flexbox 提供不错的后备,使其显示与IE8 一样低。这实际上取决于您从flexbox 使用的确切内容。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-04
  • 1970-01-01
  • 2012-05-01
  • 2015-11-05
  • 1970-01-01
相关资源
最近更新 更多