带有图片背景的 li 元素中的居中文本

Center text in li element with a picture background

如何在具有图片背景的 li 元素中将文本 horizontally/vertically 居中?

例如:

到目前为止,这是我的代码:

#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 这里:http://jsfiddle.net/womyhrd8/3/

您可以使用 text-align:center; 进行水平对齐,使用 line-height 进行垂直对齐

希望这DEMO对您有所帮助

您可以使用 CSS 仅使用以下技巧与 table 模拟(我更改了文本大小和颜色以使其更明显):

/*
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: 24px;
    color: #000000;
    background-color: rgb(240, 240, 240);
    color: white;
    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");
}
.tablediv{
    display: table;
    width: 100%;
    height: 100%;
}
.trdiv{
    display: table-row;
}
.tddiv{
    display: table-cell;
    text-align: center;
    vertical-align: middle;    
}
<div id="frame" class="frame bag_item">
    <ul class="slidee">
        <li id="ad1" class="ad">
            <div class=tablediv>
                <div class=trdiv>
                    <div class=tddiv>THIS IS TEXT</div>
                </div>
            </div>
        </li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
</div>

您可以做的最简单的事情就是添加伪元素以帮助垂直对齐:

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

为什么会这样?因为文本内容和 :after 伪元素成为兄弟内联元素,并且由于最后一个是 100% 高度并且具有 vertical-align: middle; 它会自动强制文本也对齐。

查看下面的演示。

/*
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>