23
19

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 5 years have passed since last update.

display:flexを使った横並びメニューの書き方

Last updated at Posted at 2018-03-18

はじめに

display:flexを使った横並びメニューの書き方をまとめました。

事前準備

ulタグにメニューの内容を記述し「list-style:none」を指定して縦に並べておきます。
見やすいようにデザインも少し整えておきます。

<ul>
    <li class="btn">menu1</li>
    <li class="btn">menu2</li>
    <li class="btn">menu3</li>
    <li class="btn">menu4</li>
    <li class="btn">menu5</li>
</ul>
/**list-styleを指定する**/
ul{    
    list-style: none;
}

/** 見た目を整える **/
body,ul,li{
    margin:0px;
    padding:0px;
}

.btn{    
    padding:10px 30px;
    margin:2.5px;

    background-color: rgba(135, 206, 235,1);
    border-radius: 5px;

    text-align: center;
    color:white;
}

この状態でのブラウザの表示は、こんな感じです。
image.png

display:flexを使って横並びメニューを作る。

display:flexを一行追加します。
これで横並びメニューの出来上がりです。

ul{
    list-style: none;
    display:flex;
}

image.png

左右の表示位置を変更する

justify-contentを指定することで左右の表示位置を変更できます。

左寄せ(デフォルト)

ul{
    list-style: none;
    display:flex;
    justify-content: flex-start; // 省略可
}

image.png

中央寄せ

ul{
    list-style: none;
    display:flex;
    justify-content: center;
}

image.png

右寄せ

ul{
    list-style: none;
    display:flex;
    justify-content: flex-end;
}

image.png

上下の表示位置を変更する

align-itemsを指定することで左右の表示位置を変更できます。
align-itemsは、高さを指定しないと上手く動かないので高さの指定を忘れないようにしましょう。

上寄せ

ul{    
    list-style: none;
    display:flex;

    height:70px;
    align-items: flex-start;

    background-color: rgba(211, 211, 211,0.3); // 分かりやすいように背景色を指定
}

image.png

中央寄せ

ul{    
    list-style: none;
    display:flex;

    height:70px;
    align-items: center;

    background-color: rgba(211, 211, 211,0.3); // 分かりやすいように背景色を指定
}

image.png

下寄せ

ul{    
    list-style: none;
    display:flex;

    height:70px;
    align-items: flex-end;

    background-color: rgba(211, 211, 211,0.3); // 分かりやすいように背景色を指定
}

image.png

以上です。

23
19
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
23
19

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?