如何让我的主要内容在页眉和页脚之后占据剩余的空间?

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

How do I get my main content to take up the rest of the space left over after the header and footer?

问题

I'm working through The Odin Project and I'm having trouble making my main content take up the rest of the space of the browser.

Right now it looks like this:

如何让我的主要内容在页眉和页脚之后占据剩余的空间?

The 1px solid red border is as far as the main content goes. I have tried this but it's not allowing for a fixed header and footer. I have also tried some other flex solutions. Those are commented out in the code.

Am I just doing this whole thing wrong? Is there a standard way that I don't know about?

index.html:

<body>
    <div class="header">
        <h1>
            MY AWESOME WEBSITE
        </h1>
    </div>

    <div class="main-content">
        <div class="sidebar">
            <ul>
                <li><a href="#">⭐ - link one</a></li>
                <li><a href="#">🏈‍♂️ - link two</a></li>
                <li><a href="#">🌌️ - link three</a></li>
                <li><a href="#">🌼🏝 - link four</a></li>
            </ul>
        </div>
        <div class="content">
            <div class="card">Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora, eveniet? Dolorem
                dignissimos
                maiores non delectus possimus dolor nulla repudiandae vitae provident quae, obcaecati ipsam unde impedit
                corrupti veritatis minima porro?</div>
            <div class="card">Lorem ipsum dolor sit amet consectetur adipisicing elit. Quasi quaerat qui iure ipsam
                maiores
                velit tempora, deleniti nesciunt fuga suscipit alias vero rem, corporis officia totam saepe excepturi
                odit
                ea.
            </div>
            <div class="card">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi
                eligendi
                aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia
                esse
                autem?</div>
            <div class="card">Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius
                amet
                adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum
                minima
                laboriosam eos!</div>
            <div class="card">Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi
                eligendi
                aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia
                esse
                autem?</div>
            <div class="card">Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius
                amet
                adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum
                minima
                laboriosam eos!</div>
        </div>
    </div>

    <div class="footer">
        The Odin Project ❤️
    </div>
</body>

style-07.css:

:root{
    --header-height: 72px;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    margin: 0;
    min-height: 100vh;
    height: 100%;
}

.main-content{
    display: flex;
    height: 100%;
    padding-top: var(--header-height); 
    flex-direction: row;
    border: 1px solid red;
}

.sidebar{
    flex-shrink: 0;
}

.content {
    padding: 32px;
    display: flex;
    flex-wrap: wrap;
}

.card {
    width: 300px;
    padding: 16px;
    margin: 16px;
}

.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    height: var(--header-height);
    background: darkmagenta;
    color: white;
    padding: 0px 15px;
}

h1 {
    font-weight: 1000;
}

.footer {
    height: var(--header-height);
    background: #eee;
    color: darkmagenta;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 5%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.sidebar {
    width: 300px;
    background: royalblue;
    box-sizing: border-box;
    padding: 16px;
}

.card {
    border: 1px solid #eee;
    box-shadow: 2px 4px 16px rgba(0, 0, 0, .06);
    border-radius: 4px;
}

ul{
    list-style-type: none;
    margin: 0;
    padding: 0;
}

a {
    text-decoration: none;
    color: white;
    font-size: 24px;
}

li{
    margin-bottom: 16px;
}
英文:

I'm working through The Odin Project and I'm having trouble making my main content take up the rest of the space of the browser.

Right now it looks like this:

如何让我的主要内容在页眉和页脚之后占据剩余的空间?

The 1px solid red border is as far as the main content goes. I have tried this but it's not allowing for a fixed header and footer. I have also tried some other flex solutions. Those are commented out in the code.

Am I just doing this whole thing wrong? Is there a standard way that I don't know about?

index.html:

&lt;body&gt;
    &lt;div class=&quot;header&quot;&gt;
        &lt;h1&gt;
            MY AWESOME WEBSITE
        &lt;/h1&gt;
    &lt;/div&gt;

    &lt;div class=&quot;main-content&quot;&gt;
        &lt;div class=&quot;sidebar&quot;&gt;
            &lt;ul&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;⭐ - link one&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#129464;&#127997;‍♂️ - link two&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#128396;️ - link three&lt;/a&gt;&lt;/li&gt;
                &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#128076;&#127997; - link four&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
        &lt;/div&gt;
        &lt;div class=&quot;content&quot;&gt;
            &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora, eveniet? Dolorem
                dignissimos
                maiores non delectus possimus dolor nulla repudiandae vitae provident quae, obcaecati ipsam unde impedit
                corrupti veritatis minima porro?&lt;/div&gt;
            &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Quasi quaerat qui iure ipsam
                maiores
                velit tempora, deleniti nesciunt fuga suscipit alias vero rem, corporis officia totam saepe excepturi
                odit
                ea.
            &lt;/div&gt;
            &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi
                eligendi
                aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia
                esse
                autem?&lt;/div&gt;
            &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius
                amet
                adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum
                minima
                laboriosam eos!&lt;/div&gt;
            &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi
                eligendi
                aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia
                esse
                autem?&lt;/div&gt;
            &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius
                amet
                adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum
                minima
                laboriosam eos!&lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;footer&quot;&gt;
        The Odin Project ❤️
    &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

style-07.css:

:root{
    --header-height: 72px;
}
body {
    font-family: -apple-system, BlinkMacSystemFont, &#39;Segoe UI&#39;, Roboto, Oxygen, Ubuntu, Cantarell, &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, sans-serif;
    margin: 0;
    min-height: 100vh;
    height: 100%;
}

.main-content{
    display: flex;
    height: 100%; /* If I use px units it will force the main content to go down but I know that is not ideal. */
    padding-top: var(--header-height); 
    flex-direction: row;
    border: 1px solid red;
    /* Things I have tried from other answers*/
    /* flex: 1 1 auto; */
    /* height: calc(100% - var(--header-height)); */
}

.sidebar{
    flex-shrink: 0;
}

.content {
    padding: 32px;
    display: flex;
    flex-wrap: wrap;
}

.card {
    width: 300px;
    padding: 16px;
    margin: 16px;
}

.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    display: flex;
    align-items: center;
    height: var(--header-height);
    background: darkmagenta;
    color: white;
    padding: 0px 15px;
}

h1 {
    font-weight: 1000;
}

.footer {
    height: var(--header-height);
    background: #eee;
    color: darkmagenta;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 5%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.sidebar {
    width: 300px;
    background: royalblue;
    box-sizing: border-box;
    padding: 16px;
}

.card {
    border: 1px solid #eee;
    box-shadow: 2px 4px 16px rgba(0, 0, 0, .06);
    border-radius: 4px;
}

ul{
    list-style-type: none;
    margin: 0;
    padding: 0;
}

a {
    text-decoration: none;
    color: white;
    font-size: 24px;
}

li{
    margin-bottom: 16px;
}

答案1

得分: 1

您可以在body上使用flex布局,而不是在headerfooter上使用fixed,并将bodyflex-direction设置为column,然后对于main-content,只需设置flex: 1并移除顶部内边距,flex: 1将确保main-content占据父容器中的剩余空间。将body的高度设置为height: 100vh,并添加overflow: hidden,对于main-content,设置overflow: auto

此外,要使sidebar在滚动时保持粘性,我在main-content上添加了position: relative;,在sidebar上添加了position: sticky;

为了强制设置headerfooter的高度并防止它们被flex布局挤压,使用min-height而不是height,就像我在代码中修改的那样。

如果有更多问题,请留言。

<body>
  <div class="header">
    <h1>
      我的精彩网站
    </h1>
  </div>

  <div class="main-content">
    <div class="sidebar">
      <ul>
        <li><a href="#">⭐ - 链接一</a></li>
        <li><a href="#">🧔‍♂️ - 链接二</a></li>
        <li><a href="#">🎖️ - 链接三</a></li>
        <li><a href="#">🐶🧔 - 链接四</a></li>
      </ul>
    </div>
    <div class="content">
      <!-- 此处是您的内容卡片 -->
    </div>
  </div>

  <div class="footer">
    The Odin Project ❤️
  </div>
</body>
:root {
  --header-height: 72px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
  margin: 0;
  height: 100vh;
  overflow: hidden;
  
  display: flex; 
  flex-direction: column;
}

.main-content {
  flex: 1;
  display: flex;
  overflow-y: auto;
  /* 如果使用像素单位,将会迫使主内容向下移动,但我知道这不是理想的。 */
  flex-direction: row;
  border: 1px solid red;
  /* 我从其他答案中尝试过的方法 */
  /* flex: 1 1 auto; */
  /* height: calc(100% - var(--header-height)); */
  
  position: relative;
}

/* 其他样式不变 */

这是根据您提供的代码进行的翻译。如果有其他问题,请留言。

英文:

You can use flex diplay on body instead of using instead of fixed on header and footer and make the body display flex with column direction, then for main-content all you need is to set flex: 1 and remove padding top, flex: 1 will make sure that main-content take any remaining space in the parent. Set the body height to height: 100vh and overflow: hidden, for man-content, set overflow: auto.

Additionally, To make sidebar sticky when scrolling, I added position: relative; to main-content and position: sticky; to the sidebar.

To force header and footer heights and prevent them to be squeezed by the flex position, use min-height instead of height as I modified in the code.

Try to view the run code in full page, if you have any further questions, comment below.

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

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

:root {
  --header-height: 72px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, &#39;Segoe UI&#39;, Roboto, Oxygen, Ubuntu, Cantarell, &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, sans-serif;
  margin: 0;
  height: 100vh;
  overflow:hidden;
  
  display: flex; 
  flex-direction: column;
}

.main-content {
  flex: 1;
  display: flex;
  overflow-y: auto;
  /* If I use px units it will force the main content to go down but I know that is not ideal. */
  flex-direction: row;
  border: 1px solid red;
  /* Things I have tried from other answers*/
  /* flex: 1 1 auto; */
  /* height: calc(100% - var(--header-height)); */
  
  position: relative;
}



.content {
  padding: 32px;
  display: flex;
  flex-wrap: wrap;
}

.card {
  width: 300px;
  padding: 16px;
  margin: 16px;
}

.header {

 
  display: flex;
  align-items: center;
  min-height: var(--header-height);
  background: darkmagenta;
  color: white;
  padding: 0px 15px;
}

h1 {
  font-weight: 1000;
}

.footer {
  min-height: var(--header-height);
  background: #eee;
  color: darkmagenta;
 
  width: 100%;
  height: 5%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.sidebar {
  width: 300px;
  background: royalblue;
  box-sizing: border-box;
  padding: 16px;
  
  position: sticky;
  top: 0;
  
  white-space: nowrap;
  min-height: 250px;
}

.card {
  border: 1px solid #eee;
  box-shadow: 2px 4px 16px rgba(0, 0, 0, .06);
  border-radius: 4px;
}

ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
}

a {
  text-decoration: none;
  color: white;
  font-size: 24px;
}

li {
  margin-bottom: 16px;
}

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

&lt;body&gt;
  &lt;div class=&quot;header&quot;&gt;
    &lt;h1&gt;
      MY AWESOME WEBSITE
    &lt;/h1&gt;
  &lt;/div&gt;

  &lt;div class=&quot;main-content&quot;&gt;
    &lt;div class=&quot;sidebar&quot;&gt;
      &lt;ul&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;⭐ - link one&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#129464;&#127997;‍♂️ - link two&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#128396;️ - link three&lt;/a&gt;&lt;/li&gt;
        &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#128076;&#127997; - link four&lt;/a&gt;&lt;/li&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    &lt;div class=&quot;content&quot;&gt;
      &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora, eveniet? Dolorem dignissimos maiores non delectus possimus dolor nulla repudiandae vitae provident quae, obcaecati ipsam unde impedit corrupti veritatis minima porro?&lt;/div&gt;
      &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Quasi quaerat qui iure ipsam maiores velit tempora, deleniti nesciunt fuga suscipit alias vero rem, corporis officia totam saepe excepturi odit ea.
      &lt;/div&gt;
      &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi eligendi aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia esse autem?
      &lt;/div&gt;
      &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius amet adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum minima laboriosam eos!&lt;/div&gt;
      &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi eligendi aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia esse autem?
      &lt;/div&gt;
      &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius amet adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum minima laboriosam eos!&lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;

  &lt;div class=&quot;footer&quot;&gt;
    The Odin Project ❤️
  &lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;

<!-- end snippet -->

答案2

得分: 1

:root{
    --header-height: 72px;
}
body {
    height: 100vh;/* add this */
}

.main-content{
    height: calc(100% - var(--header-height) - 5%); /* add this */
}
英文:

Everything you have done is okay, just change the body height to 100vh and main-content height calculation height: calc(100% - var(--header-height) - 5%);

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

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

:root{
        --header-height: 72px;
    }
    body {
        font-family: -apple-system, BlinkMacSystemFont, &#39;Segoe UI&#39;, Roboto, Oxygen, Ubuntu, Cantarell, &#39;Open Sans&#39;, &#39;Helvetica Neue&#39;, sans-serif;
        margin: 0;
        height: 100vh;/* add this */
    }
    
    .main-content{
        display: flex;
        height: calc(100% - var(--header-height) - 5%); /* add this */
        padding-top: var(--header-height); 
        flex-direction: row;
        border: 1px solid red;
    }
    
    .sidebar{
        flex-shrink: 0;
    }
    
    .content {
        padding: 32px;
        display: flex;
        flex-wrap: wrap;
    }
    
    .card {
        width: 300px;
        padding: 16px;
        margin: 16px;
    }
    
    .header {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        display: flex;
        align-items: center;
        height: var(--header-height);
        background: darkmagenta;
        color: white;
        padding: 0px 15px;
    }
    
    h1 {
        font-weight: 1000;
    }
    
    .footer {
        height: var(--header-height);
        background: #eee;
        color: darkmagenta;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 5%;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    
    .sidebar {
        width: 300px;
        background: royalblue;
        box-sizing: border-box;
        padding: 16px;
    }
    
    .card {
        border: 1px solid #eee;
        box-shadow: 2px 4px 16px rgba(0, 0, 0, .06);
        border-radius: 4px;
    }
    
    ul{
        list-style-type: none;
        margin: 0;
        padding: 0;
    }
    
    a {
        text-decoration: none;
        color: white;
        font-size: 24px;
    }
    
    li{
        margin-bottom: 16px;
    }

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

&lt;div class=&quot;header&quot;&gt;
            &lt;h1&gt;
                MY AWESOME WEBSITE
            &lt;/h1&gt;
        &lt;/div&gt;
    
        &lt;div class=&quot;main-content&quot;&gt;
            &lt;div class=&quot;sidebar&quot;&gt;
                &lt;ul&gt;
                    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;⭐ - link one&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#129464;&#127997;‍♂️ - link two&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#128396;️ - link three&lt;/a&gt;&lt;/li&gt;
                    &lt;li&gt;&lt;a href=&quot;#&quot;&gt;&#128076;&#127997; - link four&lt;/a&gt;&lt;/li&gt;
                &lt;/ul&gt;
            &lt;/div&gt;
            &lt;div class=&quot;content&quot;&gt;
                &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempora, eveniet? Dolorem
                    dignissimos
                    maiores non delectus possimus dolor nulla repudiandae vitae provident quae, obcaecati ipsam unde impedit
                    corrupti veritatis minima porro?&lt;/div&gt;
                &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Quasi quaerat qui iure ipsam
                    maiores
                    velit tempora, deleniti nesciunt fuga suscipit alias vero rem, corporis officia totam saepe excepturi
                    odit
                    ea.
                &lt;/div&gt;
                &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi
                    eligendi
                    aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia
                    esse
                    autem?&lt;/div&gt;
                &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius
                    amet
                    adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum
                    minima
                    laboriosam eos!&lt;/div&gt;
                &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur, adipisicing elit. Nobis illo ex quas, commodi
                    eligendi
                    aliquam ut, dolor, atque aliquid iure nulla. Laudantium optio accusantium quaerat fugiat, natus officia
                    esse
                    autem?&lt;/div&gt;
                &lt;div class=&quot;card&quot;&gt;Lorem ipsum dolor sit amet consectetur adipisicing elit. Necessitatibus nihil impedit eius
                    amet
                    adipisci dolorum vel nostrum sit excepturi corporis tenetur cum, dolore incidunt blanditiis. Unde earum
                    minima
                    laboriosam eos!&lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    
        &lt;div class=&quot;footer&quot;&gt;
            The Odin Project ❤️
        &lt;/div&gt;

<!-- end snippet -->

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

发表评论

匿名网友

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

确定