私の問題は、タイトルよりも少し複雑です。申し訳ありませんが、具体的にする方法がわかりません...複雑なCSSイメージセンタリングヘルプ?
私はウェブサイト上で作業していますが、サムネイルを表示する部分があります。サムネイルのサイズが一致していません。 (これはサムネイルがありますので、ばかばかしいと思います。)いいえ、同じ次元で作成する方法はありません。私はこの問題を解決するためにHTML + CSS構造を作成しました。画像がアスペクト比を維持しながら小さければ、コンテナに合わせて伸びません。
残りの唯一の問題は、画像を中央に配置することです。 "0 auto"または "auto 0"にマージンを設定することは役に立たないので、私は画像を配置するためにmultiple containers and setting the marginsを設定しようとしました。これも機能しません:120x80の内側のコンテナに120x120の画像を置き、コンテナの上下の余白を-50%に設定すると、余白は両方とも-60pxになります。これは修正できますか?それともイメージを中心にする別の方法がありますか?
私は任意の提案に開放されています!
HTML:
<div id="roll">
<div class="imgfix">
<div class="outer">
<div class="inner">
@if (ImageDimensionHelper.WhereToAlignImg(item.Width, item.Height, 120, 82) == ImgAlign.Width)
<!-- ImageDimensionHelper tells me if the image should fit the
container with its width or height. I set the class of the img
accordingly. -->
{
<img class="width" src="@Url.Content(item.URL)" alt="@item.Name"/>
}
else
{
<img class="height" src="@Url.Content(item.URL)" alt="@item.Name"/>
}
</div>
</div>
</div>
</div>
CSS:
.imgfix{ overflow:hidden; }
.imgfix .outer { width:100%; height:100%;}
.imgfix .inner { width:100%; height:100%; margin-top:-50%; margin-left:-50%; }
/*This div (.inner) gets -60px for both margins every time, regardless of the size of itself, or the image inside it*/
#roll .imgfix { width:120px; height:82px; border: 1px #5b91ba solid; }
#roll .imgfix .outer { margin-top:41px; margin-left:60px; }
/*since I know specificly what these margins should be, I set them explicitly, because 50% got the wrong size.*/
#roll .imgfix img.width { width:120px; height:auto; margin: auto 0; }
#roll .imgfix img.height { height:82px; width:auto; margin: 0 auto; }
:-)それはあなたの問題のために良いスタートだ願ってい – sandeep
あなたは純粋なCSSのセンタリングをしたいか、あなたは反対ではないんそれを行うにはjsを使用しますか?私はあなたにjQueryのソリューションを提供することができます(おそらく次元のプラグインの拡張子) –
私は可能な限り、CSSのみに固執したいと思います。 – Tenshiko