第6章 WEBレイアウト

この章では、CSSついて説明します。

HOMEPAGE

WEBレイアウト


Webサイトのレイアウトはページ要素であるヘッダー、ナビゲーション、コンテンツ、フッターをどのうように配置するかで決まります。基本レイアウトは上部ナビゲーション型 、左サイドナビゲーション型、右サイドナビゲーションなどあります。

例:[sample6-1.html]
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>タイトル</title>
  5. <style>
  6. #b01 {
  7. border:dashed 5px red;
  8. width:100%;
  9. height:70px;
  10. text-align: center;
  11. font-size:30px;
  12. color:red;
  13. }
  14. #b02 {
  15. border:dashed 5px blue;
  16. width:100%;
  17. position:relative;
  18. height:60px;
  19. margin:0 0 0 0;
  20. text-align: center;
  21. font-size:30px;
  22. color:blue;
  23. }
  24. #b03 {
  25. border:dashed 5px green;
  26. width:100%;
  27. position:relative;
  28. height:200px;
  29. margin:0 0 0 0;
  30. text-align: center;
  31. font-size:30px;
  32. color:green;
  33. }
  34. #b04 {
  35. border:dashed 5px gray;
  36. width:100%;
  37. height:70px;
  38. position:relative;
  39. text-align: center;
  40. font-size:30px;
  41. color:gray;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div id="b01">ヘッダー</div>
  47. <div id="b02">ナビゲーション</div>
  48. <div id="b03">コンテンツ</div>
  49. <div id="b04">フッター</div>
  50. </body>
  51. </html>
実行結果

ヘッダー
ナビゲーション
コンテンツ
フッター


<div>タグはそれ自身は特に意味を持っていませんが、 CSSを加えてウェブページのレイアウトを設定することができます。

例:[sample6-2.html]
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>タイトル</title>
  5. </head>
  6. <body>
  7. <div style="width:50px;height:50px;background-color:red;">div1</div>
  8. <div style="width:50px;height:50px;background-color:green;margin:0 0 0 50px;">div2</div>
  9. <div style="width:50px;height:50px;background-color:blue;margin:0 0 0 100px;">div3</div>
  10. </body>
  11. </html>
実行結果  
div1
div2
div3

これまでCSSのセレクタは要素( <h1>、 <p>など)を指定していましたが、 <p>要素のように多用される要素の場合、同じ要素でも別なCSSを適用したい場合があります。そのような場合、HTMLの id属性と class属性を利用します。

例:[sample6-3.html]
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>タイトル</title>
  5. <style>
  6. #I1 {
  7. font-size: 25px;
  8. }
  9. #I2 {
  10. color:red;
  11. }
  12. .C1 {
  13. font-size: 30px;
  14. color:green;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <p id="I1">id要素1</p>
  20. <p id="I2">id要素2</p>
  21. <p class="C1">class要素1</p>
  22. <p class="C1">class要素2</p>
  23. </body>
  24. </html>
実行結果  

id要素

id要素

class要素

class要素

positionプロパティは、HTML要素の配置方法を、 relative相対位置と absolute絶対位置を指定する際に使用します。
z-indexプロパティは、HTML要素の重なりの順序を指定する際に使用します。

  1. <style>
  2. セレクタ名{
  3. position: 位置種類;
  4. z-index: 数値;
  5. }
  6. </style>

z-indexの指定なり
例:[sample6-4.html]
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>タイトル</title>
  5. <style>
  6. .divSize { width: 100px; height: 100px; border: 1px black solid; font-size: 40px; }
  7. .divParent { width: 200px; height: 200px; border: 1px black dotted; position: relative; }
  8. /* 背景色付け */
  9. #div1 {background-color: #FF8550; }
  10. #div2 {background-color: #93ca76; }
  11. #div3 {background-color: #76b9ca; }
  12. /* z-indexの指定なし */
  13. #div1 { position: absolute; }
  14. #div2 {
  15. position: absolute;
  16. top: 40px;
  17. left: 40px;
  18. }
  19. #div3 {
  20. position: absolute;
  21. top: 80px;
  22. left: 80px;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="div1" class="divSize">A</div>
  28. <div id="div2" class="divSize">B</div>
  29. <div id="div3" class="divSize">C</div>
  30. </body>
  31. </html>
実行結果
A
B
C



z-indexの指定あり
例:[sample6-5.html]
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>タイトル</title>
  5. <style>
  6. .divSize { width: 100px; height: 100px; border: 1px black solid; font-size: 40px; }
  7. .divParent { width: 200px; height: 200px; border: 1px black dotted; position: relative;}
  8. /* 背景色付け */
  9. #z_div1 {background-color: #FF8550; }
  10. #z_div2 {background-color: #93ca76; }
  11. #z_div3 {background-color: #76b9ca; }
  12. /* z-indexの指定あり */
  13. #z_div1 {
  14. position: absolute;
  15. z-index: 3;
  16. }
  17. #z_div2 {
  18. position: absolute;
  19. top: 40px;
  20. left: 40px;
  21. z-index: 2;
  22. }
  23. #z_div3 {
  24. position: absolute;
  25. top: 80px;
  26. left: 80px;
  27. z-index: 1;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="div1" class="divSize">A</div>
  33. <div id="div2" class="divSize">B</div>
  34. <div id="div3" class="divSize">C</div>
  35. </body>
  36. </html>
実行結果
A
B
C

練習

問題 [Ex5_1.html]
二章で作ったウェブページにCSSを添加しましょう。

html03 pic here

戻る