SORU
1 EYLÜL 2011, PERŞEMBE


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
5 EYLÜL 2011, PAZARTESİ


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:

  1. 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ın

    Two aligned blocks

  2. Sabit yükseklik (px, em veya diğer mutlak birimi) ile bir blok varsa, % iç blok yüksekliği ayarlayabilirsiniz.

  3. Yani, sabit yüksekliğe sahip bir blokta height: 100% ile inline-block ekleme inline-block başka bir öğe (sizin durumunuzda<img/>) dikey yanına uyum sağlayacak.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Andrea Lewis

    Andrea Lewis

    14 Mart 2013
  • BachelorsPadTv

    BachelorsPad

    17 Ocak 2012
  • Rhapsody

    Rhapsody

    7 ŞUBAT 2008