HTML&CSS イチから作るヘッダーの作り方
ホームページのヘッダーの作り方を紹介します。
順番はHTMLでマークアップをした後にCSSで見た目を整え、最後にスマホ対応を行います。
関連記事
最終的なHTMLとCSS
HTML
<!DOCTYPE html>
<html lang="en" >
<head>
<meta charset="UTF-8">
<title>CodePen - header</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<header>
<p class="logo"><a class="over" href="#">COMPANY</a></p>
<ul class="navi">
<li><a href="/feature/">選ばれる理由</a></li>
<li><a href="/service/">サービス</a></li>
<li><a href="/case/">制作事例</a></li>
<li><a href="/company/">会社案内</a></li>
<li><a href="/recruit/">採用情報</a></li>
</ul>
<button class="sp-navi-toggle"><span class="bar"></span><span class="bar"></span><span class="bar"></span><span class="menu">MENU</span><span class="close">CLOSE</span></button>
</header>
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script>
<script src="js/script.js"></script>
</body>
</html>
CSS
/* header */
header {
width: 100%;
padding: 20px 40px;
background: #eee;
display: flex;
align-items: center;
box-sizing: border-box;
}
/* logo */
header .logo {
position: relative;
margin: 0;
padding: 0;
}
header .logo a {
font-weight: bold;
text-decoration: none;
color: #333;
}
@media screen and (max-width: 767px) {
header .logo {
padding: 0;
}
header .logo a {
}
}
/* scroll */
header {
transition: height 0.4s cubic-bezier(0.34, 0.615, 0.4, 0.985);
}
header.scroll {
height: 60px;
position: fixed;
z-index: 9999;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
header .logo {
transition: width 0.4s cubic-bezier(0.34, 0.615, 0.4, 0.985);
}
header.scroll .logo {
width: 70px;
}
/* header-navi */
header .navi {
margin: 0 0 0 auto;
padding: 0;
font-size: 14px;
font-weight: bold;
display: flex;
list-style: none;
}
header .navi li {
margin: 5px 0 5px 40px;
}
header .navi li:first-child {
margin-left: 0;
}
header .navi li a {
display: block;
box-sizing: border-box;
text-decoration: none;
color: #333;
}
header .navi li a:hover {
text-decoration: underline;
}
@media screen and (max-width: 767px) {
header .navi {
display: none;
}
}
/* open-button */
.sp-navi-toggle {
display: none;
margin: auto 0;
font-size: 10px;
font-weight: bold;
line-height: 1;
position: absolute;
top: 0;
bottom: 0;
right: 40px;
width: 16px;
height: 25px;
transition: all 0.4s;
color: #464646;
border: none;
outline: none;
background: none;
-webkit-appearance: unset;
}
.sp-navi-toggle .menu,
.sp-navi-toggle .close {
position: absolute;
bottom: 0;
left: -50%;
display: block;
width: 34px;
height: 11px;
}
.sp-navi-toggle .close {
display: none;
}
.sp-navi-toggle .bar {
position: absolute;
left: 0;
width: 100%;
height: 2px;
background-color: #464646;
}
.sp-navi-toggle .bar:nth-of-type(1) {
top: 0;
}
.sp-navi-toggle .bar:nth-of-type(2) {
top: 5px;
}
.sp-navi-toggle .bar:nth-of-type(3) {
top: 10px;
}
/* close-button */
html.sidebar-is-open .sp-navi-toggle .bar:nth-of-type(1) {
top: 5px;
transform: rotate(45deg);
}
html.sidebar-is-open .sp-navi-toggle .bar:nth-of-type(2) {
top: 5px;
transform: rotate(-45deg);
}
html.sidebar-is-open .sp-navi-toggle .bar:nth-of-type(3) {
display: none;
}
html.sidebar-is-open .sp-navi-toggle .menu {
display: none;
}
html.sidebar-is-open .sp-navi-toggle .close {
display: block;
}
@media screen and (max-width: 767px) {
.sp-navi-toggle{
display: block;
}
}
/* sp-navi */
.sp-navi-box {
display: none;
}
.sp-navi {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
list-style: none;
width: 100%;
height: 100vh;
font-size: 14px;
margin: 0;
padding: 0;
}
.sp-navi li {
padding: 20px 0;
}
.sp-navi li a {
display: block;
box-sizing: border-box;
text-decoration: none;
color: #333;
font-weight: bold;
}
.sp-navi li a:hover {
text-decoration: underline;
}
@media screen and (max-width: 767px) {
html.sidebar-is-open .sp-navi-box {
display: block;
}
html.sidebar-is-open {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: hidden;
width: 100%;
}
}
body {
margin: 0;
padding: 0;
}
1. ヘッダータグを入れる
ヘッダーの基礎となる<header>タグを入れます。
<header>
</header>
2. ロゴを入れる
ヘッダーの左側に挿入すロゴのタグを入れます。
<header>
<p class="logo"><a class="over" href="#">COMPANY</a></p>
</header>
3. ナビゲーションを入れる
ヘッダーの右側に挿入するナビゲーションのタグを入れます。
<header>
<p class="logo"><a class="over" href="#">COMPANY</a></p>
<ul class="navi">
<li><a href="/feature/">選ばれる理由</a></li>
<li><a href="/service/">サービス</a></li>
<li><a href="/case/">制作事例</a></li>
<li><a href="/company/">会社案内</a></li>
<li><a href="/recruit/">採用情報</a></li>
</ul>
</header>
4. CSSで見た目を整える
ロゴとナビゲーションを入れたらCSSで見た目を整えます。
header {
width: 100%;
padding: 20px 40px;
background: #eee;
display: flex;
align-items: center;
box-sizing: border-box;
}
/* logo */
header .logo {
position: relative;
margin: 0;
padding: 0;
}
header .logo a {
font-weight: bold;
text-decoration: none;
color: #333;
}
/* header-navi */
header .navi {
margin: 0 0 0 auto;
padding: 0;
font-size: 14px;
font-weight: bold;
display: flex;
list-style: none;
}
header .navi li {
margin: 5px 0 5px 40px;
}
header .navi li:first-child {
margin-left: 0;
}
header .navi li a {
display: block;
box-sizing: border-box;
text-decoration: none;
color: #333;
}
header .navi li a:hover {
text-decoration: underline;
}
5. スマホ対応をする
5-2. ハンバーガーメニューを入れる
<header>
<div class="inner">
<p class="logo"><a class="over" href="#">COMPANY</a></p>
<ul class="navi">
<li><a href="/feature/">選ばれる理由</a></li>
<li><a href="/service/">サービス</a></li>
<li><a href="/case/">制作事例</a></li>
<li><a href="/company/">会社案内</a></li>
<li><a href="/recruit/">採用情報</a></li>
</ul>
</div>
<button class="sp-navi-toggle"><span class="bar"></span><span class="bar"></span><span class="bar"></span><span class="menu">MENU</span><span class="close">CLOSE</span></button>
</header>
5-3. Javascriptを入れる
jQueryとスマホのメニューを表示するために、ページの下部にJavascriptを読み込みます。
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js'></script>
<script src="js/script.js"></script>
5-4. CSSで見た目を整える
最後にCSSで見た目を整えて完了です。
最終的なCSS以下になります。
/* header */
header {
width: 100%;
padding: 20px 40px;
background: #eee;
display: flex;
align-items: center;
box-sizing: border-box;
}
/* logo */
header .logo {
position: relative;
margin: 0;
padding: 0;
}
header .logo a {
font-weight: bold;
text-decoration: none;
color: #333;
}
@media screen and (max-width: 767px) {
header .logo {
padding: 0;
}
header .logo a {
}
}
/* scroll */
header {
transition: height 0.4s cubic-bezier(0.34, 0.615, 0.4, 0.985);
}
header.scroll {
height: 60px;
position: fixed;
z-index: 9999;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
header .logo {
transition: width 0.4s cubic-bezier(0.34, 0.615, 0.4, 0.985);
}
header.scroll .logo {
width: 70px;
}
/* header-navi */
header .navi {
margin: 0 0 0 auto;
padding: 0;
font-size: 14px;
font-weight: bold;
display: flex;
list-style: none;
}
header .navi li {
margin: 5px 0 5px 40px;
}
header .navi li:first-child {
margin-left: 0;
}
header .navi li a {
display: block;
box-sizing: border-box;
text-decoration: none;
color: #333;
}
header .navi li a:hover {
text-decoration: underline;
}
@media screen and (max-width: 767px) {
header .navi {
display: none;
}
}
/* open-button */
.sp-navi-toggle {
display: none;
margin: auto 0;
font-size: 10px;
font-weight: bold;
line-height: 1;
position: absolute;
top: 0;
bottom: 0;
right: 40px;
width: 16px;
height: 25px;
transition: all 0.4s;
color: #464646;
border: none;
outline: none;
background: none;
-webkit-appearance: unset;
}
.sp-navi-toggle .menu,
.sp-navi-toggle .close {
position: absolute;
bottom: 0;
left: -50%;
display: block;
width: 34px;
height: 11px;
}
.sp-navi-toggle .close {
display: none;
}
.sp-navi-toggle .bar {
position: absolute;
left: 0;
width: 100%;
height: 2px;
background-color: #464646;
}
.sp-navi-toggle .bar:nth-of-type(1) {
top: 0;
}
.sp-navi-toggle .bar:nth-of-type(2) {
top: 5px;
}
.sp-navi-toggle .bar:nth-of-type(3) {
top: 10px;
}
/* close-button */
html.sidebar-is-open .sp-navi-toggle .bar:nth-of-type(1) {
top: 5px;
transform: rotate(45deg);
}
html.sidebar-is-open .sp-navi-toggle .bar:nth-of-type(2) {
top: 5px;
transform: rotate(-45deg);
}
html.sidebar-is-open .sp-navi-toggle .bar:nth-of-type(3) {
display: none;
}
html.sidebar-is-open .sp-navi-toggle .menu {
display: none;
}
html.sidebar-is-open .sp-navi-toggle .close {
display: block;
}
@media screen and (max-width: 767px) {
.sp-navi-toggle{
display: block;
}
}
/* sp-navi */
.sp-navi-box {
display: none;
}
.sp-navi {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
list-style: none;
width: 100%;
height: 100vh;
font-size: 14px;
margin: 0;
padding: 0;
}
.sp-navi li {
padding: 20px 0;
}
.sp-navi li a {
display: block;
box-sizing: border-box;
text-decoration: none;
color: #333;
font-weight: bold;
}
.sp-navi li a:hover {
text-decoration: underline;
}
@media screen and (max-width: 767px) {
html.sidebar-is-open .sp-navi-box {
display: block;
}
html.sidebar-is-open {
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
overflow: hidden;
width: 100%;
}
}
body {
margin: 0;
padding: 0;
}