CSS - Replacing a simple HTML table

B

Bigus

Hi

[possible repost - didn't show up last time I posted it]

I want to do the equivalent of a 1-row, 2 column table in CSS, ie: a
coloured bar across the top of the web page with some text aligned to the
left of the bar and an image aligned to the right of the bar.

The tricky bit is that I want it so that if the user increases/decreases the
text size, using their browser, then the text and image remain vertically
centered on the bar.

The nearest I can get to this is with the code:

<div id="thebar">
Some text
<img src="myimage.jpg" width="50" height="30" border="0"
style="position:absolute;right:25px;" />
</div>

The bar expands and contracts with the text and so it remains centered,
however:

a) if you make the text larger the image stays at the top of the bar
b) if you make the text smaller the bar height becomes less than the height
of the image

Specifying a height to the bar stops it getting smaller than the image but
the text no longer vertically centres, and vertical-align: middle doesn't
seem to work in IE, so I cannot think of any solutions for both a) and b)

Any ideas?
 
T

Toby A Inkster

Bigus said:
The tricky bit is that I want it so that if the user increases/decreases the
text size, using their browser, then the text and image remain vertically
centered on the bar.

Try something like:

#thebar {
position: relative;
}

#thebar img {
position: absolute;
top: 50%;
right: 25px;
margin-top: -15px;
}

You could do similar for the text if it were a fixed font size.
 
K

KirstyH

Toby said:
Try something like:

#thebar {
position: relative;
}

I don't understand this. I thought that "relative" meant "relative to
the natural position of the element in the document flow". So why does
it make sense to position something relatively without specifying a
position?

Kirsty
 
N

Neal

I don't understand this. I thought that "relative" meant "relative to
the natural position of the element in the document flow". So why does
it make sense to position something relatively without specifying a
position?

A positioned element serves as a containing block for elements inside.
Elements not contained in a relative or absolute positioned element behave
according to the viewport. So by relative positioning a div, we can have
its contents floated and positioned relative to it instead.
 
T

Toby A Inkster

KirstyH said:
I don't understand this. I thought that "relative" meant "relative to
the natural position of the element in the document flow". So why does
it make sense to position something relatively without specifying a
position?

If you position an element relatively or absolutely it becomes what I
think the CSS specs call a "positioned element". Once you have a
positioned element, you can position absolute elements within it.

Example 1:
<body>
<div>
<span style="position:absolute;top:3px;left:8px;">x</span>
</div>
</body>

Example 2:
<body>
<div style="position:relative">
<span style="position:absolute;top:3px;left:8px;">x</span>
</div>
</body>

In example 1, the letter 'x' is positioned 3px down and 8px along from the
top left corner of <body>.

In example 2, the letter 'x' is positioned 3px down and 8px along from the
top left corner of the <div>.
 
B

Bigus

Toby A Inkster said:
Try something like:

#thebar {
position: relative;
}

#thebar img {
position: absolute;
top: 50%;
right: 25px;
margin-top: -15px;
}

You're a genius! Thank you.. I had to tweak the numbers a bit of course, but
it's worked and has taught me a couple of lessons on positioning.

Bigus
 
B

Bigus

Bigus said:
increases/decreases

You're a genius! Thank you.. I had to tweak the numbers a bit of course, but
it's worked and has taught me a couple of lessons on positioning.

hmmm.. just discovered it doesn't work properly in Opera. The effect in
Opera (7.23) is the same as IE/Mozilla if you removed the "top:50%;" part of
#thebar img :(

Bigus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top