# CSSの基本

## CSSの記述方法

次に作成した`index.html`にcssを適応します。

`part1`ディレクトリ内に、`styles.css`のファイルを作成します。

作成したら、以下を記述しましょう。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css"><strong>img {
</strong><strong>  width: 100px;
</strong><strong>  border-radius: 10px;
</strong><strong>}
</strong></code></pre>

CSSは、まず適応したいタグを記述します。上記ではi**mgタグ**を指定しています。タグを指定後、`{}`（カーリーブラケット）で括ります。

その中に、変更したい**プロパティ**（文字の大きさ、色、上下左右のスペースなど）と**値**を記述していきます。

上記の場合は、`width`とい**プロパティ**に`100px`という**値**をセットしています。

これにより、画像の横幅を100pxにすることができます。

`border-radius`は要素の角を値の分だけ、丸くすることができます。

作成したcssファイルを、htmlファイルに適応するためには、`index.html`ファイルのheadタグに、linkタグを追加します。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">&#x3C;!DOCTYPE html>
&#x3C;html>
    &#x3C;head>
        &#x3C;title>初めてのHTML&#x3C;/title>
<strong>        &#x3C;link rel="stylesheet" href="styles.css" />
</strong>    &#x3C;/head>
    &#x3C;body>
        &#x3C;img src="images/sample.jpg" alt="東茶屋街の街並みの画像" />
        &#x3C;p>東茶屋街の街並みです！&#x3C;/p>
        &#x3C;p>とても綺麗でした！&#x3C;/p>
    &#x3C;/body>
&#x3C;/html>

</code></pre>

上記により、以下のように画像のレイアウトが変更されたことが確認できるかと思います。

<figure><img src="https://1869761657-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FcUBbYqol4PMzZJggiMqV%2Fuploads%2F1xXEiA4gHQyKTVDpHpgA%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-20%2012.57.27.png?alt=media&#x26;token=2de097b2-c8c6-47d4-b77d-1f1d0216ac3e" alt=""><figcaption></figcaption></figure>

次に、「東茶屋街の街並みです！」の文字を太くしましょう。

ただ、下記のような記載の場合、「とても綺麗でした！」の文字も太くなってしまいます。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">img {
  width: 100px;
  border-radius: 10px;
}

<strong>p {
</strong><strong>  font-weight: bold;
</strong><strong>}
</strong></code></pre>

特定のタグにcssを適応したい場合、そのタグに**class属性**を付与します。

**class属性**の値は、英数字であれば自由に定義できます。

<pre class="language-html" data-title="index.html◎"><code class="lang-html">...
    &#x3C;body>
        &#x3C;img src="images/sample.jpg" alt="東茶屋街の街並みの画像" />
<strong>        &#x3C;p class="place">東茶屋街の街並みです！&#x3C;/p>
</strong>        &#x3C;p>とても綺麗でした！&#x3C;/p>
    &#x3C;/body>
...

</code></pre>

そしてcssファイルの方でclassを指定する場合は、先頭に`.`(ドット)を先頭に付けて指定します。

これで「東茶屋街の街並みです！」の文字列のみ太文字にすることができます。

<pre class="language-css" data-title="styles.css◎"><code class="lang-css">img {
  width: 100px;
  border-radius: 10px;
}

<strong>.place {
</strong><strong>  font-weight: bold;
</strong><strong>}
</strong></code></pre>
