简体   繁体   中英

Responsive Text Aligned Center on Image

I have created this layout for my blog posts and each post automatically gets posted there. I am quite happy with the setup I have, but I cannot for the life of me get the text centered (vertically) on the image (it already horizontally centered). Just wondering how others would approach this.

I have completely laid out the HTML and CSS here in a codepen. If you are interested in tweaking the code, I would appreciate if you based it off of the current css classes in the codepen. :)

Thanks in advance for your help and recommendations!

HTML:

<div id="blog-posts">

<div class="blog-post">

<a href="http://www.ericshio.com/2016/08/07/test-2/">

<img width="5472" height="3648" src="http://www.ericshio.com/wp-content/uploads/2016/08/IMG_0303.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="IMG_0303" srcset="http://www.ericshio.com/wp-content/uploads/2016/08/IMG_0303.png 5472w, http://www.ericshio.com/wp-content/uploads/2016/08/IMG_0303-300x200.png 300w, http://www.ericshio.com/wp-content/uploads/2016/08/IMG_0303-768x512.png 768w, http://www.ericshio.com/wp-content/uploads/2016/08/IMG_0303-1024x683.png 1024w" sizes="(max-width: 5472px) 100vw, 5472px" scale="0">            

<h3>Test 2</h3>

</a>

</div>

<div class="blog-post">

<a href="http://www.ericshio.com/2016/08/07/hey-johnson/">

<img width="5472" height="3648" src="http://www.ericshio.com/wp-content/uploads/2016/08/macbook.png" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" alt="macbook" srcset="http://www.ericshio.com/wp-content/uploads/2016/08/macbook.png 5472w, http://www.ericshio.com/wp-content/uploads/2016/08/macbook-300x200.png 300w, http://www.ericshio.com/wp-content/uploads/2016/08/macbook-768x512.png 768w, http://www.ericshio.com/wp-content/uploads/2016/08/macbook-1024x683.png 1024w" sizes="(max-width: 5472px) 100vw, 5472px">            

<h3>Hey Johnson</h3>

</a>

</div>

</div>

CSS:

#blog-posts {
     text-align: center;
     display: inline-block;
     max-width: 100%;
}

.blog-post {
    width: 40%;
    height: auto;
    display: inline-block;
    position: relative;
    margin: 0.2em;
    overflow: hidden;
}

.blog-post h3 {
    font: 1em Cabin Sketch;
    color: white;
    width: 100%;
    height: 100%;
    line-height: 100%;
    text-transform: uppercase;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 0;
    padding: 0;
    font-size: 2em;
    background-color: rgba(0,0,0,0.4);
    opacity: 0;
    -webkit-transition: opacity 0.6s;
    -moz-transition: opacity 0.6s;
    transition: opacity 0.6s;
    vertical-align: middle !important;
}

.attachment-post-thumbnail {
   position: relative;
    width: 100%;
    display: inline-block;
    height: auto;
}

.blog-post:hover h3,
.blog-post h3:hover,
.blog-post .attachment-post-thumbnail:hover {
    opacity: 1;
}

.blog-post:hover .attachment-post-thumbnail {
   -webkit-filter: blur(1px);
    opacity: 1;
}

I set padding-top to 30%. Check out the codepen:

http://codepen.io/anon/pen/mEQVZy

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM