Reputation: 13206
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/
Upvotes: 2
Views: 124
Reputation: 193271
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>
Upvotes: 2
Reputation: 2924
You can do it with CSS only using the following trick with table simulation (I changed the text size and color to make it more visible):
/*
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>
Upvotes: 2
Reputation: 1404
you can use text-align:center;
for horizontal align and line-height
for vertical align
hope this DEMO helps
Upvotes: 0