tags:

views:

83

answers:

2

Hi,

I have a div wrapped around a couple span's and using JS to make the span's fade in and out (for testimonials). Everything works, except I can't get the span text to center within the div. Here's the relevant code:

HTML -

<div class="slideshow">
    <span style="font-size:12px; color:#333333; font-family:Lucida Grande,Lucida Sans Unicode,Calibri,Arial,sans-serif;">Hi</span>
    <span style="font-size:12px; color:#333333; font-family:Lucida Grande,Lucida Sans Unicode,Calibri,Arial,sans-serif;">Goodbye</span>
</div><br />

And here's the slideshow CSS -

.slideshow {
width:940px;
height:64px;
background-image:url(../images/quotes.png);
}

Can anyone tell me what I'm doing wrong? Oh and yes I've tried text-align:center; with no luck.

[Edit] I'd like to add I've also tried adding in !important with no change.

+2  A: 

You need to apply text-align: center to the containing <div>, and not do anything that causes the <span>s to be rendered as a block (such as floating them). Absolute positioning would also pull elements out of normal flow so they wouldn't be influenced by text-align.


Update : Now a working example has been provided, we can see what is going on. The span elements are being absolutely positioned by the plugin:

$slides.css({position: 'absolute', top:0, left:0}).hide()

Looks like you will need to rethink the layout logic the plugin is using.

David Dorward
What else would cause `<span>` to be rendered as a block? I'm not floating anything. And I tried adding `text-align: center` to `.slideshow` ... no luck.
Matt Untsaiyi
Did he not say he tried that? And he is not floating anything. And why the upvotes???
Yehonatan
@Matt Untsaiyi you can use the css property "display: block" to display almost any element as a block element. This is a very common thing to do. If you want to make double sure the spans are displayed inline you could add "display: inline", but normally this should not be neccessary.
thomasmalt
@Yehonatan — no, he said he was using `text-align: center`, but not where, and he didn't say he wasn't floating anything. Whatever is causing the problem isn't in the code that is being shown.
David Dorward
@David He didn't say that he had his computer plugged in either.
Yehonatan
He didn't say it was absolutely positioned either — but it was.
David Dorward
No need to argue guys. It was the JS thank you Dorward.
Matt Untsaiyi
I'm stuck. I've tried adding `left:50%` to `$slides.css` and although it centers, the slide is broken and all my `<span>`s appear at once.
Matt Untsaiyi
+4  A: 

I tested your code and just added

.slideshow {
    text-align: center; 
}

and the text centered just fine.

Maybe you've got a typo, or a css definition somewhere else that overrides your text-align?

Edit

I did an "inspect-element" of the fiddle posted and here are the css definitions i got:

<div class="slideshow" style="position: relative; ">
    <span style="font-size: 12px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', 'Lucida Sans Unicode', Calibri, Arial, sans-serif; position: absolute; z-index: 3; top: 0px; left: 0px; display: block; width: 13px; height: 15px; opacity: 0.275808; ">Hi</span>
    <span style="font-size: 12px; color: rgb(51, 51, 51); font-family: 'Lucida Grande', 'Lucida Sans Unicode', Calibri, Arial, sans-serif; position: absolute; top: 0px; left: 0px; display: block; width: 52px; height: 15px; z-index: 2; opacity: 0.724192; ">Goodbye</span>
</div>

element.style {
    color: #333;
    display: block;
    font-family: 'Lucida Grande', 'Lucida Sans Unicode', Calibri, Arial, sans-serif;
    font-size: 12px;
    height: 15px;
    left: 0px;
    opacity: 1;
    position: absolute;
    top: 0px;
    width: 52px;
    z-index: 3;
}

I would say the "display: block" is your problem.

thomasmalt
http://jsfiddle.net/sGA45/ he's right.
Chouchenos
It, perhaps obviously, should work. But it doesn't in his situation (which I think is the point @David Dorward makes in his comment to his own answer). It's also why I asked the OP to post a live demo somewhere, so we could try and find out what it is, elsewhere, that's causing his problem(s).
David Thomas
Yes I tested the above code and it worked fine, so it has to be somewhere else. I've included a fiddle link (http://jsfiddle.net/v7EuF/) that reproduces the same problem.
Matt Untsaiyi
If that element is set to `display: block;` then adding `margin: 0 auto;` should probably work, rather than trying to fight the plug-in that appears to be adding the extra styles.
David Thomas
I tried adding `margin` `display:inline` and neither worked, but Dorward pointed something out about my plugin. Gonna have a looksie, might fall asleep though it's late (err, early). Thanks guys.
Matt Untsaiyi