【问题标题】:How Can I Do? CSS Flexbox Layout Question我能怎么做? CSS Flexbox 布局问题
【发布时间】:2020-10-04 18:27:07
【问题描述】:

https://i.stack.imgur.com/XUzsT.png

如何在不使用 div 的情况下使用 flex 以最少的代码制作这种结构?

我的html;

<header></header>
<nav></nav>
<main></main>
<footer></footer>

【问题讨论】:

  • 您是否有不想使用div的具体原因?
  • @ttoshiro 我对此很敏感。我真的不想用它。
  • @ttoshiro 示例:页脚不正确。 codepen.io/chriscoyier/pen/PoPwPbE

标签: html css layout flexbox frontend


【解决方案1】:

根据您在上面的 cmets 中给出的示例,您可以尝试这样的事情,它不使用 &lt;div&gt; 标签:

body {
  display: flex;
  flex-wrap: wrap;
}

* {
  text-align: center;
  vertical-align: middle;
}

.header {
  background-color: red;
  height: 100px;
  flex: 100%;
}

.sidebar {
  background-color: yellow;
  height: 200px;
  width: 25%;
}

.main {
  align-items: flex-end;
  flex-direction: column;
  width: 75%;
}

.article {
  background-color: green;
  height: 150px;
  text-align: center;
}

.footer {
  background-color: blue;
  height: 50px;
}
<header class="header">Header</header>
<aside class="sidebar">Nav</aside>
<main class="main">
  <article class="article">Content</article>
  <footer class="footer">Footer</footer>
</main>
</div>

坦率地说,使用&lt;div&gt; 元素来调用指定容器的类会更有条理。如果您想这样做,请尝试:

* {
  box-sizing: border-box;
}

.header {
  background-color: red;
  padding: 50px;
  text-align: center;
}

.display {
  display: flex;
  flex-wrap: flex;
  text-align: center;
}

.content {
  background-color: green;
  text-align: center;
  padding: 75px;
}

.nav {
  background-color: yellow;
  flex: 25%;
  text-align: center;
}

.footer {
  background-color: blue;
  padding: 10px;
}

.column {
  flex-direction: column;
  flex: 75%;
}

@media screen and (max-width: 700px) {
  .row, .navbar {   
    flex-direction: column;
  }
}
<div class="header">
  <h1>header</h1>
</div>

<div class="display">
  <div class="nav">
    <h1>Nav</h1>
  </div>
  <div class="column">
    <div class="content">
      <h1>Content</h1>
      <p>Context text</p>
    </div> 
    <div class="footer">
      <h1>Footer</h1>
      <p>Footer text</p>
    </div>
  </div>
</div>

【讨论】:

  • 感谢您的回答。你很善良。使用div不适合我的构造。
  • 很公平。我编辑了我的答案以添加一个仅使用一次 &lt;div&gt; 的示例——检查第一个示例!
  • 哦,对不起。我正在立即审查。
  • 不幸的是,我没有在正文中使用任何 div。
  • 我明白了。我删除了&lt;div&gt; 标签并用&lt;main&gt;&lt;article&gt; 标签替换了它。它的工作方式应该非常相似。再试第一个例子!
猜你喜欢
  • 2020-11-12
  • 2015-06-20
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多