在li元素的中心文本有图片背景

时间:2015-01-04 10:55:08

标签: javascript html css

如何在具有图片背景的li元素中水平/垂直居中文本?

例如:

enter image description here

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

#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>

在这里小提琴:http://jsfiddle.net/womyhrd8/3/

3 个答案:

答案 0 :(得分:2)

你可以使用CSS只使用下面的表格模拟技巧(我更改了文字大小和颜色以使其更加明显):

&#13;
&#13;
/*
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;    
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

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

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

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

查看下面的演示。

&#13;
&#13;
/*
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%;
}
&#13;
<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>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

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

希望DEMO帮助