Center of the text in the li element with a background of the image

advertisements

How do you center the text horizontally/vertically in an li element that has a picture background?

For example:

This is my code so far:

#frame {
    height: 100%;
}
.frame {
    width: 100%;
    height: 100%;
    padding: 0;
    overflow-y: hidden;
}
.frame .slidee {
    margin: 0;
    padding: 0;
    height: 100%;
    list-style: none;
}
.frame .slidee li {
    float: left;
    margin: 0 5px 0 0;
    padding: 0;
    width: 300px;
    height: 98%;
    border: solid #CCC 1px;
}
.ad {
    background-position: center;
    background-size: 100% auto;
}
#ad1 {
    background: url("http://blogs.arts.ac.uk/csm/files/2014/03/fashion-revolution.jpg");
}
<div id="frame" class="frame bag_item">
    <ul class="slidee">
        <li id="ad1"></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>

Fiddle here: http://jsfiddle.net/womyhrd8/3/


Simplest thing you can do is to add pseudo-element for help with vertical aligning:

#ad1:after {
    content: '';
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}

Why it works like this? Because text content and :after pseudo-element becomes sibling inline elements, and since the last is of 100% height and has vertical-align: middle; it automatically forces text to align too.

Check the demo below.

/*
Theme Name:
Theme URI:
Description:
Version: 1.0
Author:
Author URI:
*/
 html {
    color: #444;
    font-size: 100%;
    background: #f7f7f7 url(../images/bg.png) repeat center top;
    height: 100%;
}
body {
    line-height: 1;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    height: 100%;
    font-size: 62.5%;
    color: #000000;
    background-color: rgb(240, 240, 240);
    color: #777;
    font-family: sans-serif;
}
/*     Layout container Style       */
 #main {
    padding-top: 6em;
    padding-bottom: 3em;
    height: 82%;
    width: 97%;
    padding-left: 1em;
}
#layout, #menu, #frame, .slidee, .menu-link {
    -webkit-transition: all 0.3s ease-in-out;
    -moz-transition: all 0.3s ease-out;
    -ms-transition: all 0.3s ease-out;
    -o-transition: all 0.3s ease-out;
    transition: all 0.3s ease-in-out;
}
.pure-img-responsive {
    max-width: 100%;
    height: auto;
}
#frame {
    height: 100%;
}
.frame {
    width: 100%;
    height: 100%;
    padding: 0;
    overflow-y: hidden;
}
.frame .slidee {
    margin: 0;
    padding: 0;
    height: 100%;
    list-style: none;
}
.frame .slidee li {
    float: left;
    margin: 0 5px 0 0;
    padding: 0;
    width: 300px;
    height: 98%;
    border: solid #CCC 1px;
}
.ad {
    background-position: center;
    background-size: 100% auto;
}
#ad1 {
    background: url("http://blogs.arts.ac.uk/csm/files/2014/03/fashion-revolution.jpg");
    text-align: center;
    font-size: 20px;
}
#ad1:after {
    content: '';
    display: inline-block;
    vertical-align: middle;
    height: 100%;
}
<div id="frame" class="frame bag_item">
    <ul class="slidee">
        <li id="ad1" class="ad">
            SOME TEXT CONTENT
        </li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>