ウェブ学のすすめ

Study of Web Design & Programing

floatを使ったレイアウトver2

floatを使ったレイアウト - ウェブ学のすすめ
で書いたHTMLを授業で習ったことを踏まえてもう一度書き直してみました。

解答ver2

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>floatを使ったレイアウトver2</title>
<style type="text/css">
* {
    margin: 0;
    padding: 0;
}

body {
    color: #fff;
    background-color: #ccc;
    font-size: 1.0em;
    font-family:
	"ヒラギノ角ゴ Pro W3", 
	  "Hiragino Kaku Gothic Pro", 
	  "メイリオ", 
	  Meiryo, 
	  Osaka, 
	  "MS Pゴシック", 
	  "MS PGothic", 
	  sans-serif;
}

#container {
    background-color: #fff;
    width: 800px;
    height: auto;
    margin: 0 auto; /* ブラウザの中央寄せ */
    padding: 10px;
    overflow: hidden;
}

#header {
    background-color: #a3bed5;
    width: auto;
    height: 100px;
    margin: 0 0 10px; /* header下に10pxの余白 */
}

h1 {
    color: #fff;
    font-size: 1.6em;
    font-family: serif;
    padding: 20px 0 0 20px; /* 上と左にそれぞれ内側の余白20px */
}

/* 情報の優先順位が高いものから上から書いていく。sidebarから書かない。 */
#content {
    background-color: #dcd78a;
    width: 540px;
    height: 290px;
    float: right;
    margin: 0 0 10px; /* footerとの間隔を10px空ける */
    padding: 10px;
}

#sidebar {
    float: left;
    width: 200px;
    height: auto;
    padding: 10px 10px 0;
    background-color: #9cc56e;
}

#sidebar li {
    height: 80px;
    margin: 0 0 10px; /* liの下だけに余白10px */
}

ul {
    list-style-type: none;
}

img {
    border: none; /* imgの枠線を消す */
}

#footer {
    clear: both; /* floatの解除 */
    background-color: #e0b9d2;
    width: auto;
    height: 50px;
} 

address {
  text-align: center;
  font-style: normal;
  padding: 15px 0 0;
}
</style>
</head>
<body>
<div id="container">
<div id="header">
<h1>ここにサイトタイトルが入る</h1>
</div>
<div id="wrapper">
<div id="content">
<h2>ここに見出しが入る</h2>
<p>ここに本文が入る</p>
</div>
<div id="sidebar">
<ul>
<li><img src="http://jsrun.it/assets/j/p/x/6/jpx6R.gif" alt="" width="200px" height="80px" /></li>
<li><img src="http://jsrun.it/assets/j/p/x/6/jpx6R.gif" alt="" width="200px" height="80px" /></li>
<li><img src="http://jsrun.it/assets/j/p/x/6/jpx6R.gif" alt="" width="200px" height="80px" /></li>
</ul>
</div>
</div>
<div id="footer">
<address>ここに連絡先のテキストが入る</address>
</div>
</div>
</body>
</html>

Valid XHTML 1.0 Transitional正当なCSSです!

floatを使ったレイアウトver2 - jsdo.it - share JavaScript, HTML5 and CSS

ポイント

imgとbackground-image

説明がいらない画像CSSのbackground-image
意味がある画像HTMLのimg

imgはボーダーを生む
img {
  border: none; /* ボーダーリセット */
}
IE6はautoを理解できない
body {
  text-align: center; /* IE6でブラウザの中央表示 */
}
#container {
  overflow: hidden; 
}
floatするものにはwidth、heightを指定する
CSSは階層構造

重層構造の同じ階層でないと位置を指定できない