はじめに
この記事を読めば、HTML Living Standardを使いこなすことができるようになります。
初心者の方でも分かりやすいように、使い方、対処法、注意点、カスタマイズ方法を解説し、10のサンプルコードで具体的にどのように使用できるかを学べます。
●HTML Living Standardとは
○簡単な説明
HTML Living Standardとは、現在のHTML仕様を表す言葉です。
これは、HTMLの進化に合わせて常に更新されるため「生きた標準」と呼ばれています。
●使い方
ここでは、HTML Living Standardを使った基本的な使い方をサンプルコードで紹介します。
○サンプルコード1:フォーム作成
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sample Form</title>
</head>
<body>
<form>
<label for="name">Name:</label>
<input type="text" id="name" name="name">
<input type="submit" value="Submit">
</form>
</body>
</html>
このサンプルコードでは、簡単なフォームを作成しています。
○サンプルコード2:画像表示
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Image Display</title>
</head>
<body>
<img src="example.jpg" alt="Example Image">
</body>
</html>
このサンプルコードでは、画像を表示する方法を紹介しています。
○サンプルコード3:リンク設定
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Link Setting</title>
</head>
<body>
<a href="https://example.com">Visit Example Website</a>
</body>
</html>
このサンプルコードでは、リンクを設定する方法を紹介しています。
●対処法
○サンプルコード4:表示エラーの解決
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Error Fixing</title>
</head>
<body>
<h1>Heading</h1>
<p>Paragraph</p>
</body>
</html>
このサンプルコードでは、表示エラーを解決する方法を紹介しています。
●注意点
HTML Living Standardを使う際の注意点は、次の通りです。
- タグの閉じ忘れに注意する
- 属性の指定方法を正しく行う
- DOCTYPE宣言を忘れずに記述する
●カスタマイズ方法
○サンプルコード5:テーブルのデザイン変更
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Table Design Change</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
border: 1px solid black;
padding: 8px;
text-align: left;
}
th {
background-color: #f2f2f2;
}
</style>
</head>
<body>
<table>
<tr>
<th>Header 1</th>
<th>Header 2</th>
</tr>
<tr>
<td>Row 1 Column 1</td>
<td>Row 1 Column 2</td>
</tr>
<tr>
<td>Row 2 Column 1</td>
<td>Row 2 Column 2</td>
</tr>
</table>
</body>
</html>
このサンプルコードでは、テーブルのデザインを変更する方法を紹介しています。
●応用例
下記では、HTML Living Standardを応用したサンプルコードを紹介します。
○サンプルコード6:スライドショー作成
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Slideshow</title>
<style>
.slideshow {
display: none;
}
</style>
</head>
<body>
<img class="slideshow" src="image1.jpg" alt="Image 1">
<img class="slideshow" src="image2.jpg" alt="Image 2">
<img class="slideshow" src="image3.jpg" alt="Image 3">
<script>
let slides = document.getElementsByClassName("slideshow");
let index = 0;
function showSlides() {
for (let i = 0; i < slides.length; i++) {
slides[i].style.display = "none";
}
index++;
if (index > slides.length) { index = 1 }
slides[index - 1].style.display = "block";
setTimeout(showSlides, 3000);
}
showSlides();
</script>
</body>
</html>
このサンプルコードでは、スライドショーを作成する方法を紹介しています。
○サンプルコード7:動的なグラフ描画
※このサンプルコードでは、Chart.jsという外部ライブラリを使用しています。
Chart.jsの利用には、下記のように外部リソースを読み込む必要があります。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Dynamic Graph</title>
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<canvas id="myChart"></canvas>
<script>
const data = {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: 'Color Distribution',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(54, 162, 235, 0.2)', 'rgba(255, 206, 86, 0.2)', 'rgba(75, 192, 192, 0.2)', 'rgba(153, 102, 255, 0.2)', 'rgba(255, 159, 64, 0.2)'],
borderColor: ['rgba(255, 99, 132, 1)', 'rgba(54, 162, 235, 1)', 'rgba(255, 206, 86, 1)', 'rgba(75, 192, 192, 1)', 'rgba(153, 102, 255, 1)', 'rgba(255, 159, 64, 1)'],
borderWidth: 1
}]
};
const config = {
type: 'bar',
data: data,
options: {
scales: {
y: {
beginAtZero: true
}
}
}
};
const myChart = new Chart(document.getElementById('myChart'), config);
</script>
</body>
</html>
このサンプルコードでは、動的なグラフを描画する方法を紹介しています。
○サンプルコード8:レスポンシブデザイン
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Responsive Design</title>
<style>
.container {
max-width: 1200px;
margin: 0 auto;
}
.column {
float: left;
width: 100%;
}
@media screen and (min-width: 768px) {
.column {
width: 50%;
}
}
</style>
</head>
<body>
<div class="container">
<div class="column">
<p>Column 1 content...</p>
</div>
<div class="column">
<p>Column 2 content...</p>
</div>
</div>
</body>
</html>
このサンプルコードでは、レスポンシブデザインの実装方法を紹介しています。
○サンプルコード9:アコーディオンメニュー
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Accordion Menu</title>
<style>
.accordion {
cursor: pointer;
}
.panel {
display: none;
}
</style>
<script>
function toggleAccordion() {
this.classList.toggle("active");
let panel = this.nextElementSibling;
if (panel.style.display === "block") {
panel.style.display = "none";
} else {
panel.style.display = "block";
}
}
</script>
</head>
<body>
<button class="accordion" onclick="toggleAccordion.call(this)">Section 1</button>
<div class="panel">
<p>Section 1 content...</p>
</div>
<button class="accordion" onclick="toggleAccordion.call(this)">Section 2</button>
<div class="panel">
<p>Section 2 content...</p>
</div>
<button class="accordion" onclick="toggleAccordion.call(this)">Section 3</button>
<div class="panel">
<p>Section 3 content...</p>
</div>
</body>
</html>
このサンプルコードでは、アコーディオンメニューの作成方法を紹介しています。
○サンプルコード10:オンラインショッピングカート
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Shopping Cart</title>
<style>
.product {
display: flex;
justify-content: space-between;
margin-bottom: 1rem;
}
.add-to-cart {
cursor: pointer;
}
</style>
<script>
function addToCart(product) {
alert(product + " has been added to your cart!");
}
</script>
</head>
<body>
<div class="product">
<p>Product 1</p>
<button class="add-to-cart" onclick="addToCart('Product 1')">Add to Cart</button>
</div>
<div class="product">
<p>Product 2</p>
<button class="add-to-cart" onclick="addToCart('Product 2')">Add to Cart</button>
</div>
<div class="product">
<p>Product 3</p>
<button class="add-to-cart" onclick="addToCart('Product 3')">Add to Cart</button>
</div>
</body>
</html>
このサンプルコードでは、オンラインショッピングカートに商品を追加する機能の実装方法を紹介しています。
まとめ
この記事を読むことで、HTML Living Standardの基本から応用までの使い方が理解できました。
フォーム作成や画像表示、リンク設定、デザインのカスタマイズ、そして動的なコンテンツの作成など、様々な方法が紹介されました。
これらの知識を活かし、自分だけのウェブページを作成してみましょう。