CSS Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignCSS Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 25th, 2005, 09:50 PM
brisray brisray is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Terre Haute, Indiana
Posts: 32 brisray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 19 sec
Reputation Power: 5
Send a message via AIM to brisray Send a message via Yahoo to brisray
vertically centre text to an image

Using CSS, is it possible to vertically align centrally text to an image as you can using tables?

A simple example using a table would be

<table border="0" width="100%">
<tr>
<td><img border="0" src="image.gif" width="609"height="115"></td>
<td valign="middle">Ray did this</td>
</tr>
</table>

Using CSS so far I've got...

<p><img class="imglft" border="0" src="img.jpg" width="434" height="400" alt="my image">Ray did this>

The CSS for imglft is...

.imglft{
float: left;
margin-right: 30px;

}

I've tried putting the whole lot in a DIV and using text-align: middle and vertical-align: middle but that doesn't work.

You can see what I mean by comparing http://brisray.com/grad.htm or http://members.lycos.co.uk/brisray/grad.htm with http://brisray.com/ray/rgrad.htm or http://members.lycos.co.uk/brisray/ray/rgrad.htm

Ray

Reply With Quote
  #2  
Old June 26th, 2005, 12:20 AM
clm2206 clm2206 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Lima, Peru
Posts: 127 clm2206 User rank is Private First Class (20 - 50 Reputation Level)clm2206 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 Days 11 h 29 m 57 sec
Reputation Power: 4
Send a message via MSN to clm2206
Hi Ray

Images must not be left floated to keep itself in the same line text does, because they are treated like inline elements. Another tip, in CSS images don't need to be sized.

CSS file should be like this

Code:
	.imglft
	{
	border: 0;
	vertical-align: middle;
	}


and markup should be like this

Code:
<p><img class="imglft" src="anyimage.jpg" alt="my image">Ray did this</p>


That's it.

Good luck!

Carlos

Reply With Quote
  #3  
Old June 26th, 2005, 01:33 AM
kk5st's Avatar
kk5st kk5st is offline
Thanks Johnny Hart (BC) R.I.P.
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: May 2003
Location: Dallas
Posts: 4,674 kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 15 h 15 m 17 sec
Reputation Power: 687
Something like this will work if the text is limited to one line;
Code:
<div style=" border: 1px solid black;">
  <p>
    <img src="bullseye.jpg" 
         height="400" 
         alt="" 
         style="vertical-align: middle;" />some text
  </p>
</div>

If you don't need to cater to IE, you can use something like this;
Code:
<div style="display: table;">
  <div style="
      border: 1px solid black; display: table-cell; vertical-align: middle;">
    <p><img src="bullseye.jpg"
             height="400"
             alt="" /></p>
  </div>

  <div style="
      border: 1px solid black; display: table-cell; vertical-align: middle;">
    <p>some text</p>
    <p>some more text</p>
  </div>
</div>

Otherwise, if you need multiple lines centered, you're stuck with a 2-cell table.

cheers,

gary
__________________
There are those who manage to build a web site without knowing what they're doing; thereby proving to themselves they do, indeed, know what they're doing.

My html and css workshop, demos and tutorials.
Ask a better question, get a better answer.

Reply With Quote
  #4  
Old June 26th, 2005, 07:43 AM
brisray brisray is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Terre Haute, Indiana
Posts: 32 brisray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 19 sec
Reputation Power: 5
Send a message via AIM to brisray Send a message via Yahoo to brisray
thanks for the replies. I had a nasty feeling this wasn't going to work properly. After looking at several sites about CSS image and text positioning not one mentioned doing what I wanted to do.

I tried Carlos's suggestion and that doesn't work. Though it looks like it should.

Gary's idea looks good but according to my site stats - http://gostats.com/gogi/browser_war.pl?mn=brisray - won't work for over 90% of my visitors.

Looks like I'll have to choose between the "middled" tables or CSS.

Ray

Reply With Quote
  #5  
Old June 26th, 2005, 02:58 PM
Kravvitz's Avatar
Kravvitz Kravvitz is online now
CSS & JS/DOM Adept
Dev Shed God 23rd Plane (16000 - 16499 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 16,008 Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 3 Weeks 6 Days 3 h 36 m 48 sec
Reputation Power: 1544
Vertically Centering Elements with CSS

Quote:
Originally Posted by clm2206
Another tip, in CSS images don't need to be sized.

Images have never had to have their sizes specified, unless they were being used for something like the nasty transparent GIF hack.

Specifying the size for images usually will help the page to render faster.
__________________
Spreading knowledge, one newbie at a time.

Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Dynamic Site Solutions

Design/program for Firefox (and/or Opera), apply fixes for IE, not the other way around.

I'm looking for new clients.

Reply With Quote
  #6  
Old June 26th, 2005, 03:37 PM
kk5st's Avatar
kk5st kk5st is offline
Thanks Johnny Hart (BC) R.I.P.
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: May 2003
Location: Dallas
Posts: 4,674 kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 15 h 15 m 17 sec
Reputation Power: 687
clm2206: By specifying image dimensions, the browser may render the page while reserving space for the images. As images are loaded, it is unnecessary to redraw the page. The user may begin viewing/reading even as images are downloaded and placed in their reserved parking spots.

kravvitz: That's an elegant, if a bit klutzy, work-around for IE's lack of css support. Nice find.

I owe you some rep++. Can I get a proxy? Someone?

cheers,

gary

Last edited by kk5st : June 26th, 2005 at 03:39 PM.

Reply With Quote
  #7  
Old June 26th, 2005, 06:23 PM
kk5st's Avatar
kk5st kk5st is offline
Thanks Johnny Hart (BC) R.I.P.
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: May 2003
Location: Dallas
Posts: 4,674 kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 15 h 15 m 17 sec
Reputation Power: 687
bisray: I'll not argue about my example not being appropriate for the majority browser. I used it mostly to point out that there are css properties to handle your needs, except that IE has little support for css2. I had seen kravvitz's method (probably in another kravvitz post), but had not used it, so I didn't think to try to describe it, much less google for it. That left the 2-cell table.

I wonder about your stats. I don't know your target audience, but they must be technologically dysfunctional. Except for 28 instances of Opera[1], there seems to be no cases of any but out-of-the-box preloaded browsers[2]. I saw no NN6+, Mozilla, or Firefox.

Since your audience is about 98% IE5/6, and not exactly early adopters, are you making any effort to steer them away from such a security risk by suggesting they use Opera or Firefox?

cheers,

gary

[1] Opera <= 7 identified itself as IE, by default. Don't know about 8.

[2] NN4 was usually a manufacturer's preload, at least until IE6 arrived.

Last edited by kk5st : June 26th, 2005 at 06:27 PM.

Reply With Quote
  #8  
Old June 27th, 2005, 12:21 AM
clm2206 clm2206 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Lima, Peru
Posts: 127 clm2206 User rank is Private First Class (20 - 50 Reputation Level)clm2206 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 Days 11 h 29 m 57 sec
Reputation Power: 4
Send a message via MSN to clm2206
Quote:
Originally Posted by kk5st
clm2206: By specifying image dimensions, the browser may render the page while reserving space for the images. As images are loaded, it is unnecessary to redraw the page. The user may begin viewing/reading even as images are downloaded and placed in their reserved parking spots.


Hi Gary

Thank you very much, great tip!! So I must declare every image size for a faster rendering. Jus a couple of questions:

1. Where does it work better? In the CSS file or inside the <img> tag?

2. Does the same applies to background images?

Sorry for deviating the thread.

Carlos

Reply With Quote
  #9  
Old June 27th, 2005, 12:31 AM
Kravvitz's Avatar
Kravvitz Kravvitz is online now
CSS & JS/DOM Adept
Dev Shed God 23rd Plane (16000 - 16499 posts)
 
Join Date: Jul 2004
Location: USA
Posts: 16,008 Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level)Kravvitz User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Months 3 Weeks 6 Days 3 h 36 m 48 sec
Reputation Power: 1544
1) I don't know, maybe Gary does.
2) No, because space does not need to be reserved for them. How would you specify dimensions of a background image anyway?

Reply With Quote
  #10  
Old June 27th, 2005, 12:36 AM
clm2206 clm2206 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Lima, Peru
Posts: 127 clm2206 User rank is Private First Class (20 - 50 Reputation Level)clm2206 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 Days 11 h 29 m 57 sec
Reputation Power: 4
Send a message via MSN to clm2206
Quote:
Originally Posted by Kravvitz
How would you specify dimensions of a background image anyway?


You're right...

Reply With Quote
  #11  
Old June 27th, 2005, 12:41 AM
clm2206 clm2206 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Location: Lima, Peru
Posts: 127 clm2206 User rank is Private First Class (20 - 50 Reputation Level)clm2206 User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 3 Days 11 h 29 m 57 sec
Reputation Power: 4
Send a message via MSN to clm2206
Quote:
I tried Carlos's suggestion and that doesn't work. Though it looks like it should.


Hi Ray

I tested the code before pasting my reply. I don't doubt about your word, so there must be some inherited code that messes up what in theory should really work.

Take a look at this link and see it by yourself.

The only code I've added to the original posted lines above are image width and height attributes.

Regards,

Carlos

Reply With Quote
  #12  
Old June 27th, 2005, 02:35 AM
kk5st's Avatar
kk5st kk5st is offline
Thanks Johnny Hart (BC) R.I.P.
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: May 2003
Location: Dallas
Posts: 4,674 kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level)kk5st User rank is Brigadier General (60000 - 70000 Reputation Level) 
Time spent in forums: 1 Month 3 Days 15 h 15 m 17 sec
Reputation Power: 687
Quote:
Originally Posted by clm2206
Hi Gary

Thank you very much, great tip!! So I must declare every image size for a faster rendering. Jus a couple of questions:

1. Where does it work better? In the CSS file or inside the <img> tag?
I have not a clue. My gut sez as an html attribute, but I have no reason. :shrug: OTOH, as an attribute, it applies to that image and no other. As a css property, you'd have to create classes, or ids, or some other cascade of selectors to indicate just which image you mean.

One more reason to always dimension your images; if the link breaks, not having the size will cause your layout to cinch up into the space you meant for the pic.

As to the snipped query no. 2; what kravvitz said.

cheers,

gary

Last edited by kk5st : June 27th, 2005 at 02:42 AM.

Reply With Quote
  #13  
Old June 27th, 2005, 03:02 AM
brisray brisray is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: Terre Haute, Indiana
Posts: 32 brisray User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 17 m 19 sec