# 実務でよく使うCSSプロパティ

`html_and_css`ディレクトリに、`part6`ディレクトリを作り、以下のようにディクトリとファイルを作成しましょう。normalize.cssは[ノーマライズCSS](https://ryos-organization-1.gitbook.io/happiness-chain/html-css/nmaraizucss)のセクションで紹介したものをダウンロードして格納しましょう。

```
part6/
    css/
        normalize.css
        styles.css
    index.html
```

<pre class="language-html" data-title="index.html◎"><code class="lang-html"><strong>&#x3C;!DOCTYPE html>
</strong><strong>&#x3C;html lang="en">
</strong><strong>  &#x3C;head>
</strong><strong>    &#x3C;meta charset="UTF-8" />
</strong><strong>    &#x3C;meta name="viewport" content="width=device-width, initial-scale=1.0" />
</strong><strong>    &#x3C;link rel="stylesheet" href="css/normalize.css" />
</strong><strong>    &#x3C;link rel="stylesheet" href="css/styles.css" />
</strong><strong>    &#x3C;title>Document&#x3C;/title>
</strong><strong>  &#x3C;/head>
</strong><strong>  &#x3C;body>&#x3C;/body>
</strong><strong>&#x3C;/html>
</strong></code></pre>

このセクションでは、以下のプロパティを紹介します。

* [color、background-color](#color-background-color)
* [border](#border)
* [padding、maring](#padding-margin)
* [box-shadow](#box-shadow)
* [width、height](#width-height)
* [font-size、font-weight、text-align](#font-size-font-weight-text-align)
* [display](#display)

## color、background-color

**color**は文字色、**background-color**は背景色のプロパティです。

divタグを追加して、cssを当てましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">&#x3C;!DOCTYPE html>
&#x3C;html lang="en">
  &#x3C;head>
    &#x3C;meta charset="UTF-8" />
    &#x3C;meta name="viewport" content="width=device-width, initial-scale=1.0" />
    &#x3C;link rel="stylesheet" href="css/normalize.css" />
    &#x3C;link rel="stylesheet" href="css/styles.css" />
    &#x3C;title>Document&#x3C;/title>
  &#x3C;/head>
  &#x3C;body>
<strong>    &#x3C;div class="color-box">
</strong><strong>      &#x3C;p>colorとbackground-color&#x3C;/p>
</strong><strong>    &#x3C;/div>
</strong>  &#x3C;/body>
&#x3C;/html>
</code></pre>

<pre class="language-css" data-title="styles.css◎"><code class="lang-css"><strong>.color-box {
</strong><strong>    color: blue;
</strong><strong>    background-color: yellow;
</strong><strong>}
</strong></code></pre>

ブラウザで確認すると、以下のように文字色と、背景色が指定した色に変化するかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2F0IFgGkUBZ927xI85vub3%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2014.06.06.png?alt=media&#x26;token=c9d57d2d-7fad-4423-ada3-0316f50f1a16" alt=""><figcaption></figcaption></figure>

色の指定の仕方に関してですが、以下の4種類の指定方法があります。

* キーワード
* RGB
* 16進数カラーコード
* HSL

#### キーワード

上記で記載したように、`blue` `yellow`のように固有名詞で指定する方法です。代表的な色に関しては、キーワードで指定することができます。

#### RGB

**Red**、**Green**、**Blue**の頭文字で、各色の度合いを256段階で指定するもので、`rgb(12, 25, 125)`のように記載すれば、Redが12、Greenが25、Blueが125の色を混ぜた色になります。

ちなみに`rgb(0, 0, 0)`が黒、`rgb(255, 255, 255)`が白になります。

#### 16進数カラーコード

こちらは、上記のRGBの概念を16進数で表したもので、例えば`rgb(12, 25, 125)`を例にすると、

Redの`12`を16進数で表すと`c`になります。

(16進数については、[こちら](https://wa3.i-3-i.info/word1610.html)の記事が参考になるかと思います。)

同様にGreenの`25`は`19`、Blueの`125`は`7d`になります。

(10進数の255までの数字は、16進数で2桁で表見することができます。)

この数字をRGBの順で、各値を0埋め2桁で表現し、前方にシャープをつけて

`#0c197d`という文字列になります。

ちなみに黒は`#000000`、白は`#ffffff`となります。

#### HSL

こちらはHue(色相)、Saturation(彩度)、Lightness(明度)の３つを指定するもので、

色相は0°〜360°、彩度、明度は0%\~100%で表現するので、`hsl(125°, 50%, 25%)`のように記述します。

こちらに関しては、詳細な説明は省略します。

4種類の指定方法を説明しましたが、実務ではよく16進数カラーコードで記載することが多いかと思います。

カラーコードはブラウザで「color picker」と検索して、表現したい色のカラーコードを取得することができます。

## border

要素を線で囲みたい場合は**borderプロパティ**を使います。

**borderプロパティ**に指定する値は、**線の太さ**、**線の種類**、**色**で、各値を半角スペースで区切ります。

`border: 5px solid black`の場合は、線の太さが5pxで、線の種類が実線、色が黒ということになります。

線の種類は他にも、dotted(点線)、dashed(二重線)等があります。

下記の記述を追加しましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">&#x3C;!DOCTYPE html>
&#x3C;html lang="en">
  &#x3C;head>
    &#x3C;meta charset="UTF-8" />
    &#x3C;meta name="viewport" content="width=device-width, initial-scale=1.0" />
    &#x3C;link rel="stylesheet" href="css/normalize.css" />
    &#x3C;link rel="stylesheet" href="css/styles.css" />
    &#x3C;title>Document&#x3C;/title>
  &#x3C;/head>
  &#x3C;body>
    &#x3C;div class="color-box">
      &#x3C;p>colorとbackground-color&#x3C;/p>
    &#x3C;/div>
<strong>    &#x3C;div class="border-box">
</strong><strong>      &#x3C;p>border&#x3C;/p>
</strong><strong>    &#x3C;/div>
</strong>  &#x3C;/body>
&#x3C;/html>

</code></pre>

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">.color-box {
    color: bulue;
    background-color: yellow;
}

<strong>.border-box {
</strong><strong>  border: 5px solid black;
</strong><strong>}
</strong></code></pre>

ブラウザで以下のように、確認できるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FIIuYN5qgm06TLJig3FTF%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2015.09.26.png?alt=media&#x26;token=63c02420-0cae-4595-9011-862406099df5" alt=""><figcaption></figcaption></figure>

また、一部分のみ線を引きたい場合は、**border-top、border-bottom、border-left、border-right**を使いましょう。

```css
.border-box {
  border-bottom: 5px solid black;
}
```

上記のように指定した場合、下線のみになります。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2Fm7fSQWsncmOkVjhjBhQN%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2015.11.32.png?alt=media&#x26;token=96605808-ac5f-49c6-9894-b5622b8f69fb" alt=""><figcaption></figcaption></figure>

また、角を丸める場合は、**border-radius**を使用しましょう。指定したpx分、角を丸めることができます。

<pre class="language-css" data-title="styles.css"><code class="lang-css">...
.color-box {
    color: bulue;
    background-color: yellow;
}

.border-box {
  border: 5px solid black;
<strong>  border-radius: 20px;
</strong>}
...
</code></pre>

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FA0hFJTBv05uGa5td4ynX%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2015.14.17.png?alt=media&#x26;token=6d6b929a-9492-4c50-817d-f4cc44d6c2e8" alt=""><figcaption><p>＃</p></figcaption></figure>

## padding、margin

要素の外側に余白を入れたい場合は、padding、marginを使いましょう。

その前に、前セクションで説明したボックスモデルに関して、実際にページ上でボックスはどんな構成になっているのか説明します。

ボックスは以下のような構成になってます。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FfFirUO5u0utd3zKS1LNt%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2017.41.07.png?alt=media&#x26;token=2e843c69-df33-4782-b2a5-126587b87f2b" alt=""><figcaption></figcaption></figure>

まず、1番内側には、**コンテンツ領域**があります。こちらは内容が表示される部分になります。

その外側に**padding**があるのですが、こちらは一旦飛ばして、その外側に**border**があります。

**border**は、線を囲む場合の領域になります。borderを指定していない場合は、borderの領域はありません。

**padding**と**margin**の違いは、**borderの内側か、外側か**ということになります。

以下の記述を追加しましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html"> ... 
    &#x3C;div class="border-box">
      &#x3C;p>border&#x3C;/p>
    &#x3C;/div>
<strong>    &#x3C;div class="padding-margin-box">
</strong><strong>      paddingとmargin
</strong><strong>    &#x3C;/div>
</strong>  ...
</code></pre>

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.border-box {
  border: 5px solid black;
  border-radius: 20px;
}

<strong>.padding-margin-box {
</strong><strong>  border: 5px solid black;
</strong><strong>  padding: 40px;
</strong><strong>  margin: 80px;
</strong><strong>}
</strong>...
</code></pre>

ブラウザにて、borderの内側が40px、borderの外側が80px分余白ができたかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FhIOzUuAnm6siModWaJB5%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2017.49.10.png?alt=media&#x26;token=85962231-f04b-41f4-b93b-9dca80242cb3" alt=""><figcaption></figcaption></figure>

**padding**、**marigin**の指定に関して、上下左右の値をそれぞれ変えたい場合は、

**上、右、左、下**の順で記載することができ、`padding: 10px 20px 30px 40px`の場合は、

上10px、右20px、左30px、下40pxになります。

もし、上下と左右がそれぞれ同じ値の場合は、まとめることができ、

`padding: 20px 40px`と記載した場合は、上下が20px、左右が40pxとなります。

また、左側のみにpaddingを当てたい場合は、`padding-left: 20px`のように記載することができます。

実務では、よく`margin: 0 auto`という記述を使うことがあります。

これは、**コンテンツ幅**をウィンドウ幅いっぱいにせず、左右に空白を開ける際に、記述します。その際に、`width: 1080px`と記述して、コンテンツ幅を指定してセットで記述することが多いです。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FaolGSi8MmcujZC99uNLR%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-24%2013.59.54.png?alt=media&#x26;token=8367a970-2460-4c28-91ca-4a24ab62c81f" alt=""><figcaption></figcaption></figure>

## box-shadow

影をつけたい場合は、**box-shadow**を使いましょう。

box-shadowに指定する値は、**横軸方向の位置**、**縦軸方向の位置**を指定するので、

`box-shadow: 10px 10px`のように記述します。

横軸方向の位置は10pxだと、右方向に10px、-10pxだと、左方向に10px、

縦軸方向の位置は10pxだと、下方向に10px、-10pxだと、上方向に10pxになります。

さらに、**ぼかしの度合い、色**を指定することができ、位置の値に続けて、

`box-shadow: 10px 10px 5px gray`のように記述します。

下記の記述を追加しましょう。

<pre class="language-css" data-title=""><code class="lang-css">...
.color-box {
    color: bulue;
    background-color: yellow;
}

.border-box {
  border: 5px solid black;
  border-radius: 20px;
<strong>  box-shadow: 10px 10px 5px gray;
</strong>}
...
</code></pre>

ブラウザにて以下のように確認できるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FLhvy1R7ELXHCA0WQSimd%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2015.31.20.png?alt=media&#x26;token=5da69747-60ee-479c-8fee-b849a1c70339" alt=""><figcaption></figcaption></figure>

## width、height

要素の幅や、高さを変更したい場合は、**width**と**height**を使います。

下記の記述を追加しましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">...
    &#x3C;div class="padding-margin-box">
      paddingとmargin
    &#x3C;/div>
<strong>    &#x3C;div class="width-height-box">
</strong><strong>      &#x3C;p>widthとheight&#x3C;/p>
</strong><strong>    &#x3C;/div>
</strong>  &#x3C;/body>
...

</code></pre>

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.padding-margin-box {
  border: 5px solid black;
  padding: 40px;
  margin: 80px;
}

<strong>.width-height-box {
</strong><strong>  background-color: green;
</strong><strong>  width: 500px;
</strong><strong>  height: 500px;
</strong><strong>}
</strong>...
</code></pre>

ブラウザにて、幅500px、高さ500pxの要素が確認できるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2Ffa5CrRMbcR9Gp7Xps4X9%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2017.55.39.png?alt=media&#x26;token=e8eb24c8-54f6-41e2-9be3-6a74caa8c4c4" alt=""><figcaption></figcaption></figure>

上記にて値をpxにて指定しましたが、

実務では、width&#x306F;**%**、heightは**vh(view height)**&#x3092;使うことがよくあります。

`width: 50%`と指定すると、ウィンドウ幅50%の表示になります。

`height: 100vh`と指定すると、ウインドウの高さの100%の表示になります。

以下のように記述を修正しましょう。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.padding-margin-box {
  border: 5px solid black;
  padding: 40px;
  margin: 80px;
}

.width-height-box {
  background-color: green;
<strong>  width: 50%;
</strong><strong>  height: 100vh;
</strong>}
...
</code></pre>

ブラウザで確認すると、要素がブラウザ幅の半分で、ブラウザの高さいっぱいになったかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2F5s2RGGQpA9smcTaBXXBC%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2015.49.40.png?alt=media&#x26;token=8ed8a70f-d638-4b2c-b1c2-a032cf195946" alt=""><figcaption></figcaption></figure>

##

## font-size、font-weight、text-align

文字の大きさは**font-size**、文字の太さは**font-weigh**tを使用します。

**font-weigh**tは`100`から`900`まで100刻みで指定でき、数字が大きほど太くなります。

また、`normal` `bold`のキーワードを指定することができます。

下記の記述を追加しましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">...
    &#x3C;div class="width-height-box">
      &#x3C;p>widthとheight&#x3C;/p>
    &#x3C;/div>
<strong>    &#x3C;div class="font-box">
</strong><strong>      &#x3C;p>font-sizeとfont-weight、text-align&#x3C;/p>
</strong><strong>    &#x3C;/div>
</strong>...
</code></pre>

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.width-height-box {
  background-color: green;
  width: 50%;
  height: 100vh;
}

<strong>.font-box {
</strong><strong>  font-size: 40px;
</strong><strong>  font-weight: bold;
</strong><strong>}
</strong>...
</code></pre>

ブラウザで以下のように確認できるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2Fd0iVbnWrsxt5mPIoLhnp%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2017.23.08.png?alt=media&#x26;token=5cf7dfc7-0534-4569-be1a-7507704256d5" alt=""><figcaption></figcaption></figure>

文章の配置を変更したい場合は、**text-align**を使用しましょう。

**text-align**は`left`、`right`、`center`を指定することができます。

以下の記述を追加しましょう。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.font-box {
  font-size: 40px;
  font-weight: 900;
<strong>  text-align: center;
</strong>}

...
</code></pre>

ブラウザにて以下のように文章が中央に配置されたかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FQT2KoUSYyrhNYvenzEtQ%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2017.28.04.png?alt=media&#x26;token=da9d27e2-51e8-4848-aceb-64ceeb4e1dfb" alt=""><figcaption></figcaption></figure>

## display

前セクションで説明したボックスモデルに関して、タグによってブロックボックスか、インラインボックスか決まっていると説明しましたが、それを変更することができます。

* インラインボックスをブロックボックスにする場合は、`block`
* ブロックボックスをインラインボックスにする場合は、`inline`

また、**inline-block**というボックスに変更することができます。

**inline-block**というのは、インラインとブロックの中間的なボックスで、

**横並び**になるのですが、**widthやheight、paddingやmarginを変更することができます**。

また、`none`を指定すると、その要素は表示されなくなります。

## display: flex

そして、実務では**flex**をよく使います。

**flex**を指定すると、指定した要素の**子要素**が並列になります。

典型的な使用例として、下の写真のようなヘッダーのナビゲーションを作るとします。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FFtCkJPT1K6OP5rwIzA7l%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2018.14.22.png?alt=media&#x26;token=b4bd9bb9-324a-4213-8341-87ed8a384bfb" alt=""><figcaption></figcaption></figure>

下記の記述を追加しましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">...
    &#x3C;div class="font-box">
      &#x3C;p>font-sizeとfont-weight、text-align&#x3C;/p>
    &#x3C;/div>
<strong>    &#x3C;ul　class="nav">
</strong><strong>      &#x3C;li>メニュー1&#x3C;/li>
</strong><strong>      &#x3C;li>メニュー2&#x3C;/li>
</strong><strong>      &#x3C;li>メニュー3&#x3C;/li>
</strong><strong>    &#x3C;/ul>
</strong>...
</code></pre>

このままだと、liタグはブロックボックスであり、縦並びに表示されてしまうかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FD69Xf3Ats23WCzBvTp61%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2018.18.19.png?alt=media&#x26;token=bfa93283-5e8f-42c7-b800-1c1db088e646" alt=""><figcaption></figcaption></figure>

上記の問題を解消するために、ulタグに`display: flex`を指定すると、その直下の要素であるliタグを横並びにすることができます。

また、**gapプロパティ**を指定すると、横並びにした要素の間を指定した分、スペースを開けることができます。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.font-box {
  font-size: 40px;
  font-weight: 900;
  text-align: center;
  display: in;
}

<strong>.nav {
</strong><strong>  display: flex;
</strong><strong>  gap: 40px;
</strong><strong>}
</strong>...
</code></pre>

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FxHkdDVjV2RAhezwC0X8E%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2018.26.51.png?alt=media&#x26;token=05118fb5-36a6-408d-ada2-9c42cd5a9618" alt=""><figcaption></figcaption></figure>

新たに`display: flex`の要素を作りましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">...
    &#x3C;ul class="nav">
      &#x3C;li>メニュー1&#x3C;/li>
      &#x3C;li>メニュー2&#x3C;/li>
      &#x3C;li>メニュー3&#x3C;/li>
    &#x3C;/ul>
<strong>    &#x3C;div class="justify-box">
</strong><strong>      &#x3C;div class="justify-left-box">左側の要素&#x3C;/div>
</strong><strong>      &#x3C;div class="justify-right-box">右側の要素&#x3C;/div>
</strong><strong>    &#x3C;/div>
</strong>...
</code></pre>

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.nav {
  display: flex;
  gap: 40px;
}

<strong>.justify-box {
</strong><strong>  display: flex;
</strong><strong>  gap: 40px;
</strong><strong>}
</strong><strong>
</strong><strong>.justify-left-box {
</strong><strong>  background-color: red;
</strong><strong>  width: 50%;
</strong><strong>  height: 40vh;
</strong><strong>}
</strong><strong>
</strong><strong>.justify-right-box {
</strong><strong>  background-color: blue;
</strong><strong>  width: 15%;
</strong><strong>  height: 80vh;
</strong><strong>}
</strong>...
</code></pre>

ブラウザにて以下を確認することができるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FRvY4s1vOUOAEcBnLV8Qm%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2018.33.38.png?alt=media&#x26;token=f3592800-510b-4ae2-a13d-79db1190af00" alt=""><figcaption></figcaption></figure>

こちらに関して、横並びにした要素が左によっているかと思いますが、こちらを中央に寄せたい場合は、

**justify-contentプロパティ**に`center`を指定します。

<pre class="language-css" data-title="styles.css"><code class="lang-css">...
.justify-box {
  display: flex;
  gap: 40px;
<strong>  justify-content: center;
</strong>}
...
</code></pre>

上記にて、以下のように中央に寄ったことが確認できるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FKNNbLIe6pgA24oqbv7Cb%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2018.36.32.png?alt=media&#x26;token=40d8242b-822d-489b-888a-40a57e64089f" alt=""><figcaption></figcaption></figure>

コンテンツを左右いっぱいに配置したい場合は、`space-between`を指定します。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.justify-box {
  display: flex;
<strong>  justify-content: space-between;
</strong>}
...
</code></pre>

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FM5ClJQv5iw1FvT0xXvvL%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2018.37.10.png?alt=media&#x26;token=806982a8-94f2-4bb8-8d17-4ebbb5982a14" alt=""><figcaption></figcaption></figure>

上下方向に関して、要素を中央に合わせたい場合は、**align-itemsプロパティ**に`center`を指定します。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.justify-box {
  display: flex;
  justify-content: space-between;
<strong>  align-items: center;
</strong>}
...
</code></pre>

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FagFcE9g5hjD4QHmPls7b%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-23%2018.42.38.png?alt=media&#x26;token=91fde25a-dc8d-49d1-9797-860259e0b652" alt=""><figcaption></figcaption></figure>

また、新たに`display: flex`の要素を作りましょう。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">...
    &#x3C;div class="justify-box">
      &#x3C;div class="justify-left-box">左側の要素&#x3C;/div>
      &#x3C;div class="justify-right-box">右側の要素&#x3C;/div>
    &#x3C;/div>
<strong>    &#x3C;div class="flex-wrap-box">
</strong><strong>      &#x3C;div class="flex-wrap-item">item1&#x3C;/div>
</strong><strong>      &#x3C;div class="flex-wrap-item">item2&#x3C;/div>
</strong><strong>      &#x3C;div class="flex-wrap-item">item3&#x3C;/div>
</strong><strong>      &#x3C;div class="flex-wrap-item">item4&#x3C;/div>
</strong><strong>    &#x3C;/div>
</strong>...
</code></pre>

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.justify-right-box {
  background-color: blue;
  width: 15%;
  height: 80vh;
}

<strong>.flex-wrap-box {
</strong><strong>  display: flex;
</strong><strong>}
</strong><strong>
</strong><strong>.flex-wrap-item {
</strong><strong>  width: 30%;
</strong><strong>  height: 200px;
</strong><strong>  border: 1px solid brown;
</strong><strong>}
</strong></code></pre>

上記の場合、4つ子要素の幅が30%で、合計で120%となり、ブラウザからはみ出すかと思いますが、ブラウザで確認すると、100%に収まるように、各子要素をよしなに縮小しています。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FS3bobliPVO50v3bTflSc%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-24%2013.42.44.png?alt=media&#x26;token=04f451e5-ef48-4a6b-b36d-8b5dbda6ba33" alt=""><figcaption></figcaption></figure>

もし、幅を超える場合に、その子要素を折り返したい場合は、**flex-wrapプロパティ**に、`wrap`を指定しましょう。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">...
.justify-right-box {
  background-color: blue;
  width: 15%;
  height: 80vh;
}

.flex-wrap-box {
  display: flex;
<strong>  flex-wrap: wrap;
</strong>}

.flex-wrap-item {
  width: 30%;
  height: 200px;
  border: 1px solid brown;
}
</code></pre>

そうすると、以下のように折り返して表示させることができます。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2FWkb5zK5qy08hKSPAwDVW%2F%E3%82%B9%E3%82%AF%E3%83%AA%E3%83%BC%E3%83%B3%E3%82%B7%E3%83%A7%E3%83%83%E3%83%88%202023-11-24%2013.53.00.png?alt=media&#x26;token=55dc23de-9789-44dc-a464-c2c1c8a36394" alt=""><figcaption></figcaption></figure>
