0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

備忘録として残します。
もっと良い書き方があれば指摘していただけると幸いです。

ヘッダーはページ最上部、フッターはページ最下部に来るようにコンポーネントを作成していきます。

コード

LayoutWrapper.tsx
"use client";

import styled from "styled-components";

const LayoutWrapper = styled.div`
  width: 100%;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
`;

export default LayoutWrapper;
Footer.tsx
"use client";

import styled from "styled-components";

const FooterComponent = styled.footer`
  margin-top: auto;
  padding: 15px 0;
  text-align: center;
  color: #fff;
  background: #000;
`;

const FooterText = styled.p`
  font-size: 16px;
`;

const Footer = () => {
  return (
    <FooterComponent>
      <FooterText>Sample Page Author: SBS_takumi</FooterText>
    </FooterComponent>
  );
};

export default Footer;
layout.tsx
(中略)
<LayoutWrapper>
 <Header/> // 任意のHeaderコンポーネントでよいです。
 {children}
 <Footer/>
</LayoutWrapper>

説明

LayoutWrapperについて、min-height: 100%にすることで、childrenの内容が少ない場合でも画面いっぱいに要素が表示されるようになります。

display: flexflex-direction: columnとすることで、LayoutWrapper内の要素が
列(縦)方向に並ぶようになります。
これを利用して、ヘッダー、フッター、childrenを縦方向に並べることができます。

上記の設定を行ったうえでFooterComponentについて、margin-top: autoと設定することでページ最下部にフッターを固定することができます。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?