固定了带有响应式CSS网格布局的页眉或页脚

时间:2018-11-13 01:07:03

标签: html css css3 css-grid

我需要制作一个固定页脚的网页,并使用CSS Grid对页脚内的内容进行布局。

但是当我将position: fixed;应用于页脚时,列宽不再填充页面,而是改为最长的文本行的宽度。

我该如何进行这项工作?

https://codepen.io/simonhrogers/pen/pQNYjW

html, body {
  padding: 0;
  margin: 0;
}

img {
  width: 100%;
}

.image-grid {
  max-width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-column-gap: 2em;
  grid-row-gap: calc(2em - 0.22em);
  padding-left: 2em;
  padding-right: 2em;
  padding-top: 2em;
  padding-bottom: calc(2em - 0.22em);
}

.footer-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-column-gap: 2em;
  grid-row-gap: calc(2em - 0.22em);
  padding-top: 2em;
  padding-bottom: 2em;
  padding-left: 2em;
  padding-right: 2em;
  bottom: 0;
  background-color: green;
  opacity: 0.75;
}


li {
  list-style-type: none;
}

ul {
  padding: 0;
}

.fixed-footer-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-column-gap: 2em;
  grid-row-gap: calc(2em - 0.22em);
  padding-top: 2em;
  padding-bottom: 2em;
  padding-left: 2em;
  padding-right: 2em;
  bottom: 0;
  background-color: red;
  opacity: 0.75;
}

.fixed-footer-grid-container {
  position: fixed;
  bottom: 0;
}
<div class="image-grid">
  <div class="image">
    <img src="http://img2.storyblok.com/1400x0/filters:format(webp)/f/45723/2835x3543/6531101ff6/jr_0209_hb_aw18_campaign_mf_sp_4.jpg" alt="">
  </div>
    <div class="image">
    <img src="http://img2.storyblok.com/1400x0/filters:format(webp)/f/45723/2835x3543/6531101ff6/jr_0209_hb_aw18_campaign_mf_sp_4.jpg" alt="">
  </div>
    <div class="image">
    <img src="http://img2.storyblok.com/1400x0/filters:format(webp)/f/45723/2835x3543/6531101ff6/jr_0209_hb_aw18_campaign_mf_sp_4.jpg" alt="">
  </div>
    <div class="image">
    <img src="http://img2.storyblok.com/1400x0/filters:format(webp)/f/45723/2835x3543/6531101ff6/jr_0209_hb_aw18_campaign_mf_sp_4.jpg" alt="">
  </div>
    <div class="image">
    <img src="http://img2.storyblok.com/1400x0/filters:format(webp)/f/45723/2835x3543/6531101ff6/jr_0209_hb_aw18_campaign_mf_sp_4.jpg" alt="">
  </div>
    <div class="image">
    <img src="http://img2.storyblok.com/1400x0/filters:format(webp)/f/45723/2835x3543/6531101ff6/jr_0209_hb_aw18_campaign_mf_sp_4.jpg" alt="">
  </div>
</div>

<div class="footer-grid">
    <div class="credits">
      <ul>
        <li>Correct Column Layout</li>
      </ul>
    </div>
    <div class="credits">
      <ul>
        <li>Correct Column Layout</li>
        <li>Correct Column Layout</li>
        <li>Correct Column Layout</li>
        <li>Correct Column Layout</li>
        <li>Correct Column Layout</li>
      </ul>
    </div>
    <div class="credits right">
      <ul>
        <li>But Needs to be Fixed Position at Bottom</li>
      </ul>
    </div>
</div>

<div class="fixed-footer-grid-container">
  <div class="fixed-footer-grid">
    <div class="credits">
      <ul>
        <li>Incorrect Column Width</li>
      </ul>
    </div>
    <div class="credits">
      <ul>
        <li>Incorrect Column Width</li>
        <li>Incorrect Column Width</li>
        <li>Incorrect Column Width</li>
        <li>Incorrect Column Width</li>
        <li>Incorrect Column Width</li>
      </ul>
    </div>
    <div class="credits right">
      <ul>
        <li>Needs to be 100% screen width</li>
      </ul>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

由于固定位置元素已从文档流中移除,因此它不会像流入的块元素一样自动展开全宽。您需要指定其尺寸。

将此添加到您的代码中:

.fixed-footer-grid-container {
    position: fixed;
    bottom: 0;
    width: 100%; /* new */
}

revised codepen