Nasıl dikey olarak div içine bir resim hizalamak için
Nasıl düzgün bir dikey hizalama .frame
öğe: bu resim için
<div class="frame" style="height: 25px;">
<img src="http://jsfiddle.net/img/logo.png" />
</div>
.frame
'nın yüksekliği sabit ve görüntünün yüksekliği bilinmiyor. Eğer tek çözüm buysa .frame
yeni öğeler ekleyebilirsiniz. ≥7, Y, Gecko YANİ bunu yapmaya çalışıyorum.
Bu jsfiddle buraya bakın: http://jsfiddle.net/4RPFa/61/
CEVAP
Bildiğim kadarıyla (ve en iyi tarayıcı çapraz) tek yolu, her iki öğe üzerinde height: 100%
vertical-align: middle
inline-block
bir yardımcı kullanın.
Bir çözüm var: http://jsfiddle.net/kizu/4RPFa/4570/
.frame {
height: 25px; /* equals max image height */
width: 160px;
border: 1px solid red;
white-space: nowrap;
text-align: center; margin: 1em 0;
}
.helper {
display: inline-block;
height: 100%;
vertical-align: middle;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=250 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=25 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=23 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=21 />
</div>
<div class=frame>
<span class="helper"></span><img src="http://jsfiddle.net/img/logo.png" height=19 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=17 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=15 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=13 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=11 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=9 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=7 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=5 />
</div>
<div class=frame>
<span class="helper"></span>
<img src="http://jsfiddle.net/img/logo.png" height=3 />
</div>
Veya, eğer istemiyorsan yapmak zorunda ekstra eleman olarak modern tarayıcılar ve umursamaz kullanarak IE ifadeler kullanabilirsiniz bir pseudo-element ve eklemek için IE kullanarak uygun bir İfade, çalışan sadece bir kez başına öğe, bu yüzden olmayacak, herhangi bir performans sorunları:
IE için :before
expression()
çözüm: http://jsfiddle.net/kizu/4RPFa/4571/
.frame {
height: 25px; /* equals max image height */
width: 160px;
border: 1px solid red;
white-space: nowrap;
text-align: center; margin: 1em 0;
}
.frame:before,
.frame_before {
content: "";
display: inline-block;
height: 100%;
vertical-align: middle;
}
img {
background: #3A6F9A;
vertical-align: middle;
max-height: 25px;
max-width: 160px;
}
/* Move this to conditional comments */
.frame {
list-style:none;
behavior: expression(
function(t){
t.insertAdjacentHTML('afterBegin','<span class="frame_before"></span>');
t.runtimeStyle.behavior = 'none';
}(this)
);
}
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=250 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=25 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=23 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=21 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=19 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=17 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=15 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=13 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=11 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=9 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=7 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=5 /></div>
<div class=frame><img src="http://jsfiddle.net/img/logo.png" height=3 /></div>
Nasıl çalışır:
Birbirlerine yakın
inline-block
iki öğe varsa,vertical-align: middle
ile böyle bir şey alacaksın yani diğer tarafına her hizalayınSabit yükseklik (
px
,em
veya diğer mutlak birimi) ile bir blok varsa,%
iç blok yüksekliği ayarlayabilirsiniz.- Yani, sabit yüksekliğe sahip bir blokta
height: 100%
ileinline-block
eklemeinline-block
başka bir öğe (sizin durumunuzda<img/>
) dikey yanına uyum sağlayacak.
Nasıl divs dikey olarak hizalamak için...
Dikey olarak hizalamak için nasıl <...
Nasıl dikey olarak tüm tarayıcılar içi...
Nasıl bootstrap ile resim ölü bir merk...
Öğeleri yatay veya dikey olarak ortala...