2011-05-12 66 views

回答

0

我用这个溶液

$('.wp-caption-text').each(function() { 
this.style.position = 'absolute'; 
this.style.top = $(this).parent().height()/2 + 'px'; 
this.style.marginTop = -$(this).height()/2 + 'px'; 
this.style.left = ($(this).parent().width() - $(this).width())/2 + 'px'; 

});

0

做最简单的事情就是增加一个leftwidth属性包含文本。但是,这确实假设文本长度有限。

#rugs #content .rugsAll div p { 
    color: #FFFFFF; 
    font-size: 24px; 
    left: 35%; 
    position: absolute; 
    text-align: center; 
    z-index: 4; 
    top: 40%; 
    width: 20%; 
} 

如果您有不同长度的标题。然后,您将需要jQuery动态设置lefttop属性以基于图像大小对其进行居中。

+0

是啊这就是我认为,我会有不同长度的段落,所以需要jquery来设置左侧和右侧 – sat 2011-05-12 16:20:46

0

sat,
如果您将<p>级别的宽度设置为100%,那么text-align:center会将文本置于水平中心。

垂直对齐更复杂。您应该将您的<p>包装在容器中,并将vertical-align:middle包装在容器中。

CSS:

.container { 
    width: 100%; 
    vertical-align:middle; 
} 

p .wp-caption-text { 
    position: relative; 
    display: block; 
    z-index: 40; 
    font-size: 24px; 
    text-align: center; 
    width: 100%; 
    color: #fff; 

}

HTML

<div id="attachment_249" class="wp-caption alignnone" style="width: 319px"> 
     <img class="animal new" title="Rug-12" src="http://satbulsara.com/luke-irwin/wp-content/uploads/2011/05/Rug-121.jpg" alt="" width="309" height="453" /> 
     <div class="container"> 
      <p class="wp-caption-text"> 
      testing 8 
      </p> 
     </div> 
    </div> 

我相信这样的事情会工作。玩弄它,但我认为它会。