CSS – 如何使用 flexbox 来适应项目

huangapple go评论51阅读模式
英文:

CSS - How to fit the item using the flexbox

问题

您可以在此图片中看到我的代码输出。我希望HeaderContent能够垂直排列并紧密相连。这应该在“Menu”标签在它们之间(#header、#content)的中间执行,但在外观上紧邻它。

#app {
    position: relative;
    display: flex;
    width: 100%;
    background-color: #eee;
    border-radius: 10px;
    height: 100%;
    padding: 2%;
    gap: 2%;
    flex-wrap: wrap;
}

#header, #menu, #content {
    position: relative;
    width: 40%;
    min-width: 300px;
    height: 300px;
    background-color: #333;
    border-radius: 10px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
}

#menu {
    height: 100%;
}
<div id="app">
    <div id="header">HEADER</div>
    <div id="menu">MENU</div>
    <div id="content">CONTENT</div>
</div>
英文:

You can see my codes output in this image.
I want the Header and Content to be placed under each other and stick together. This should be executed while the 'Menu' tag is in the middle them (#header, #content), but next to it in appearance.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#app {
    position: relative;
    display: flex;
    width: 100%;
    background-color: #eee;
    border-radius: 10px;
    height: 100%;
    padding: 2%;
    gap: 2%;
    flex-wrap: wrap;
}

#header, #menu, #content {
    position: relative;
    width: 40%;
    min-width: 300px;
    height: 300px;
    background-color: #333;
    border-radius: 10px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
}

#menu {
    height: 100%;
}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
        &lt;div id=&quot;header&quot;&gt;HEADER&lt;/div&gt;
        &lt;div id=&quot;menu&quot;&gt;MENU&lt;/div&gt;
        &lt;div id=&quot;content&quot;&gt;CONTENT&lt;/div&gt;
 &lt;/div&gt;

<!-- end snippet -->

答案1

得分: 1

我认为对于这个情况,grid 会是一个更好的选择。

英文:

I think grid would be a better choice for this.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#app {
  /*position: relative;*/
  /*display: flex;*/
  background-color: #eee;
  border-radius: 10px;
  /*height: 100%;
    width: 100%;*/
  padding: 1rem;
  grid-gap: 1rem;
  /*flex-wrap: wrap;*/
  
  display: grid;
  grid-template: 
  &quot;header menu&quot; 1fr /* [grid area] [grid area] [row height] */
  &quot;content menu&quot; 1fr /* [grid area] [grid area] [row height] */
  / 3fr 1fr;  /* [left column width] [right column width] */
 
}

#header,
#menu,
#content {
  /*position: relative;*/
  /*width: 40%;*/
  /*min-width: 300px;*/
  /*height: 300px;*/
  background-color: #333;
  border-radius: 10px;
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  
  flex-direction: column;
}

#header {
  grid-area: header;
  max-height: 300px;
}

#menu {
  grid-area: menu;
}

#content {
  grid-area: content;
}

<!-- language: lang-html -->

&lt;div id=&quot;app&quot;&gt;
  &lt;div id=&quot;header&quot;&gt;
    &lt;h1&gt;HEADER&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div id=&quot;menu&quot;&gt;
    &lt;h1&gt;MENU&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div id=&quot;content&quot;&gt;
    &lt;h1&gt;CONTENT&lt;/h1&gt;
  &lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月6日 23:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189837.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定