CSSを使用してテーブルのdivを中央に配置するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/1042209

  •  22-07-2019
  •  | 
  •  

質問

ウェブサイトの1つにスライドショーを追加しようとしています。ページ全体がHTMLテーブルにレイアウトされています(情熱を持って嫌いで、選択しませんでした)。スライドショーを特定の列の中央に配置したい。 CSSは次のようになります。

#slideshow {
position:relative;
}

#slideshow IMG {
position:absolute;
z-index:8;
opacity:0.0;
}

#slideshow IMG.active {
z-index:10;
opacity:1.0;
}

#slideshow IMG.last-active {
z-index:9;
}

画像を変更するための私のJQuery関数は次のとおりです。

function slideSwitch() {
var $active = $('#slideshow IMG.active');

if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

// use this to pull the images in the order they appear in the markup
var $next =  $active.next().length ? $active.next()
    : $('#slideshow IMG:first');

$active.addClass('last-active');

$next.css({opacity: 0.0})
    .addClass('active')
    .animate({opacity: 1.0}, 1000, function() {
        $active.removeClass('active last-active');
    });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

最後に、テーブル内のスライドショーdivを示します。

<td bgcolor="red" align="center" valign="top"> 
<div id="slideshow">
    <img src="2009Slideshow\1.jpg" alt="Slideshow Image 1" class="active" />
    <img src="2009Slideshow\2.jpg" alt="Slideshow Image 2" />
    <img src="2009Slideshow\3.jpg" alt="Slideshow Image 3" />
    etc...
    etc...
</div>                    
</td> 

では、スライドショーをその列の中央に配置できないのはなぜですか?

役に立ちましたか?

解決

cssプロパティ<!> quot; text-align:center; <!> quot;を使用すると、インライン要素を簡単に中央揃えできます。ブロック要素は、<!> quot; margin-left:auto; margin-right:auto; <!> quot;を使用して中央に揃える必要があります。そして、それらに固定幅を与えることによって。あなたの特定のケースでは、<!> quot; div <!> quotではなくspanを使用することをお勧めします。または、divを完全に取り除き、text-alignメソッドを使用します。

更新

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">   
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">     
    <head>      
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />   
        <title>Centering</title>
    <style type="text/css">
      table{
       border:1px solid black; 
      }
      span{
        background-color:red; 
        color:white;       
      } 
      td{
        text-align:center;
        width:200px; 
      }
    </style>
  </head>
  <body>
    <div class="item" id="item1">
      <table>
        <tr>
          <td>
            <span>Hi there!</span>  
          </td>
        </tr>        
      </table>
    </div>    
    </body>
</html> 

他のヒント

#slideshow {
margin: 0 auto;
}

and also text-align: center

テーブル用

次の解決策は、画像のサイズがわからない場合でも機能します。

  

<!> lt; div style = <!> quot; height:100px;幅:100px; display:table-cell; text-align:center; vertical-align:middle; <!> quot; <!> gt;
   <!> nbsp; <!> nbsp; <!> nbsp; <!> nbsp; <!> lt; img src = <!> quot; images / myPicture.png <!> quot; <!> gt;
   <!> lt; / div <!> gt;

上記のコメントを追加しますが、50人の担当者はいません。 IE6でdoctypeの前に何かがあると、それがQuirksモードに送信されることを覚えているようです。それが問題を引き起こしている可能性があります

<?xml version='1.0' encoding='UTF-8'?>

これを削除してみる価値があるかもしれません。

絶対配置を削除できる場合は、tdでtext-align:centerを使用します。

絶対配置を削除できず、すべての画像が同じサイズである場合、左をtd / 2-img_width / 2に設定します。したがって、幅200ピクセルのtdに幅80ピクセルの画像がある場合は、

を使用します
#slideshow img {position:absolute; left:60px; opacity:0; z-index:8;}

絶対配置を削除できず、画像のサイズが異なる場合、javascriptの左の値を計算する必要があります。

IEは不透明度をサポートしていません。代わりにfilter: alpha(opacity = 50);を使用します。

標準

 div#slideshow {
    margin : 0 auto;
    width : 200px;
}

およびfor(old?)ie

td {
    text-align : center;
}

ieは<!> quot; like <!> quot;ではありません。自動マージン ただし、インライン要素だけでなくブロック要素(div)も中央に配置します

また、tdのdisplayプロパティの標準値を隠しません。つまり、table-cellを使用せず、text-alignはインライン要素内では機能しないため、別のdivでラップする方法があります:

<td>
<div id="slideshow_container">
<div id="slideshow">
...
</div>
</div>
</td>

and css

div#slideshow {
    margin : 0 auto;
    width : 200px;
}
div#slideshow_container {
    text-align : center;
    width : 100%;
    height : 100%;
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top