CSS (Cascading Style Sheets)
CSS
見た目のスタイルを設定CSS 記述方法
1. インラインstyle
HTMLファイル:タグに直接スタイルを記述
<p style="color: green; background-color: orange">緑文字でオレンジ背景</p>
表示イメージ
緑文字でオレンジ背景
ちょこっとメモ
スタイルの継承
親のタグにスタイルを設定:子のタグにも適用2. styleタグに記述
<head> : <style> p { color: red; } </style> </head> <body> <p>赤い文字</p> </body>
表示イメージ
赤い文字
3. CSSファイルを読込む
index.html
<head> : <link rel="stylesheet" href="style.css"> </head> <body> <p>赤い文字</p> </body>
style.css
@charset "utf-8"; p { color: red; }
要素セレクタ
P{
color:red;
}
classセレクタ
index.html
<head> : <link rel="stylesheet" href="style.css"> </head> <body> <p class="moji">赤い文字</p> </body>
style.css
.moji { color: red; }
表示イメージ
赤い文字
idセレクタ
index.html
<head> : <link rel="stylesheet" href="style.css"> </head> <body> <p id="moji">赤い文字</p> </body>
style.css
#moji { color: red; }
表示イメージ
赤い文字
複数のセレクタ
h1, p { color: red; }
子セレクタ・子孫セレクタ
index.html
<head> : <link rel="stylesheet" href="style.css"> </head> <body> <article> <h1>メイン</h1> <p>サブ</p> <div> <p>1行目</p> <p>2行目</p> </div> </article> </body>
style.css
article p { color: red; }
表示イメージ
メイン
サブ
1行目
2行目
子セレクタのみ
index.html
<head> : <link rel="stylesheet" href="style.css"> </head> <body> <article> <h1>メイン</h1> <p>サブ</p> <div> <p>1行目</p> <p>2行目</p> </div> </article> </body>
style.css
article > p { color: red; }
表示イメージ
メイン
サブ
1行目
2行目
兄弟セレクタ:同一階層要素
index.html
<head> : <link rel="stylesheet" href="style.css"> </head> <body> <article> <h1>メイン</h1> <p>サブ</p> <div> <p>1行目</p> <p>2行目</p> </div> <p>3行目</p> </article> </body>
条件
- :first-child:最初の兄弟要素
- :last-child:最後の兄弟要素
- :nth-of-type(n):n番目の要素
特定部分の生成
- ::before:要素の前に子要素
- ::after:要素の後に子要素
- ::first-letter:要素の最初の1文字
コメント
/* コメント */