在图像中水平和垂直居中文本

时间:2014-06-26 19:47:27

标签: html css center

我想将照片上的文字更改为中心位置,包括水平和垂直。它目前以水平为中心,但不是垂直的。记住这是一个“悬停效果背景图像” - 一切。

HTML

<a href="portrait"> 
<div id="imagebox1" class="imagebox columns"> 
    <div id="image1">
        <span id="plus">Portrett</span>
    </div>
</div> 
</a>

<a href="nature">
<div id="imagebox3" class="imagebox columns">
    <div id="image3">
        <span id="plus">Natur</span>
    </div>
 </div> 
 </a>

<a href="various">
<div id="imagebox2" class="imagebox columns">
    <div id="image2">
        <span id="plus">Annet</span>
    </div>
</div> 
</a>

CSS

#imagebox1, #imagebox2, #imagebox3 { 
    float: left;
    background-repeat: no-repeat;
    margin-right: 0px;
}


#imagebox1 {
    background:url(../images/sample_image400.jpg);
    background-size: cover;
    margin-left: 0px;

}

#imagebox2 {
    background:url(../images/sample_image.jpg);
    background-size: cover;
}
#imagebox3 {
    background:url(../images/sample_image.jpg);
    background-size: cover;
}

#image1, #image2, #image3 {   
    background:rgba(255,255,255,.75);
    text-align:center;
    padding-top: 100%;
    opacity:0;
    -webkit-transition: all 0.3s ease-in-out;
}

#imagebox1:hover #image1 {
    opacity:1;
}
#imagebox2:hover #image2 {
    opacity:1;
}
#imagebox3:hover #image3 {
    opacity:1;
}

.images img {
    margin-left: 0px;
    padding-bottom:30px;
    padding-top:35px;
}

#imagebox1, #imagebox2, #imagebox3 {
    margin-bottom: 30px;
}

#plus {
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-weight: 200;
    color: #282828;
    font-size: 20px;
    text-transform: uppercase;
}

的jsfiddle

http://jsfiddle.net/8abCY/

2 个答案:

答案 0 :(得分:2)

好的......我认为您的HTML结构太复杂了,无法实现您的目标。

所以我简化了它。

JSfiddle DEmo

图片内容,所以它应该在HTML中,而不是CSS中的背景图片,只是

<强> HTML

<a href="portrait" class="imagebox"> 
    <img src="http://www.ginakorslund.no/images/sample_image400.jpg" alt=""/>      
    <span class="plus">Portrett</span>
</a>

注意我为更通用的类更改了很多ID。如果你愿意,你仍然可以给每个锚链接一个单独的ID,但这些类意味着CSS可以重复使用。

<强> CSS

.imagebox {
    position: relative; /* required */
    text-align: center;
    display: block;
}



.plus {
    position: absolute;

    top:50%; /* top and left values put the span's top left corner exactly */
    left:50%;  /* halfway down and across the screen but do **not** center it */

    -webkit-transform:translate(-50%, -50%);
    transform:translate(-50%, -50%); 
    /* move the span back exactly half it's own width and height */
    /* and now it's centered regardless of width & height */

    display: none; /* hide it until hovered */

    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-weight: 200;
    color: #282828;
    font-size: 20px;
    text-transform: uppercase;
    color:white;
}

.imagebox:hover .plus {
    display: block; /* show it when the link is hovered */
}

答案 1 :(得分:0)

尝试...

#plus {
    font-family: Gotham, "Helvetica Neue", Helvetica, Arial, sans-serif;
    font-weight: 200;
    color: #282828;
    font-size: 20px;
    text-transform: uppercase;
    position: relative;
    margin-top: auto;
    margin-bottom: auto;
}