Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm having trouble vertically aligning some h2 text in the middle of a block.

Basically I have a number of images with different heights all the same width (300px) with varying amounts of text that that could be over a number lines which appear ontop of an image on a hover.

What I would like to do is vertically align this text in the middle...

hes a demo
http://jsfiddle.net/wn3Kj/2/

<div class="post-thumb">            
      <a href="http://eclectivism.com/text/">
          <h2 class="entry-title">Text text  text  text  text  text  text  text  text text text text text text text text text text text text text text text text</h2>
          <img src="http://eclectivism.com/wp-content/uploads/2013/09/space-doodle-01-580x7041.jpg" class="attachment-blog-large wp-post-image" />
      </a>
</div>

CSS

.post-thumb {
    margin: 0;
    padding: 0;
    border: 0;
    outline: 0;
    font-size: 100%;
    vertical-align: baseline;
    background: transparent; }

a {position: relative; display: inline-block; }

h2 {
    opacity: 0;
    padding-right: 20px;
    padding-left: 20px;
    padding-bottom: 0px;
    padding-top: 50%;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    background: rgba(214,238,219,0.8);
    color: #fff;
    text-decoration: none;
    text-shadow: 0px 1px 5px rgb(63, 97, 56);
    text-align:center;
    display:block;
}

img {
    width: 300px;
}

a:hover  h2 {opacity:1}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
129 views
Welcome To Ask or Share your Answers For Others

1 Answer

Solution without JS.

Add one extra element into HTML for background:

<div class="post-thumb">            
      <a href="http://eclectivism.com/text/">
           <span class="post-thumb-highlight"></span> <!-- HERE -->
           <h2 class="entry-title">Text text  text  text  text  text  text  text  text text text text text text text text text text text text text text text text</h2>
          <img src="http://eclectivism.com/wp-content/uploads/2013/09/space-doodle-01-580x7041.jpg" class="attachment-blog-large wp-post-image" />
       </a>
 </div>

with CSS:

.post-thumb-highlight {
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    background: rgba(214,238,219,0.8);
    opacity: 0;
}

h2 {
    opacity: 0;
    text-decoration: none;
    display: inline-block;
    color: #fff;
    text-decoration: none;
    text-shadow: 0px 1px 5px rgb(63, 97, 56);
    text-align:center;
    width: 300px; /* same width as IMG */
    vertical-align: middle;
    margin-right: -300px; /* negative value of IMG width */
    position: relative;
    font-size: 24px;
}

img {
    width: 300px;
    vertical-align: middle;
}

a:hover h2, a:hover .post-thumb-highlight {opacity:1}

Check out demo - http://jsfiddle.net/3epgP/1/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...