Forum Moderators: not2easy

Message Too Old, No Replies

Tab appearance using background images

Floated div containing 3 floated paragraphs

         

Mike521

5:49 pm on Jan 23, 2009 (gmt 0)

10+ Year Member



I'm trying to create a "tab" appearance for some text links to appear at the top of a page. The links will vary in width so I don't want the setup to need a hardcoded width on each element.

I can't use a static background image since the width of the links can vary. So my solution was to break the "tab" up into 3 parts:

left -> paragraph, 8px wide, has the left side of the tab as a background image
middle -> paragraph, any width, has a repeating background image of the middle portion of tab
right -> paragraph, 8px wide, has the right side of the tab

Since these are all paragraphs they wouldn't appear side by side, so I had to float them to the right. It worked fine but they could be separated from each other. For example if the browser was shrunk and they got squeezed -- sometimes the right paragraph would jump down a line, leaving the rest of the tab behind.

My solution to that problem was to surround all 3 paragraphs in a div, and float that to the right. That's where the problem is now. Almost every browser I view this in has a different interpretation of how it should look.

firefox 2 -> thinks the tabs should be stacked. It thinks some part of each tab extends the width of the screen
IE7 -> thinks the tabs should be stacked
firefox 3 -> thinks the tabs should be side by side (which is what I want)
google chrome -> thinks the tabs should be side by side

Here's a code sample below. Anyone tackle a similar problem before, or see an error here? thanks in advance


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head></head>
<body>
<!-- start tab -->
<div style="float: right;">
<p style="float: right; margin: 0px; background-image: url(images/tab-right.png); width: 8px; height: 22px;"></p>
<p style="float: right; margin: 0px; background-image: url(images/tab-middle.png); height: 22px;">
Content here: middle
</p>
<p style="float: right; margin: 0px; background-image: url(images/tab-left.png); width: 8px; height: 22px;"></p>
</div>
<!-- end tab -->
<!-- start tab -->
<div style="float: right;">
<p style="float: right; margin: 0px; background-image: url(images/tab-right.png); width: 8px; height: 22px;"></p>
<p style="float: right; margin: 0px; background-image: url(images/tab-middle.png); height: 22px;">
Content here: middle
</p>
<p style="float: right; margin: 0px; background-image: url(images/tab-left.png); width: 8px; height: 22px;"></p>
</div>
<!-- end tab -->
</body>
</html>

simonuk

1:37 pm on Jan 24, 2009 (gmt 0)

10+ Year Member



It would be easier to create a menu using the list-item method. You have a lot of control but what's even better is you don't have to indivually style everything.

Google css menus for some excellent pre-built templates.

Mike521

9:55 pm on Jan 26, 2009 (gmt 0)

10+ Year Member



It doesn't matter if the container is a list item or a div, the problems are still the same. Nevertheless I tried it with list items and it was the same, code is below. If you look at that code in google chrome it'll be how I want (tabs are side by side). If you look at it in firefox 2, they'll be stacked.

The problems are:

1. I need a way to "stretch" a background image across the container - hence the left, middle, right approach
2. I need a way to keep the left, middle, and right attached to each other that works in all browsers


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head></head><body>
<ul>
<!-- start tab -->
<li style="display: block; float: right;"><p style="float: right; margin: 0px; background-image: url(images/tab-right.png); width: 8px; height: 22px;"></p>
<p style="float: right; margin: 0px; background-image: url(images/tab-middle.png); height: 22px;">
Content here: middle
</p>
<p style="float: right; margin: 0px; background-image: url(images/tab-left.png); width: 8px; height: 22px;"></p></li>
<!-- end tab -->
<!-- start tab -->
<li style="display: block; float: right;"><p style="float: right; margin: 0px; background-image: url(images/tab-right.png); width: 8px; height: 22px;"></p>
<p style="float: right; margin: 0px; background-image: url(images/tab-middle.png); height: 22px;">
Content here: middle
</p>
<p style="float: right; margin: 0px; background-image: url(images/tab-left.png); width: 8px; height: 22px;"></p></li>
<!-- end tab -->
</ul>
<p style="clear: both">&nbsp;</p>
<!-- start tab -->
<div style="float: right;">
<p style="float: right; margin: 0px; background-image: url(images/tab-right.png); width: 8px; height: 22px;"></p>
<p style="float: right; margin: 0px; background-image: url(images/tab-middle.png); height: 22px;">
Content here: middle
</p>
<p style="float: right; margin: 0px; background-image: url(images/tab-left.png); width: 8px; height: 22px;"></p>
</div>
<!-- end tab -->
<!-- start tab -->
<div style="float: right;">
<p style="float: right; margin: 0px; background-image: url(images/tab-right.png); width: 8px; height: 22px;"></p>
<p style="float: right; margin: 0px; background-image: url(images/tab-middle.png); height: 22px;">
Content here: middle
</p>
<p style="float: right; margin: 0px; background-image: url(images/tab-left.png); width: 8px; height: 22px;"></p>
</div>
<!-- end tab -->
</body></html>

swa66

9:02 am on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you're making it complex on yourself by not using a technique known as sliding doors [google.com].

Basically:

  • make 2 images, not 3 (a left+middle and a right one) or a right+middle one and a left one)
    Added bonus: use just one image (really wide)
  • use as html only this:

    <ul>
    <li><a href="#">anchor</a></li>
    <li><a href="#">anchor</a></li>
    </ul>

    no other elements are needed
  • apply padding so the <li> sticks out on one side of the <a>
  • apply your images to the <li> and the <a> so they seem to slide into one another and form your button/tab/whatever you want it to look like
  • float the buttons or let them be in-line (much harder to get cross browser)

Look at some examples to understand it fully, it's a rather basic technique that all should master till we get CSS3.

Mike521

3:43 pm on Feb 11, 2009 (gmt 0)

10+ Year Member



thanks! I think I'd done something like that once before and completely forgot about it. This will be much better than what I'm doing now, I'll start tinkering with it