CSS ページネーションの例

❮ 前章へ 次章へ ❯

CSS を使用したレスポンシブなページネーションを作成する方法を学習します。


簡単なページネーション

ページ数の多い web サイトを持っている場合、各ページにある種のページネーションが欲しいのではないでしょうか:

ページネーションを作るには、HTML のリストをスタイルします:

ul.pagination {
    display: inline-block;
    padding: 0;
    margin: 0;
}

ul.pagination li {display: inline;}

ul.pagination li a {
    color: black;
    float: left;
    padding: 8px 16px;
    text-decoration: none;
}
Try it Yourself ❯

Active and Hoverable ページネーション

.active クラスで現在のページをハイライト表示し、各ページのリンクの上にマウスを移動したとき、その色を変更するために、 :hover セレクタを使用します:

ul.pagination li a.active {
    background-color: #4CAF50;
    color: white;
}

ul.pagination li a:hover:not(.active) {background-color: #ddd;}
Try it Yourself ❯

角丸の Active および Hoverable ボタン

"active" および "hover" ボタンの角を丸めたい場合は、border-radius プロパティを追加します:

ul.pagination li a {
    border-radius: 5px;
}

ul.pagination li a.active {
    border-radius: 5px;
}
Try it Yourself ❯

Hoverable Transition Effect

ホバー時に、トランジション効果を作成するために、ページリンクに transition プロパティを追加します:

ul.pagination li a {
    transition: background-color .3s;
}
Try it Yourself ❯

ボーダー付きページネーション

ページネーションにボーダーを追加するには、border プロパティを使用します:

ul.pagination li a {
    border: 1px solid #ddd; /* Gray */
}
Try it Yourself ❯

角丸のボーダー

チップ: ページネーションの最初と最後のリンクに角丸のボーダーを追加します:

.pagination li:first-child a {
    border-top-left-radius: 5px;
    border-bottom-left-radius: 5px;
}

.pagination li:last-child a {
    border-top-right-radius: 5px;
    border-bottom-right-radius: 5px;
}
Try it Yourself ❯

リンク間のスペース

チップ: ページ・リンクのグループにしたくない場合は、margin プロパティを追加します:

ul.pagination li a {
    margin: 0 4px; /* 0 is for top and bottom. Feel free to change it */
}
Try it Yourself ❯

ページネーションのサイズ

font-size プロパティでページネーションのサイズを変更します:

ul.pagination li a {
    font-size: 22px;
}
Try it Yourself ❯

ページネーションの中央揃え

ページネーションを中央にするには、周りを (<div> のような) コンテナ要素でラップし、text-align:center を追加します。

div.center {
    text-align: center;
}
Try it Yourself ❯

その他の例

Try it Yourself ❯

パンくずリスト

もう一つのページネーションのバリエーションは、いわゆる「パンくずリスト」と呼ばれるものです:

ul.breadcrumb {
    padding: 8px 16px;
    list-style: none;
    background-color: #eee;
}

ul.breadcrumb li {display: inline;}

ul.breadcrumb li+li:before {
    padding: 8px;
    color: black;
    content: "/\00a0";
}
Try it Yourself ❯

❮ 前章へ 次章へ ❯