3
<div style="background: url('theImages/talentQuote.png') no-repeat center; background-size: 100% 100%; min-height: 125px; min-width: 125px; text-align: center; vertical-align: middle;">
    <span style="color: #FFF; font-weight: bold;"><xsl:value-of select="txtQuote" /></span>
</div>

Displays this:

enter image description here

How can I modify the CSS, so the quote is always centered within the background image

Si8
  • 9,141
  • 22
  • 109
  • 221

5 Answers5

5

Try this:

<div class="bubble">
    <p>blablabla</p>
</div>

.bubble { 
  position: absolute; 
  background: #cccccc;
  width: 135px; 
  height: 84px; 
  display: table; 
}

.bubble p {
  display: table-cell; 
  vertical-align: middle; 
  text-align: center; 
}

here is a fiddle: http://jsfiddle.net/hLwzymmL/

Caio Kawasaki
  • 2,894
  • 6
  • 33
  • 69
  • Where do I add the padding around so the text doesn't overflow on to the DIV? – Si8 Oct 29 '14 at 13:17
  • inside .bubble p{} / something like that: .bubble p { display: table-cell; vertical-align: middle; text-align: center; padding: 0 20px; } – Caio Kawasaki Oct 29 '14 at 13:24
2

It depends whether you want to center horizontally or vertically:

Horizontally you should give these styles to the object:

For text only:

#parent {
text-align: center;
}

For objects other than text:

#child {
margin-left: auto;
margin-right: auto;
}

or:

#child {
margin-left: 0 auto;
}

Vertically it's a bit more difficult, you have to set display: table to the parent and display: table-cell to the child, so that you can set the style vertical-align: middle to the child.

#parent {
display: table;
}

#child {
display: table-cell;
vertical-align: middle;
}

JSFiddle demo

Daan
  • 2,680
  • 20
  • 39
1

You can simply set your outter container to have display:table; and your inner container to have display:tabe-cell;

div{
    display:table;
    text-align: center; 
}

span{
    display:table-cell;
    vertical-align: middle;
}

vertical-align: middle; only works on table elements.

jleggio
  • 1,266
  • 1
  • 9
  • 16
1

demo - http://jsfiddle.net/victor_007/hLwzymmL/1/

.bubble p {
    position:absolute;
    top:50%;
    left:50%;
    transform: translate(-50%, -50%);
}
Vitorino fernandes
  • 15,794
  • 3
  • 20
  • 39
1

Do it like this:

HTML markup:

<div class="wrapper">
    <span class="middleelement">This is some text..</span>
</div>

CSS:

div.wrapper {
background: #008000;
background-size: 100% 100%;
text-align: center;
display: table;
width: 100%;
height: 285px;
}
.middleelement {
color: #FFF;
font-weight: bold;
display: table-cell;
vertical-align: middle;
}

Se the demo here: http://jsfiddle.net/e16uhcrz/2/

GeniusDesign
  • 499
  • 7
  • 25