簡體   English   中英

在相對位置父級中垂直居中的絕對位置div-在Chrome中工作,但在Safari中居中到主體等?

[英]Vertically centered absolute position div inside relative position parent - Works in Chrome, but centers to body in Safari etc?

我試圖將一些文本垂直居中放置在鼠標懸停在圖像上。 我已經找到了可以在Mac(10.7.2)上與chrome(15.0.874.106)一起使用的解決方案,但是在Safari(5.1.1)中似乎有問題,奇怪的是,它們都基於webkit。 我相信在Firefox中也有同樣的問題。

文本在chrome中相對於父div垂直居中,但在Safari中似乎居中於正文或窗口。 我做錯了什么,還是有人有更好的解決方案?

jsbin: http ://jsbin.com/iceroq

CSS:

.content {
    width: 300px;
    position: relative;
}
.content-text {
    width: 100%;
    height: 100%;
    background: black;
    position: absolute;
    top: 0;
    left: 0;
    text-align: center;
    display: none;
}

HTML:

<div class="content">
    <div class="content-image">
        <img src="http://placehold.it/300x500/E01B4C" />
    </div>
    <div class="content-text">
        <a href="http://www.google.com">Google</a>
    </div>
</div>
.content-text a {
    color: white;
    position: relative;
    top: 50%;
    margin-top: -0.5em;
}

JS:

$(document).ready(function() {
    $('.content').hover(

    function() {
        $(this).children('.content-text').show();
    }, function() {
        $(this).children('.content-text').hide();
    });
});

我編輯了您的jsbin: http ://jsbin.com/iceroq/3

所做的所有修改都是CSS對.content-text a更改。 絕對確定鏈接的位置並為其設置height可以讓您知道要為其指定的margin-top (高度的一半)。

.content-text a {
  display: block;
  width: 100%;
  height: 20px;
  position: absolute;
  top: 50%;
  margin-top: -10px;
  color: white; 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM