views:

350

answers:

4

I have a simple button (as shown below) on which I need to display two pictures, one on either side of the button text. Im battling to create the CSS that will work in both Firefox and Internet Explorer! (the button images are coming from a JQuery UI skin file)

CSS

button div{
    width:16px;
    height:16px;
    background-image: url(images/ui-icons_d19405_256x240.png);      
}

button div.leftImage{
    background-position: -96px -112px;
    float: left;
}

button div.rightImage{
    background-position: -64px -16px;
    float: right;
}

HTML

<button><div class="leftImage"></div><span>Button Text</span><div class="rightImage"></div></button>

Preview

Firefox

alt text

Internet Explorer 8

alt text

+1  A: 

The code works perfectly if you change the class 'leftImage' to 'leftPic' and 'rightImage' to 'rightPic'. Before you ask questions like this, it'd be smart to check things like that. ;)

Arianna
+1 for correctness in both elements, though a touch harsh as we've all missed this "obvious" mistakes ;)
Amadiere
@Arianna: (Code in question has been corrected) If you try the example out you'll see that in IE and Firefox the result it totally different and in both cases still incorrect. Perhaps something to do with the default display style (block/inline) of the elements I have used?
Jimbo
A: 

try resetting the button css.

button{
border:none;
background:none;
padding:0;
margin:0;
}

And add a space inside an empty DIV see if it works.

<button><div class="leftPic">&nbsp;</div><span>Button Text</span><div class="rightPic">&nbsp;</div></button>
keithics
A: 

I would use spans not divs for the image containers, since you seem to want the images to appear inline. Using floated divs is just too complex.

In fact, you could probably simplify things further by applying one background image to the button itself, and one to the button-text span, and removing the other two containers altogether.

Another alternative is to simply add the images in as img tags.

graphicdivine
I tried using SPAN elements for the images but (cleverly) Firefox hides them all of a sudden! Very strange - I love to hate CSS :'(
Jimbo
You'll need to give them some comntent (like an  ) and/or specify an absolute width/height to hold them open. Empty elements collapse.
graphicdivine
SPAN elements with a width and height of 16px (same as the DIV used) and with   still werent being displayed in Firefox. I ended up using EM elements (see below). IMG tags cant use the JQuery skin image file as required for these buttons so I didnt even try to use them, although Im sure they WOULD work.
Jimbo
+1  A: 

Here is how to do it

The Theory

Block elements (like DIV) although displayed in order of creation, will position themselves adjacent to the previous element or when short of space, on the next line. Because we dont want to give the button a width (we want the button to be automatically sized based on the content of the button) the block elements continued to appear on the next line (see IE8 image in the question above). Using white-space:nowrap forces inline elements (like SPAN and EM) to be displayed on the same line, but is ignored by block elements, hence the solution below.

CSS

button{
    margin: 0px;
    padding: 0px;
    font-family:Lucida Sans MS, Tahoma;
    font-size: 12px;
    color: #000; 
    white-space:nowrap;
    width:auto;
    overflow:visible;
    height:28px;
}

button em{
    vertical-align:middle;
    margin:0 2px;
    display:inline-block;
    width:16px;
    height:16px;
    background-image: url(images/ui-icons_3d3d3d_256x240.png);      
}

button em.leftImage{
    background-position: -96px -112px;
}

button em.rightImage{
    background-position: -64px -16px;
}

HTML

<button><em class="leftImage"></em>Button<em class='rightImage'></em></button>

The Result

Internet Explorer 6, 7, 8 and Firefox 1.5, 2, 3

alt text

Jimbo