2012-04-28 18 views
0

CSSとjQueryを組み合わせて、一連のオブジェクトを水平方向に配置しようとしています。 jQueryの部分は私が問題を抱えています。ここに私のコードはあります:jQuery:オブジェクトの水平方向の位置を中心に

上記のコードは、オブジェクトの幅のちょうど半分の負の余白を適用する必要があります。私は間違って何をしていますか? jQueryの使い方

+0

ドロップゼロ、とでは機能です! – adeneo

+0

ああ、クール。どのように見えるだろうか? – colindunn

答えて

2
$('.tooltip').each(function() { 
     $(this).css('margin-left', '-'+($(this).width()/2)+'px'); 
} 
1
.tooltip { 
    position: absolute; 
    width: 300px; // suppose 
    left: 50%; 
    margin-left: -150px; // half of the width 
} 

$('.tooltip').each(function() { 
     $(this).css('margin-left', '-' + $(this).width/2 + 'px'); 
} 
+0

これはうまくいくが、オブジェクトの幅はすべて異なる。 – colindunn

+0

@ colindunnnだからjqueryのソリューションを試すことができます – thecodeparadox

関連する問題