HTML を分類 (クラスを設定) し、要素のクラスの CSS スタイルを定義できるようにします。

同じクラスに同じスタイルを設定するか、異なるクラスに異なるスタイルを設定します。

<!DOCTYPE html>
<html>
<head>
<style>
.cities {
    background-color:black;
    color:white;
    margin:20px;
    padding:20px;
} 
</style>
</head>

<body>

<div class="cities">
<h2>London</h2>
<p>
London is the capital city of England. 
It is the most populous city in the United Kingdom, 
with a metropolitan area of over 13 million inhabitants.
</p>
</div> 

</body>
</html>

自分で試してみる

カテゴリ ブロック レベルの要素

HTML <div> 要素はブロックレベル要素です。 他の HTML 要素のコンテナとして使用できます。

<div> 要素のクラスを設定すると、同じ <div> 要素に同じクラスを設定できます。

<!DOCTYPE html>
<html>
<head>
<style>
.cities {
    background-color:black;
    color:white;
    margin:20px;
    padding:20px;
} 
</style>
</head>

<body>

<div class="cities">
<h2>London</h2>
<p>London is the capital city of England. 
It is the most populous city in the United Kingdom, 
with a metropolitan area of over 13 million inhabitants.</p>
</div>

<div class="cities">
<h2>Paris</h2>
<p>Paris is the capital and most populous city of France.</p>
</div>

<div class="cities">
<h2>Tokyo</h2>
<p>Tokyo is the capital of Japan, the center of the Greater Tokyo Area,
and the most populous metropolitan area in the world.</p>
</div>

</body>
</html>

自分で試してみる

インライン要素を分類する

HTML <span> 要素は、テキストのコンテナとして使用できるインライン要素です。

<span> 要素のクラスを設定し、同じ <span> 要素に対して同じスタイルを有効にします。

<!DOCTYPE html>
<html>
<head>
<style>
  span.red {color:red;}
</style>
</head>
<body>

<h1>My <span class="red">Important</span> Heading</h1>

</body>
</html>

自分で試してみる