CSSでロールオーバーを中央揃えにする方法

このQ&Aのポイント
  • CSSを使ってロールオーバーを作成しました。画像が左揃えになっていて、中央揃えになりません。
  • どなたか、お力をお貸しください。
  • HTMLとCSSのコードが与えられています。
回答を見る
  • ベストアンサー

CSSでロールオーバーを作って中央揃えにしたい。

CSSを使ってロールオーバーを作成しました。 がんばっているのですが、なかなか難しく、壁ばかりです。 なんとか、ロールオーバーは出来たのですが、その画像が左揃えになっていて、中央揃えにならないのです。 どなたか、お力をお貸しください。よろしくお願い致します。 HTML ----------------------------------- <link href="css.css" rel="stylesheet" type="text/css"> <div class="contents_box2"> <h3 class="no"><a href="info.html"><img src="contents_img2_1.jpg" alt="教室のご案内" width="215" height="53" /></a></h3> <p class="contents_text2">ああああああああああああああああああああああああああああああああああああああああああああああああ <p class="contents_text2">あああああああああああああああああああああああああああああああああああああああああああああああああああああああ <p class="contents_text2"><br /> <div class="rollover01"><a href="info.html"><img src="button2.jpg" alt="詳しくはこちら" width="172" height="29" /></a></div> <img src="contents_img2_3.jpg" width="215" height="18" /></div> -------------------- css↓↓↓ ------------- BODY { COLOR: #666666; TEXT-ALIGN: center; margin-top: 0px; font: 12px/130% "MS Pゴシック", Osaka, "ヒラギノ角ゴ Pro W3"; } IMG { BORDER-RIGHT: 0px; BORDER-TOP: 0px; VERTICAL-ALIGN: bottom; BORDER-LEFT: 0px; BORDER-BOTTOM: 0px; margin: 0px; padding: 0px; } .contents_box2 { FLOAT: left; MARGIN: 0px 12px 0px 0px; WIDTH: 215px; TEXT-ALIGN: left; padding: 0px; background: url(contents_img2_2.jpg) repeat-y; } .contents_text2 { MARGIN: 10px 16px 0px } .contents_detail { PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; WIDTH: 215px; PADDING-TOP: 0px; TEXT-ALIGN: center } .rollover01 { width:172px; height:29px; background:url(img/button3.jpg) no-repeat center bottom; text-align: center; } .rollover01 a { display:block; width:172px; height:29px; font-size:1px; line-height:1px; outline:none; text-align: center; } .rollover01 a:hover { text-indent:-9999px; } -------------------------------------

質問者が選んだベストアンサー

  • ベストアンサー
  • abril
  • ベストアンサー率69% (388/560)
回答No.2

> ロールオーバーは出来たのですが、その画像が左揃えになっていて、中央揃えにならないのです。 …と仰っているところを見ると、 <div class="rollover01"><a href="info.html"><img src="button2.jpg" alt="詳しくはこちら" width="172" height="29" /></a></div> のブロック(a)を、 <div class="contents_box2">のブロック(A)(W215px)に対して水平方向にセンタリングになる様に配置したい、という解釈で宜しいでしょうか? もしそうであれば、(a)の親要素である(A)に"TEXT-ALIGN: left;"と定義されている為、子要素(a)は当然の結果としてそのプロパティを継承し左寄せになります。 (a)を(A)の幅に対してセンタリングさせたいのであれば: .rollover01 { (省略) margin: 0 auto;←追加 } として下さい。これで、IE6以外の主要モダン・ブラウザではセンタリングになります。 上記の定義は、親要素(A)の幅に対して左右均等なマージンを取る(結果としてセンタリング配置になる)、という様な意味と解釈して下さい。 ※ちなみにロールオーバーということであれば、button3.jpgはbutton2.jpgとのセットのoff/on画像でしょうからbutton3.jpgの画像自体がW172px・H29px、だと思われます。であれば、上記セレクタ中の"text-align: center;"は不要です(ブロックの幅と画像の幅自体が同一なので、centerだろうがleftだろうが結果は同じ)。 ところが、残念ながらIE6では仕様にバグがありこの"margin: auto;"が効きません。なので、この様な場合大概IE6用対策として、更に直近の親要素に"text-align: center;"を定義して子要素をセンタリングさせる、という手法が取られます。つまり今回の場合だと: .contents_box2 { (省略) TEXT-ALIGN: left;→"text-align: center;"に変更 (省略) } とすれば、IE6上でも同様の結果が得られます。 ただし、そうなると今度は親要素(A)のスタイルを継承して現在は左寄せになっている<p class="contents_text2">のブロックのテキストがセンタリングになってしまいます。それを元に戻す為には、更に: .contents_text2 { text-align: left;←追加 (省略) } と定義を追加してやる必要があります。 以上で解決はしますが、ちょっと他の問題点が見受けられます。 ■<img src="contents_img2_3.jpg" width="215" height="18" />という様な記述をしているところを見ると、XHTMLだと思いますが、それにしては書式が正しく統一されていません。 <link href="css.css" rel="stylesheet" type="text/css"> →<img~ />や<br~ />と同様、空要素は<link href="css.css" rel="stylesheet" type="text/css" />という様に閉じなければいけません。 <p class="contents_text2">テキスト <p class="contents_text2">テキスト <p class="contents_text2"><br /> →終了タグ</p>は省略できません。 ■ロールオーバーのさせ方がこの仕様である必然性が感じられません(間違っている、というわけではないのですが)。 .rollover01 a:hover { text-indent:-9999px; } 上記定義で、マウスオーバー時に<img src="button2.jpg" alt="詳しくはこちら" width="172" height="29" />(off画像)を表示領域外に追いやることで<a>の背景画像として設定しておいたbutton3.jpgの方(on画像)を表示させる、というやり方の様ですが、であれば: HTML側にはテキストリンクを置いて <div class="rollover01"><a href="info.html">詳しくはこちら</a></div> とした上で、CSSでは: .rollover01 a { (省略) background:url(button2.jpg) no-repeat center bottom;←マウスオーバーでない時はリンクの背景としてbutton2.jpgを表示 } .rollover01 a:hover { (省略) background:url(img/button3.jpg) no-repeat center bottom;←マウスオーバー時はリンクの背景としてbutton3.jpgを表示 } .rollover01 { (省略) text-indent:-9999px;←HTML側のテキストリンクを表示領域外に追いやる(非表示にする) } …とした方が理にかなっていると思いますが。 こういった"text-indent:-9999px;"を使うロールオーバーの手法は大概、HTML側ではテキストリンクとしてマークアップ(色々メリットがあるので)しておいて、CSSによって「テキストを事実上非表示&<a>の背景として画像を定義する」ことで画像リンクの様に表示させる、という為に使われますので。 多分、色々なtipsがちょっとごちゃ混ぜになった状態で試行錯誤されているのではないかと思われます。それぞれのプロパティの意味・性質、またどういった要素に対して有効であるか、といった事を基礎から学び直される事をお奨めします。XHTMLの文法も。 ※そもそも今回のセンタリング問題についても、全体を見直せばHTMLもCSSももっとすっきり無駄なく構成する事が可能なのですが、それはおいおい学んで改良してみて下さい。今回はあくまで現状に対しての問題解決の為の最小限の修正です。

yoyoyo7878
質問者

お礼

ありがとうございます。 そうなんですよね~。ずばりなんです。少しかじった程度で、他のサイトを見て真似て、いじくり回して、お手上げ状態ってのが本音なんですね。 説明がとてもわかりやすく助かりました。ありがとうございました。

その他の回答 (2)

  • abril
  • ベストアンサー率69% (388/560)
回答No.3

もう一案。こっちの方が(現状から)修正する部分は少なくて済みますね。 .rollover01 { width:172px;←削除 height:29px;←削除 background:url(img/button3.jpg) no-repeat center bottom; text-align: center; } .rollover01 a { (省略) margin: 0 auto;←追加 } ※.rollover01 {~}から幅と高さと削除しても、その子要素の<a>が、.rollover01 a {~}でブロック要素化され幅と高さの値が固定値で与えられているので、レイアウトに影響はありません。 これだけです。考え方の基本は先程と同じです。親要素である<div class="rollover01">~</div>(a)の幅を成り行きにすれば、更に親要素である<div class="contents_box2">~</div>(A)の幅を継承するので(a)の幅=(A)の幅となります。で、その子要素の<a>に対して"margin: 0 auto;"を追加すれば(a)の幅=(A)の幅の中でセンタリング配置となり、(a)のスタイルには既に"text-align: center;"が設定されているのでIE6用対策もここだけで完結する為、<div class="contents_box2">~</div>という親要素の他のコンテンツのスタイルに影響を与えません。

  • ganko3
  • ベストアンサー率67% (118/174)
回答No.1

<h3 class="no"><a href="info.html"><img src="contents_img2_1.jpg" alt="教室のご案内" width="215" height="53" /></a></h3> のところだけですが、 <h3 class="no" align="center"><a href="info.html"><img src="contents_img2_1.jpg" alt="教室のご案内" width="215" height="53" /></a></h3> に変更してください。 align="center"を入れるだけです。

関連するQ&A

  • CSSで、画像を左寄せにして全体を中央にする方法

    画像5点を左寄せで配置しています。 添付画像のようにブラウザの幅によって段数を変更できるようにしていますが、 どうしても、画像部分がセンタリングされません。 他のサイト等を丸2日かけて調べましたが、どうもうまい事できません。 どなたかご教授くださいませ。 よろしくお願いいたします。 以下、現状のHTMLとCSSです。 ■HTML <!DOCTYPE HTML> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial=1, maximum-scale=1"> <link rel="stylesheet" type="text/css" href="css/style2.css" media="all"> <title>タイトル</title> </head> <body> <header> <p>ヘッダー</p> </header> <div id="contents"> <a href="http://○○○○○○○/001.html"><img src="images/001s.jpg" > <a href="http://○○○○○○○/002.html"><img src="images/002s.jpg" > <a href="http://○○○○○○○/003.html"><img src="images/003s.jpg" > <a href="http://○○○○○○○/004.html"><img src="images/n004s.jpg" > <a href="http://○○○○○○○/005.html"><img src="images/005s.jpg" > </div> <footer> <p>フッター</p> </footer> </body> </html> ■CSS @charset "UTF-8"; *{ margin: 0;padding: 0} a { text-decoration : none} ul, ol { list-style : none} img { vertical-align : top} html { font-family : verdana, sans-serif; font-size : 120%; line-height : 150%; margin : 0; width : 100%; text-align:center; } body { width:100%; text-align:center; } header { width:100%; margin-bottom: 0px; color: #fff; font-size: 20px; height:30px; background : url(../images/back.jpg) } #contents { width:100%; padding : 30px 0 10px; text-align:left; } #contents img { margin-bottom : 24px; } footer { color: #fff; width:100%; font-size: 10px; height:30px; background : url(../images/back.jpg) }

    • 締切済み
    • CSS
  • css firefox IE 画像を中央揃えにしたいです

    初心者です。よろしくお願い致します。 cssでホームページを作っています。 index.htmlに画像を中央に貼り付けたいのですが、 firefoxでは中央揃えに、IEでは左側に表示されます。 下にタグを書くので、ご回答いただけましたら幸いです。 よろしくお願い致します。 -------------------------------------------------- <html> <head> <title>○○○</title> <link href="design/index.css" rel="stylesheet" type="text/css"> </head> <body> <div class="contents"> <img src="img/tobira.jpg"> </div> </body> </html> -------------------------------------------------- /*ページのレイアウト用css*/ body{ margin:0px; padding:0px; background-color:#FFFFFF; } body a img{ border: none; align:center; } .contents{ width:800px; margin:0px auto; margin-top:100px; } h1{ color: #FFFFFF; }

    • ベストアンサー
    • HTML
  • リスト内での画像の下部揃え

    下記のように、上部テキスト下部画像の組み合わせで、横並びでリストを作りました。 テキストの量に合わせて、画像が上下するので、見栄えが悪いです。それをテキストは そのままの位置で、画像は下部でそろえたいと思います。それが不可能な場合 せめてすべて下部で合わせたいです。 display: inline-block; vertical-align: bottom; を記入しましたが、全く効果ありません。 どうすればよろしいのでしょうか? HTML <body id="news" class="s"> <div > <ul class="u1" > <li><span class="time">テキスト</span> <div class="news_img"> <img src="hoge.jpg"> </li> </ul> </div> </body> CSS #news.s ul { padding: 10px; width: 100%; } #news.s li { float: left; width: 170px; height: 240px; padding:0 10px 8px 0; font-size: 12px; list-style: none; display:block; vertical-align: bottom; } #news.s li img { display: inline-block; vertical-align: bottom; border: solid 1px silver; width: 140px; height: 140px; max-width: 140px; max-height: 140px; } .news_img { display: table-cell; height: 160px; text-align: center; width: 160px; margin-top: 10px; } div.news_img div { /* IE 7 */ display: inline; zoom: 1; }

    • ベストアンサー
    • CSS
  • cssで画像を中央に寄せる方法について

    よろしくお願いします。一部の画像を中央に寄せたいのですが方法が分かりません。現在の内容は以下のような感じです。 【html】 <p class="font"> スタイルシート <span class="color2">レッスンブック</span> パソコン <img src="img/hoge.gif" alt="テスト" width="250" height="130" /> インターネット プリンター </p> 【css】 .font2{ text-align: left; width: 750px; padding: 20px; border-top: 2px solid #191970; border-right: 2px solid #191970; border-bottom: 2px solid #191970; border-left: 2px solid #191970; margin-left: 0; margin-right: 10; background-color: #ffff00; font-weight: bolder; } imgのみ中央に寄せたいのですがご指導をお願い致します。 良く分からなかったのですが以下のような記述をしてみたのですが 中央に寄りませんでした。 <img src="img/hoge.gif" class="aaa" alt="テスト" width="250" height="130" /> cssに .aaa{ text-align: center; } また、同スタイルにborderとpadding、widthとpaddingを使用するのは 有効な方法では無いのでしょうか? 前回のご質問で他の方にご指導頂いたのですが誤って締め切ってしまいました。 どうぞよろしくお願い致します。

    • ベストアンサー
    • HTML
  • CSSでレイアウトが崩れます

    CSSに挑戦したのですが、IE6.0とie7.0で若干表示がくります。 ただ、IEでは何とか表示します。 また、MAC IE5.2では、右側メインが左メニューの下に崩れて表示されてしまいます。 ドリームウィーバーでもやはり表示が崩れるのです。 cssは以下になります。 左と右のレイアウトに問題があるのでしょうか。一部省略しました。 #Wrapper { padding: 0px; width: 800px; display: block; margin: 0px; background: url(../images/bg_img_01.jpg) repeat-y bottom; height: auto; } body { margin: 0px; padding: 0px; text-align: center; color: #333333; font-size: 12px; line-height: 150%; vertical-align: middle; } #imgR { padding: 0px; float: left; width: 500px; margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; } #arrow { margin: 0px; padding: 0px; float: left; width: 500px; } #rContents { text-align: left; padding: 0px; margin-top: 30px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: #111184; } .TextRink { text-decoration: none; color: #FF6600; display: inline; margin: 0px; padding: 0px 0px 0px 5px; } #WrapperL { margin: 0px; padding: 0px; width: 215px; height: auto; float: left; } #WrapperR { padding: 0px; width: 563px; height: auto; margin-top: 0px; margin-right: 15px; margin-bottom: 0px; margin-left: 0px; } #Footer { padding: 0px; height: 52px; width: 563px; background-image: url(../images/footer.gif); background-repeat: no-repeat; display: block; float: left; background-position: bottom; margin: 50px 0px 0px; vertical-align: bottom; } .sabu-title { padding: 13px 0px 0px; } .underline1 { border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: #CCCCCC; } #Header { display: block; margin: 0px; padding: 0px; height: 147px; width: 563px; background-image: url(../images/header_img.jpg); background-repeat: no-repeat; } .HeaderText { color: #FFFFFF; padding-top: 3px; margin: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 150px; font-size: 10px; }

  • CSSで画面を構成しています。

    CSSで画面を構成しています。 左ブロック、右ブロック共に角丸の四角にしたいので、**_head.gifというようなフタとソコになるような画像を使っています。 このままだと左右ブロックの高さが当然そろわないのですが、何か良い方法はないでしょうか? テーブルレイアウト以外であれば、多少イレギュラーな方法でもかまいません。 *css********** div#body{width:800px; margin:10px auto; text-align:center; padding:0; } div#header{padding:0; margin:0 0 10px 0; background-image:url(**.gif); width:800px; height:50px; text-align:left; } div#main{ width:800px; text-align:left; } div#submenu{padding:0; margin:0 10px 10px 0; width:200px; text-align:left; float:left; } div#contents{padding:0; margin:0 0 10px 210px; width:590px; text-align:left; } div#footer{padding:0; margin:0 0 5px 0; clear:both; width:800px; height:30px; text-align:left; } *html**** <div id="body"> <div id="header"> ヘッダー </div> <div id="main"> <div id="submenu"> <img src="img/common/sub_head.gif" width="200" height="10" />  <div>左ブロック</div> <img src="img/common/sub_foot.gif" width="200" height="10" /> </div> <div id="contents"> <img src="img/common/main_head.gif" width="590" height="10" />  <div>右ブロック</div> <img src="img/common/main_foot.gif" width="590" height="10" /> </div> </div> <div id="footer"> フッター </div> </div>

    • ベストアンサー
    • HTML
  • HTML、CSS が、なかなかうまくいきません。

    お世話になります。 HTMLを始めたばかりですので、 ご教授いただけたら幸いです。 以下の図のような感じに並べたいのですが、 なかなかうまい具合にいきません。 黄色は、全てボタンになります。 赤は、ボタンについた吹き出し部分となります。 上段の2つのボタンは、やや大きい感じのボタン。 下段の3つのボタンは、上段に比べて、少し小さい感じのボタンになります。 ボタンは、上段(A)と下段(B)で、 HTML と CSS は、分けようかと考えています。 【HTML】 <div class="Button"> <div class="contents"> <div class="Button_boxA clearfix"> <div class="Yellow_Btn_01"><img src="img/Yellow_Btn_01.png" width="200" height="75" /> </div> <div class="Yellow_Btn_02"><img src="img/Yellow_Btn_02.png" width="200" height="80" /> </div> </div> <div class="btn_boxB clearfix"> <div class="Yellow_Btn_03"><img src="img/Yellow_Btn_03.png" width="100" height="60" /> </div> <div class="Yellow_Btn_04"><img src="img/Yellow_Btn_04.png" width="100" height="65" /> </div> <div class="Yellow_Btn_05"><img src="img/Yellow_Btn_05.png" width="100" height="60" /> </div> </div> </div> </div> 【 CSS 】 div.Button{ width:600px; margin-top:20px; margin-right:auto; margin-left:auto; padding-right:25px; } div.btn_boxA{ width::500px; margin-left:auto; margin-right:auto; margin-bottom:10px; } div.low_btn_01{ width:200px; height:75px; margin-left:auto; margin-right:auto; margin-bottom:10px; float:left; } div.low_btn_02{ width:200px; height:80px; margin-left:auto; margin-right:auto; padding-left:25px; margin-bottom:10px; float:left; } div.btn_boxB{ width::500px; margin-left:auto; margin-right:auto; margin-bottom:10px; } div.low_btn_03{ width:100px; height:60px; margin-left:auto; margin-right:auto; margin-bottom:10px; padding-left:25px; float:left; } div.low_btn_04{ width:100px; height:65px; margin-left:auto; margin-right:auto; margin-bottom:10px; padding-left:25px; float:left; ] div.low_btn_05{ width:100px; height:60px; margin-left:auto; margin-right:auto; margin-bottom:10px; padding-left:25px; float:left; } 長ったらしくなってしまいましたが、 ご教授いただけたら、幸いです。 宜しくお願いします。

    • 締切済み
    • CSS
  • 疑似インラインフレームを中央に配置する方法

    疑似インラインフレームを中央に配置する方法 疑似インラインフレームを画面中央に配置する方法を教えてください。 ちなみに、以下のような記述を試してみたのですが…。 【css】 body { text-align: center; margin-top: 100px; color: #555555; font-size: 10px; font-family: "MS Pゴシック"; } *{ margin:0px; border:0px; padding:0px; line-height:100%; } .box{ text-align: center; padding-bottom: 50px; } .box_scroll{ text-align: center; width: 300px; height: 200px; padding: 10px 10px; overflow: auto; } 【xhtml】 <div class="box"> <img src="img.jpg" width="100" height="50" alt="img" /> </div> <div class="box"> <div class="box_scroll"> ---文章--- </div> </div> 初めの<div class="box">は中央に配置されるのですが、疑似インラインの部分は左に寄ってしまいます。 他にも他の方の質問を参考に試してみたのですが、初心者の為いまいちよくわからず、うまくいきませんでした。 どうか回答よろしくお願いいたします。

    • ベストアンサー
    • HTML
  • cssによる配置の計算が合いません

    cssによる配置で width の計算がうまくあいません。 コンテンツ部分が 750(ページ)-5(padding)-1(border)-134(navi-width)-5(padding)-1(border)=604(contents-border) ちなみにSafariでは計算通りでした。 IE6ではだめなようです。 くわしくは <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"> BODY { padding-top : 0px; margin-top : 0px; text-align : center; padding-bottom : 0px; margin-bottom : 0px; } #page { background-color : #dd22aa; width : 750px; margin : 0; padding-top : 0px; text-align : left; margin-top : 0px; margin-left : auto; margin-right : auto; margin-bottom : 0px; height: 100%; padding : 0px ; } #header { width : 750px; height : 80px; position : relative; float:left; clear : both; background-color : #008899; padding: 0px; margin: 0px; color : #b99859; } #navi { width : 134px; height : 399px; float : left; position : relative; clear : both; background-color : #ffffff; background-repeat : no-repeat;background-position : center top; padding-top : 50px; padding-left : 0px; padding-right : 0px; padding-bottom : 0px; margin-top : 0px; margin-left : 5px; margin-right : 0px; margin-bottom : 0px; border-left-style : solid; border-left-width : 1px; border-left-color : #000000; } #contents { width :596px; height : 399px; float : right; position : relative; padding : 0px; margin-top : 0px; margin-left : 0px; margin-right : 5px; margin-bottom : 0px; background-color: #9999FF; border-right-style : solid; border-right-width : 1px; border-right-color : #000000; } </style> <title>テスト</title> </head> <body> <div id="page"> <div id="header"> ヘッダー </div> <div id="navi"> ナビゲーション。左のパディングが5px。左のボダーが1px。幅が134px。 </div> <div id="contents"> コンテンツ。右のパディングが5px。右のボダーが1px。幅が596px。計算すると、750-5-1-134-5-1=604(幅)となるはずなのですが、596pxでないとはまりません。 </div> </div> </body> </html>

  • CSSで画像を左右中央に配置させたい。

    CSSで画像を左右中央に配置させたい。 CSSとXHTMLでWebページをつくっていますが、左右550pxくらいの説明画像をつくりました。 コンテンツ左右は600pxくらいでして、この600pxのDIVボックスモデルの中に左右中央に配置させたいのですが、検索しても遠回りな方法ばかりしか見つからず困っております。 たった画像を中央に配置させるだけのことがそんなに難しいんでしょうか? 検索してますと色々と複合技で達成するとか、モダンブラウザはこれを記述し、IE6にはこれを記述するとか、複雑なことしか見当たりません。 <div class="boxmodel600"> <p>文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。文章エリア。 </p> <img class="img-btmctr" src="../img/img01.gif" alt="" width="550" height="400" /> </div> CSSのほうは、以下です。ダメだったんでimg-btmctrのほうに「width: 600ppx;」と入れたんですがそれでもダメでした。 img.img-btmctr { vertical-align: bottom; padding: 10px 0 0 10px; margin-left: auto; margin-right: auto; width: 600ppx; } .boxmodel { display: block; width: 600px; margin-left: auto; margin-right: auto; margin-top: 0; margin-bottom: 30px; text-align: left; }

    • ベストアンサー
    • HTML