CSSをHTML内に書く
前回はcdnを使って外部に存在するcssを使ったが、その中身を知るために、具体的にCSSをHTMLに書いてみる。
1. 最初のバージョン
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>CSS埋め込みサンプル</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
margin: 40px;
}
h1 {
color: #336699;
text-align: center;
}
p {
color: #333;
font-size: 16px;
}
.highlight {
background-color: yellow;
font-weight: bold;
}
#footer {
text-align: center;
color: gray;
font-size: 12px;
margin-top: 50px;
}
</style>
</head>
<body>
<h1>ようこそ!</h1>
<p>これはHTML内にCSSを埋め込んだサンプルページです。</p>
<p class="highlight">この行はハイライトされています。</p>
<div id="footer">サンプル</div>
</body>
</html>
2. CSSの部分を入れ替える
<style></style>の部分を入れ替えてみよう。
<style>
body {
background-color: #121212;
color: #e0e0e0;
font-family: 'Segoe UI', 'Helvetica Neue', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
padding: 20px;
}
h1 {
color: #00acc1;
font-size: 2.5em;
margin-bottom: 0.5em;
}
p {
font-size: 1.2em;
line-height: 1.6;
max-width: 600px;
text-align: center;
}
.highlight {
color: #ffeb3b;
background-color: #333;
padding: 0.3em 0.6em;
border-radius: 5px;
box-shadow: 0 0 5px #ffeb3b88;
}
#footer {
margin-top: 2em;
font-size: 0.9em;
color: #888;
}
</style>
他のスタイル例
<style>
body {
background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
font-family: 'Comic Sans MS', 'Arial Rounded MT Bold', sans-serif;
color: #444;
margin: 0;
padding: 40px;
}
h1 {
color: #ff4081;
font-size: 3em;
text-align: center;
text-shadow: 2px 2px #fff;
margin-bottom: 0.3em;
}
p {
font-size: 1.2em;
line-height: 1.6;
text-align: center;
max-width: 600px;
margin: 0 auto 1.5em;
}
.highlight {
display: inline-block;
background-color: #ffff8d;
padding: 0.4em 0.8em;
border-radius: 12px;
color: #d500f9;
font-weight: bold;
box-shadow: 2px 2px 6px rgba(0,0,0,0.2);
transition: transform 0.3s ease;
}
.highlight:hover {
transform: scale(1.1);
}
#footer {
text-align: center;
color: #666;
font-size: 0.9em;
margin-top: 60px;
}
</style>