演習(1)解答例

リセットと基準値を記述

  • リセットを記述(使用している要素のみ)


《style.css

@charset "utf-8";

/* reset */
html, body, div, h1, h2, h3, h4, p, small {
  margin: 0;
  padding: 0;
  line-height: 1.0;
  font-family:
    "Hiragino Kaku Gothic ProN",
    Meiryo,
    sans-serif;
}

/* font-size */
html { font-size: 62.5%; }  /* =10px */
body { font-size: 16px; font-size: 1.6rem; }  /* =16px */
h1 { font-size: 32px; font-size: 3.2rem; }
h2 { font-size: 24px; font-size: 2.4rem; }
h3 { font-size: 20px; font-size: 2.0rem; }
h4 { font-size: 18px; font-size: 1.8rem; }
p { font-size: 16px; font-size: 1.6rem; }

/* body */
body {
  background: #A6DCEB;
  color: #333;
}

レイアウト指定

  • 外枠から固定値を設定
  • レイアウトの空きは、下方に値を設定する
/* layout */
#container {
  width: 760px;
  background: #FFF;
  margin: 0 auto;
  padding: 20px;
}
#header {
  margin-bottom: 30px;
}
#content {
  margin-bottom: 20px;
}
#footer {
  border-top: 1px dotted #666;
  padding-top: 10px;
  text-align: center; /* 横幅全体の中央揃え */
}
各要素の幅・高さ
  • 各レイアウト要素には「幅」の数値を指定しなくても、親要素である「#container」の幅まで広がる仕様になっているので、指定は不要です
  • 高さは、レイアウトブロックの中に入るコンテンツ内容によりフレキシブルに増減をさせるため、基本的には「height: auto」を省略して記述しません


文字の修飾

  • 文字色は、いろいろな書き方がありますが、ここでは HEX Color で記述します(大文字・小文字は自由:授業では見やすさのため大文字)

#header部
  • 画像がからむ h1 要素は後回しにする


《クラス名「lead」を付与した場合》

/* #header */
#header p.lead {
  margin: 0 32px; margin: 0 3.2rem;
  line-height: 1.4;
  text-indent: 16px; text-indent: 1.6rem;
}


《子セレクタを指定した場合(クラス名「lead」は不要)》

  • #header部直下のp要素のみに適用
/* #header */
#header > p {
  margin: 0 32px; margin: 0 3.2rem;
  line-height: 1.4;
  text-indent: 16px; text-indent: 1.6rem;  /* 1文字分字下げ */
}


背景画像

このとき、この背景画像の読み込みには2つの方法があります。

  1. #headerの背景画像
  2. h1の背景画像
背景画像のトリミング

  • 「ファイル」メニュー → 「Web用に保存」
  • 「img」フォルダーに「header_bg.jpg」として保存


h1の背景画像に設定
  • 画像の読み込まれる背景色「#FFF」
  • 画像は繰り返し表示をしない
  • 読み込まれる位置は、左右「中央」、上下「上から」
  • 「h1要素」の配置する位置を「padding」で指定する
#header h1 {
  background: #FFF url(img/header_bg.jpg) no-repeat center top;
  padding: 150px 0 0 500px;
}
h1要素の文字サイズと色を調整
#header h1 {
  background: #FFF url(img/header_bg.jpg) no-repeat center top;
  padding: 150px 0 12px 520px;
  color: #FFF;
  font-size: 28px; font-size: 2.8rem;
}


  • h1要素と要約文との間をあける
#header h1 {
  background: #FFF url(img/header_bg.jpg) no-repeat center top;
  margin-bottom: 20px;
  padding: 150px 0 12px	 520px;
  color: #FFF;
  font-size: 28px; font-size: 2.8rem;
}


メインコンテンツの設定

  • 基本は文字サイズ以外の設定
  • 文字色、背景色、空き
/* #content */
#content h2 {
  color: #3E4B74;
  margin-bottom: 20px;
  padding: 7px 0 3px 10px;
  border-left: 12px solid #06C;
}
#content h3 {
  color: #1B416D;
  margin-bottom: 10px;
  padding: 10px 0 8px 16px;
  background: #E1EBFF;
  border-top: 1px dotted #1B416D;
  border-bottom: 1px dotted #1B416D;
}
#content h4 {
  color: #3268C9;
  margin-bottom: 8px;
}
#content p {
  margin: 0 0 16px 64px; margin: 0 0 1.6rem 6.4rem;
  line-height: 1.4;
}



スマートフォンに対応(レスポンシブWebデザイン)

  • HTMLにviewportを記述する
  • CSSにメディアクエリを記述する
viewport
  • viewportのwidthという値に、device-widthと指定すると、スマホの画面幅 = 表示するwindowサイズ、となる
<meta name="viewport" content="width=device-width">
メディアクエリ
  • CSSで書式指定を行う際に、条件を追加できる機能
  • レスポンシブWebデザインでは、モバイルの端末やブラウザの「画面の幅」を基準に条件分岐して書式を指定します
@charset "UTF-8";

/* reset */
html, body, div, h1, h2, h3, h4, p, small {
  margin: 0;
  padding: 0;
  line-height: 1.0;
  font-family:
    "Hiragino Kaku Gothic ProN",
    Meiryo,
    sans-serif;
}

/* font-size */
html { font-size: 62.5%; }  /* =10px */
body { font-size: 16px; font-size: 1.6rem; }  /* =16px */
h1 { font-size: 32px; font-size: 3.2rem; }
h2 { font-size: 24px; font-size: 2.4rem; }
h3 { font-size: 20px; font-size: 2.0rem; }
h4 { font-size: 18px; font-size: 1.8rem; }
p { font-size: 16px; font-size: 1.6rem; }

/* body */
body {
  background: #A6DCEB;
  color: #333;
}

/* layout */
#container {
  width: 760px;
  background: #FFF;
  margin: 0 auto;
  padding: 20px;
}
#header {
  margin-bottom: 30px;
}
#content {
  margin-bottom: 20px;
}
#footer {
  border-top: 1px dotted #666;
  padding-top: 10px;
  text-align: center;
}

/* #header */
#header h1 {
  background: #FFF url(img/header_bg.jpg) no-repeat center top;
  margin-bottom: 20px;
  padding: 150px 0 12px	 520px;
  color: #FFF;
  font-size: 28px; font-size: 2.8rem;
}
#header > p {
  margin: 0 32px; margin: 0 3.2rem;
  line-height: 1.4;
  text-indent: 16px; text-indent: 1.6rem;  /* 1文字分字下げ */
}

/* #content */
#content h2 {
  color: #3E4B74;
  border-left: 12px solid #06C;
  margin-bottom: 20px;
  padding: 7px 0 3px 10px;
}
#content h3 {
  color: #1B416D;
  margin-bottom: 10px;
  padding: 10px 0 8px 16px;
  background: #E1EBFF;
  border-top: 1px dotted #1B416D;
  border-bottom: 1px dotted #1B416D;
}
#content h4 {
  color: #3268C9;
  margin-bottom: 8px;
}
#content p {
  margin: 0 0 16px 64px; margin: 0 0 1.6rem 6.4rem;
  line-height: 1.4;
}

@media screen and (max-width:640px) {
/* font-size */
html { font-size: 62.5%; } 
body { font-size: 1.4rem; }
h2 { font-size: 1.8rem; }
h3 { font-size: 1.5rem; }
h4 { font-size: 1.4rem; }
p { font-size: 1.4rem; }

#container {
  width: 90%;
}
#header {
  margin-bottom: 1.6rem;
}

/* #header */
#header h1 {
  font-size: 2.4rem; 
  background: #FFF url(img/header_bg.jpg) no-repeat center top;
  background-size: 100%;
  text-align: center;
  margin-bottom: 1.0rem	;
  padding: 2.8rem 0 1.6rem 0;
}
#header > p {
  margin: 0;
  text-indent: 1.4rem;  /* 1文字分字下げ */
}

/* #content */
#content h2 {
  margin-bottom: 1.6rem;
}
#content h3 {
  padding: 0.6rem 0 0.5rem 0.5rem;
}
#content h4 {
  margin-left: 1.0rem;
}
#content p {
  margin: 0 0 1.0rem 1.0rem;
  line-height: 1.4;
}

/* #footer */
#footer p { font-size: 1.0rem; }