• 締切済み

CSSでページ上部に余白ができてしまいました。

下記の(質問用に作った)html とcssの記述について教えてください。 これは幅800pxの水色の範囲の中に文字や写真を配置し、その外側は青色で、閲覧者の環境によって余白(青色)の幅が左右対称に増減するようにしました。 これをFirefoxで確認すると上部に5~7mmほど余白の青が見えてしまいます。 margin-top: 0px;  と記していますが、反映されていません。 どうすれば、上部の余白をなくして、上まですべて水色になるでしょうか? <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift JIS"> <title>練習・質問の為のサイトです</title> <style type="text/css"> div { width: 800px; margin-top: 0px; margin-right: auto; margin-left: auto; } .cont { width:800px; background-color:lightcyan; } body { background: navy; color: black; } h1 { background: teal; color: white } h3 { font-weight: bold; color: red; } </style> </head> <body> <div class="cont"> <h3>社会科の学習</h3> <h1>日本の地理について</h1> <p><img src="photo.jpg"></p> <h2>日本には47の都道府県があります。</h2> <h3>東北地方</h3> <p>青森 岩手 秋田 宮城 山形 福島<br> <br> <h3>関東地方</h3> <p>群馬 栃木 茨城 千葉 埼玉 東京 神奈川<br> <br> <br> <p>練習用の為、ここまでとする。 </body> </html> 以上です。 どうぞアドバイスよろしくお願いいたします。

みんなの回答

  • ORUKA1951
  • ベストアンサー率45% (5062/11036)
回答No.3

 原因は、IEが仕様どおりの動作をしないことが原因(自身のサイズをborder辺までと間違う)ですが、仕様どおりなら多少の隙間が開くのが正しい動作です。  --IEを標準モードで動作させれば、やはり隙間ができます。--- ・HTML要素、BODY要素のもつpadding  html,body{margin:0;padding:0;}  ですこし余白が小さくなる。 ・各ブロックのpaddingの値が内部のmarginより小さい  div{padding:15px;}  など大きめな値をいれとおく   この場合、ボックスの幅が大きくなります。 ・H1要素、P要素などのmargin  h3{border:solid 1px aqua;}  いれるとわかります。 のふたつです。 対策は、 1) html,body{margin:0;padding:0;} 2) 子供ブロックのpaddingの値を大きめにする。 3) body直下の要素にborderを入れる。 4) H1,H2,H3,Pなどのmarginを0にする。 の四点です。  サンプルを上げておきます。1~4の対策はコメントアウトしていますから、 ひとつずつコメントを外して、それぞれがどのような意味を持つかを理解してください。(前後の/* */を消す)  ★どれがどのような意味かを知っておくと良いでしょう。  なお、余計なことですが、HTML5の時代が来ることを考えて、きちんとセマンティックにマークアップしています。<div class="header">→<header></header>・・同様に<hgroup><section><footer>にするとHTML5になります。 ・header部分は<header> ・sectionには必ずひとつのh要素が入る。複数入るときは<hgroup>で囲む など、これはHTML4でも暗黙のうちに定められていたけど守られていなかった。 ※lightcyanは、使用できません。色名のキーワードは17色です。 4.3.6 Colors ( http://www.w3.org/TR/CSS2/syndata.html#value-def-color )  それ以外は、コードで指定しましょう。 ・<br>は余白をあけるために使ってはなりません。スタイルシートで指定しましょう。 ・日本語はフォントが大きいので、marginを0にする代わりに、line-heightを大きくすると読みやすいでしょう。またpやdd,liにはtext-indentを指定しておくと読みやすいでしょう。 ・H1にはそのページのタイトルを書きましょう。 ★HTML4.01strict + CSS2.1 のShift_JISです。  Another HTML-lint 5 ( http://www.htmllint.net/html-lint/htmllint.html# )  で検証済み ★<DOCTYPE>をIEでも標準モードで動作するように書いてあるので、すべてのブラウザできちんと表示されるはずです。 ★印刷時にはスタイルシートが適用されませんから素のスタイルで印刷されます。(media="screen") ★タブは、_に置換してあります。戻すこと。 ★この手の質問は、Webデザイン・CSS - 教えて!goo ( http://oshiete.goo.ne.jp/category/258 )が良いです。 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html lang="ja"> <head> _<meta http-equiv="content-type" content="text/html; charset=Shift_JIS"> _<title>サンプル</title> _<meta name="author" content="ORUKA1951"> _<meta http-equiv="Content-Style-Type" content="text/css"> _<link rev="made" href="mailto:oruka1951@hoge.com" title="send a mail" > _<link rel="START" href="../index.html"> _<style type="text/css"> <!-- html,body{ /*_margin:0;padding:0;*/ _background-color:navy; } div.header,div.section,div.footer{ _width:80%;min-width:480px;max-width:800px; _margin:0 auto; _background-color:rgb(224,255,255); /*_padding:15px; */ /*_border:solid 1px aqua;*/ } h1,h2,h3,h4,h5,h6,p{ /*_margin:0; */ _line-height:1.6em; } p{text-indent:1em;} div.section div.section{ _min-width:0; _margin-left:1em; _padding:0; } div.header h1{ _margin-top:80px; _font-size:1.6em; _background: teal; color: white; } div.header h2{ _position:absolute; _top:0; _font-size:1.8em; _background-color:rgb(224,255,255); } h3{color: red;} p.kenmei{text-indent:0;} --> _</style> </head> <body> _<div class="header"> __<div class="hgroup"> ___<h1>日本の地理について</h1> ___<h2>社会科の学習</h2> __</div> __<p><img src="photo.jpg" alt="地図" width="480" height="480"></p> _</div> _<div class="section"> __<h2>日本には47の都道府県があります。</h2> __<div class="section"> ___<h3>東北地方</h3> ___<p class="kenmei"><span>青森</span>、<span>岩手</span>、<span>秋田</span>、<span>宮城</span>、<span>山形</span>、<span>福島</span></p> __</div> __<div class="section"> ___<h3>関東地方</h3> ___<p class="kenmei"><span>群馬</span>、<span>栃木</span>、<span>茨城</span>、<span>千葉</span>、<span>埼玉</span>、<span>東京</span>、<span>神奈川</span></p> __</div> __<div class="section"> ___<h3>・・・</h3> ___<p>練習用の為、ここまでとする。</p> __</div> _</div> _<div class="footer"> __<h2>文書情報</h2> __<dl class="documentHistry"> ___<dt id="FIRST-PUBLISHED">First Published</dt> ___<dd>2012-08-10</dd> __</dl> __<address>&copy; ORUKA1951 2012 - 2016 All Rights Reserved mailto:*****</address> _</div> </body> </html>

全文を見る
すると、全ての回答が全文表示されます。
回答No.2

よくやる手法だけど *{ margin: 0px; padding: 0px; } をやっておいて必要な所(タグ,ID,CLASS)に対してmarginとpaddingで余白を入れていく。

hpmake
質問者

お礼

{ }の前の*や、padding, id,class など、まだ使ったことのない語なので今すぐに理解できませんが、調べながらトライしてみます。 どうもありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。
  • t_ohta
  • ベストアンサー率38% (5098/13329)
回答No.1

body と h3 に margin-top: 0px; を追加すると消えます。

hpmake
質問者

お礼

bodyとh3に下記のように追加したら、上部の余白がなくなりました! body { background: navy; color: black; margin-top: 0px; } h1 { background: teal; color: white } h3 { font-weight: bold; color: red; margin-top: 0px; } また、下記のmargin-rightと margin-leftと並んで記していたmargin-topは有っても無くても関係ない、とわかりました。 div { width: 800px; margin-right: auto; margin-left: auto; } どうもありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • ページ上部の余白をなくしたい(CSS)

    初歩的な質問で申し訳ありません。 以下のような記述なのですが、 <div id="container">の上部にスキマが出来てしまいます。 背景に仮色をつけたりして確認しましたが、 どうしても余白をなくすことができません。 marginの指定の仕方がおかしいのでしょうか? ご指摘いただけると助かります。 /* HTML(単純に書くとこんな感じです)*/ <body> <div id="container">   <div id="header"> <div> </div> </body> /* CSS */ body { margin : 0; padding : 0; background-color : #eeeeee; font-size : 12px; line-height : 150%; color : #333333; font-family : arial, helvetica, sans-serif, Verdana, Geneva, MSゴシック; text-align : center; } #container { margin : 0 auto; padding : 0; width : 751px; text-align : left; background-color : #ffffff; } #header { margin : 0 auto; padding : 0; width:745px; height:120px; background: transparent url(***.jpg) ; background-repeat: no-repeat; }

    • ベストアンサー
    • HTML
  • 最上部の余白の原因

    こんばんは、質問させてください。 XHTML 1.0 Transitional、CSS2で大枠のレイアウトを組んでいてはまっています。 コードは抜粋です。現在の書き方だと タイトル部分の上に対してのマージンによって <div class="header">の要素と一緒に余白がとられてしまい真っ白な余白が最上部にできてしまいます。 想定ではヘッダーの中で中央ぐらいにTitleという文字がくると思っていたのですが。。 原因はなんでしょうか? 宜しくお願い致します。 -index.html <body> <div class="header"> <p class="title">Title</p> </div> </body> -style.css *{ margin:0; padding:0; } .header{ background-color:blue; width:800px; height:250px; } .title{ width:200px; height:50px; background-color:red; color:white; margin-left:210px; margin-top:100px; }

    • ベストアンサー
    • CSS
  • safariで見るとページ上部に余白ができてしまう

    以下のhtmlとcssをChromeで見るとページ(サイト)上部に余白がないのですが、Safariで見ると余白ができてしまいます。どうやったら上部(と下部)の余白がどのブラウザでも表示されないようになりますでしょうか?初歩的な質問で申し訳ありませんがネットで検索してもわからなかったので、宜しくお願いいたします。 [html] <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.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" /> <link rel="stylesheet" type="text/css" href="index.css" /> <title></title> </head> <body> <div id="wrap"> <div id="inner"> <div id="header"> <img src="image_basic/logo.png" width="450" height="140" alt="veggietimeslogo" /> <img src="image_basic/slide.jpg" width="920" height="450" alt="slide2" /> </div><!-- /header --> <div id="mainwrap"> <div id="firstwrap"> <p>ここは一段目<br />ここは一段目<br />ここは一段目</p> </div><!-- /firstwrap --> <div id="middlewrap"> <p>ここは二段目<br />ここは二段目<br />ここは二段目</p> </div><!-- /middlewrap --> <div class="clear"><hr /> </div><!-- /clear --> </div><!-- /mainwrap --> <div id="sidewrap"> <p>ここはサイドバー<br />ここはサイドバー<br />ここはサイドバー</p> </div><!-- /sidewrap --> <div class="clear"><hr /> </div><!-- /clear --> <div id="footer"> </div><!-- /footer --> </div><!-- /inner --> </div><!-- /wrap --> </body> </html> [css] @charset "UTF-8"; /* CSS Document */ body{ background-color:#000000; text-align:center; margin:0;} #wrap{ background-color:#ffffff; margin:0 auto; width:960px; text-align:left;} #inner{ margin:0 20px;} #mainwrap{ float:left; width:610px;} #firstwrap{ float:left; width:300px;} #middlewrap{ float:right; width:290px;} #sidewrap{ float:right; width:290px;} .clear{ clear:both;} .clear hr{ display:none;} p { font-family:Tahoma, Geneva, sans-serif}

  • divタグ+CSSでのレイアウトで、Firefox, Operaで不必要な余白ができてしまいます。

    divタグ+CSSでレイアウトしようとしています。 横関係では全体がセンタリングされていて、縦関係においては、各ブロック要素間の余白がなくぴったりくっついている状態にしたいのですが、Firefox 1.0やOpera 8などを使ってレイアウトを確認すると、上下や要素間に余白が出来てしまい、なかなかうまくいきません。 以下、HTMLとCSSのソースを、レイアウトに関する部分だけ載せます。 [--HTML--] <body> <div id="all"> <div id="header"> <p>header</p> </div> <div id="body"> <p>body</p> </div> <div id="sidebar"> <p>sidebar</p> </div> <div id="footer"> <p>footer</p> </div> </div> </body> [--CSS--] @charset "shift_jis" body { margin: 0 auto; padding: 0; text-align: center; } div#all { width: 760px; background-color: blue; margin: 0 auto; padding: 0 0 20px; text-align: left; overflow: hidden; } div#header { position: relative; left: 17px; width: 717px; height: 50px; background-color: yellow; margin: 0; padding: 0; text-align: left; } div#body { position: relative; left: 17px; width: 522px; height: 200px; background-color: lime; margin: 0 0 2em; padding: 0; text-align: left; float: left; } div#sidebar { position: relative; left:32px; width: 180px; height: 200px; background-color: red; margin: 0 0 3em; padding: 0; float: left; } div#footer { position: relative; left: 17px; width: 717px; height: 100px; background-color: fuchsia; margin: 0; padding: 0; clear: both; } ---------- marginやpaddingを"0"にしているにもかかわらず、余白が生まれてしまうのはなぜなのでしょう・・?

    • ベストアンサー
    • CSS
  • ページの一番上にできた余分な余白の消し方

    ページの一番上に800×100の画像を用意し、それを背景画像として設定。 その上に<h1>のタイトルを表示しようとすると、 背景画像の上に無駄な余白ができてしまいます。 何か間違えているのでしょうか?この余白の消し方を教えてほしいです。 よろしくお願いいたします。 <html> <head> <title></title> <style type="text/css"> body { margin: 0; padding: 0; text-align: center; background-color: #000; color: #333333; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 100%; text-align: center; } div#container { width: 800px; background-color: #ffffff; text-align: left; margin: 0 auto; padding: 0; } div#header { width: 800px; height: 100px; background-image: url(image/sample.jpg); background-repeat: no-repeat; padding: 0; margin: 0; } h1 { padding-top: 15px; padding-right: 0; padding-bottom: 15px; padding-left: 0; font-size: 15px; font-weight: normal; line-height: 15px; margin-top: 30px; margin-right: 0px; margin-bottom: 0; margin-left: 0; color: #000; } div#mainContent { padding: 5px 15px 0 15px; } </style> </head> <body> <div id="container"> <div id="header"> <h1>テスト</h1> </div> </div> <div id="mainContent"></div> </body> </html>

    • ベストアンサー
    • HTML
  • ブラウザ上部に余白があります。

    近い現象はありましたが、解決出来なかったのでご質問させて頂きます。 下記のソースでhtmlを制作しましたが、#headerの上に余白があります。 どうすればこの余白をとる事が出来るでしょうか? お教え頂ければと思います。 ↓ソース↓ <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>無題ドキュメント</title> <style type="text/css"> <!-- html, body{ margin:0; padding:0; } div#header{ background-color: #F00; margin:0px; padding:0px; } --> </style> </head> <body> <div id="header"> <h1>test</h1> </div> </body> </html>

  • CSSがうまく読み込まれません

    いつもお世話になっています。 Dreamweaver CS4でホームページ作りをしているのですが、 突然CSSが読み込まれなくなりました。 htmlのソースは <!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"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>無題ドキュメント</title> <link href="CSS/test.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="1"> <p>1</p> </div> <div id="2"> <p>2</p> </div> <div id="3"> <p>3</p> </div> <div id="4"> <p>4</p> </div> <div id="5"> <p>5</p> </div> </body> </html> cssのソースは body { padding: 20px; width: 850px; margin-right: auto; margin-left: auto; border: 1px solid #999; } #1 { width: 850px; background-color: #666; } #2 { width: 850px; background-color: #999; } #3 { background-color: #CCC; width: 850px; } #4 { background-color: #999; width: 850px; } #5 { background-color: #666; width: 850px; } です。 どちらもエンコーディングはUTF-8です。 ちなみにDreamweaverで制作中の表示ではCSSは反映されているのですが、 プレビューまたはサーバーにアップすると反映されません。 なにが原因なのでしょう。。。 何卒よろしくお願いいたします。

    • ベストアンサー
    • HTML
  • CSSでの質問です

    初めて段組を作ってみたんですが、上の余白を無くするにはどうしたらいいのでしょうか? ソースは以下のとおりです。 <style type="text/css"> <!-- #wrapper { width: 760px; margin: 0px auto; } #header { background-color: #9933FF; height: 50px; top: 0px; } #primary { background-color: #c7d5ed; float: right; width: 550px; margin-top: 10px; margin-bottom: 10px; } #secondary { background-color: #f9cfba; float: left; width: 200px; margin-top: 10px; margin-bottom: 10px; } #footer { clear: both; background-color: #99FFFF; height: 50px; } --> </style> </head> <body> <div id="wrapper"> <div id="header"></div> <div id="primary"> </div> <div id="secondary"> </div> <div id="footer"></div> </div> </body> </html> センタリングを維持したまま上の余白をクリアしたいのですが、何か解消法はありますか?

  • cssでの要素間の余白について。

    ホームページを作成しており、画像の下にdivをくっつけて、配置したいのですが、どうやっても、余白が出来てしまいます。styleでdiv要素にmargin-bottom:0、h2にmargin-top:0と記述しても、余白が出来てしまい、困っています。 さらにdiv containerの大きさについても、styleでwidth,heightで、大きさを指定しているのですが、横に画面一杯に広がってしまい、大きさ指定が反映されません。 初歩的な質問かもしれませんが、どこに問題がありますでしょうか? ご教授お願いいたします。 "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html;charset=Shift_JIS"/> <title>xxxxxxxxxxxxxx</title> <style type="text/css"> body {background-color:#f5f5f5} div#container {width="800" height="40";background-color:#fff4d8} h1 {color:#330000;font-family:Geogia,Times New Roman,sans-selif} h2 {color:#330000;font-family:Geogia,Times New Roman,sans-selif;margin-top:0} </style> </head> <body> <div id="content"> <h1><img src="xxxxxx.jpg" alt="xxxxxxxxx" </h1> </div> <div id="container"> <h2> xxxxxxxx </h2> </div> </body> </html>

    • ベストアンサー
    • CSS
  • html、CSS共に初心者です。3段目の2列目の右に、4段目の右の内容

    html、CSS共に初心者です。3段目の2列目の右に、4段目の右の内容を持っていきたいのですが、どうしてもうまくいきません! 解決できる方がいましたら、どうかご教示願います! <style type="text/css"> <!-- * { padding : 0 ; margin : 0 ; } #wrapper { width : 700px; margin-left : auto ; margin-right : auto ; background-color : #eeffff ; } #header { width : 700px; height : 150px ; background-color : #335555 ; } #navigation { /* background-color : #cceeee ; */ width : 300px; float : left ; } #navigation h2 { width : 150px; background-color : #99bbbb ; } #navigation ul { width : 150px ; } #navigation li { width : 150px ; height : 30px ; } #navigation a { width : 150px ; height : 30px ; background-color : #bbdddd ; display : block ; } #content { background-color : #eeeeee ; width : 350px; float : right ; } #subcontent { width:200 float : left ; clear :both ; } #subcontent h4 { width : 150px; background-color : #99bbbb ; } #subcontent ul { width : 150px ; } #subcontent li { width : 150px ; height : 30px ; } #subcontent a { width : 150px ; height : 30px ; background-color : #bbdddd ; display : block ; } #thirdcontent { background-color : #eeeeee ; width : 500px; float : right ; } #footer { background-color : #335555 ; width : 700px ; clear : both ; } //--> </style> </head> <body> <div id="wrapper"> <div id="header"> <h1>pppppppppppppppppppppppppppp</h1> </div> <div id="navigation"> <h2>pppppppp</h2> <ul> <li><a href="#">pppppppp</a></li> <li><a href="#">pppppppp</a></li> <li><a href="#">pppppp</a></li> <li><a href="#">pppppppp</a></li> </ul> </div> <div id="content"> <h2>welcome to my hp</h2> <br> <h3>ppppppp</h3> <p>ppppppppppp</p> <h3>pppppppp</h3> <br> <p>pppppppp</p> </div> <div id="subcontent"> <h4>この横にカテゴリーを配置させたいのです</h4> <ul> <li><a href="#">pppppppp</a></li> <li><a href="#">pppppppp</a></li> <li><a href="#">pppppppp</a></li> <li><a href="#">ppppppppp</a></li> </ul> </div> <div id="thirdcontent"> <h4>カテゴリー(これを一段上に)</h4> <br> <h5>pppppppppp</h5> <p>ppppppppp</p> <h5>ppppppp</h5> </div> <div id="footer"> <br> </div> </div> </body> </html>

    • ベストアンサー
    • HTML

専門家に質問してみよう